| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 847 人关注过本帖
标题:分享关于Button的一点小小小经验
取消只看楼主 加入收藏
novker
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
结帖率:100%
收藏
 问题点数:0 回复次数:1 
分享关于Button的一点小小小经验
在form2.load事件里 自动生动N个 button ,button的名称为 MyButton0001,MyButton0002....  把button加到GroupBox里,button单击事件为Mybutton_click(),  单击事件 是调用 form3 窗口.  form3窗口关闭后会返回Boolean 值,根据Boolean值来判断Button是否可用。
可以使用。但是

我在form2的窗体上加了一个菜单,其中有一个下拉菜单调用 form9窗体, 当form9窗体关闭后调用一个 字符串,比如“0001”,利用这个字符串 生动一个Button,  Button b1 = new Button();  b1.name="MyButton"+"0001" (form9窗体关闭后调用一个 字符串) ,b1.enable=true/false;  问题出在这里,无论 b1.enable = true/false;  GroupBox 里和刚刚创建的Button,虽然同名,但两个互不影响。

解决方法:

在form2 里定义一个  public  object  buttonsender;  在GroupBox 里自动生成Button的事件里把 sender 赋值给 buttonsender

在 form9 窗体关闭后 判断 buttonsender 是否为空.  不为空就  Button b1 = (Button)buttonsender.  b1.enable=true/false;

ok,解决关闭。

问题不大,但出现了用了一个小时才弄明白,小弟新手,贴出来分享,希望有用。


using System;
using System.Threading;
using System.Collections.Generic;
using
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FaPiao
{
    public partial class Form2 : Form
    {
        public string glyy,ttth;
        public object buttonsender=null;
        public int numms=0;

        //public Button[] b;

        public Form2()
        {
            

            InitializeComponent();


        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            Form3 myfm3 = new Form3();
            (this.glyy);
             = this.glyy;
            myfm3.ShowDialog();
            if (myfm3.thnums!=string.Empty)
            {
                Button b1 = new Button();
                Mydata mydata1 = new Mydata();
                b1.Name = "MyButton" + myfm3.thnums ;
                b1.Enabled = mydata1.truebutton(myfm3.thnums);
                mydata1.closedb();
            }
        }

        private void 帮助ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form4 myfm4 = new Form4();
            myfm4.ShowDialog();
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form6 myform = new Form6();
            myform.ShowDialog();
        }

        private void 查询数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5 myform5 = new Form5();
            myform5.ShowDialog();
        }

        private void 账单修改ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form7 myform7 = new Form7();
            myform7.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void 打印机设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form8 myfrm = new Form8();
            myfrm.ShowDialog();
        }

        private void myButton_Click(object sender, EventArgs e)
        {
            buttonsender = sender;
            Button b1 =(Button)sender;
           // MessageBox.Show(b1.Name.ToString());
            Form3 myfrm = new Form3();

            myfrm.textBox1.Text = b1.Text;
            (this.glyy);
             = this.glyy;
            myfrm.ShowDialog();
            Mydata mydata1 = new Mydata();
            b1.Enabled = ( mydata1.truebutton(b1.Text) );
            mydata1.closedb();
        }


        private void scbuttontwo(int qss,int zs)
        {
            Mydata mydata1 = new Mydata();
            int numx, numy, numrow, numcol, numsum;
            numrow = groupBox5.Height / 33;
            numcol = groupBox5.Width / 85;
            numx = 1; numy = 1;
            numsum = qss;

           // Button[] b = new Button[zs-qss+1- get_button_num(qss,zs)+50];

            for (int i = 1; i < numrow; i++)
            {
                for (int j = 1; j < numcol; j++)
                {

                    if (numsum <= zs)
                        {
                            string strlen;
                            if (( numsum.ToString().Substring(numsum.ToString().Length-1) )=="4")
                            {
                                numsum += 1;
                            }



                            Button b = new Button();
                           
                            // b.Text = "第" + numsum.ToString() + "台桌";
                           

                            switch (numsum.ToString().Length)
                            {
                                case 1: strlen = "000" + numsum.ToString(); break;
                                case 2: strlen = "00" + numsum.ToString(); break;
                                case 3: strlen = "0" + numsum.ToString(); break;
                                default: strlen = numsum.ToString(); break;

                            }
                            b.Name ="MyButton"+ strlen;
                          //  b[numsum].Name = strlen;
                            b.Text = strlen;

                            b.Enabled = mydata1.truebutton(strlen);
                            numx = (i * 23 + i * 10);
                            numy = (75 * j + j * 10);
                            if (numx <= groupBox5.Left)
                            {
                                b.Location = new Point(numy, numx);

                                //   b.Location = new Point((i * 75 + i * 10), 23 + i * 10);
                                this.groupBox5.Controls.Add(b);
                            }


                            b.Click += new EventHandler(myButton_Click);
                            numsum += 1;
                        }
                        else
                        {
                                numsum += 1;
                        }
 
                }
            }
            mydata1.closedb();
           // MessageBox.Show(numrow.ToString(),numcol.ToString());
        }

        private void Form2_Load(object sender, EventArgs e)
        {
          //  ThreadStart ths = new ThreadStart(scbutton);
           // Thread thread = new Thread(ths);
          //  thread.Start();
          //  Thread.Sleep(2000);
            int[] cs_lh1 = new int[2];
            int[] cs_lh2 = new int[2];
            

            cs_lh1 = csh_th("一楼","一楼表");

            cs_lh2 = csh_th("二楼","二楼表");



            scbuttonone(cs_lh1[0], cs_lh1[1]);
            scbuttontwo(cs_lh2[0], cs_lh2[1]);

          //  MessageBox.Show(glyy);
            trueqx(glyy);


           // MessageBox.Show(System.DateTime.Now.ToString("yyyy-MM-dd").Substring(0,10));
        }

        private void 账单结算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            Form9 myfrm = new Form9();
            myfrm.ShowDialog();

            

            if (myfrm.taihaonum != string.Empty && buttonsender!=null)
            {
                Button b = (Button)buttonsender;
                Mydata mydata1 = new Mydata();
               
                b.Enabled = mydata1.truebutton(myfrm.taihaonum);
                mydata1.closedb();
            }

        }

        private void trueqx(string glyy1)
        {
            Mydata mydata1 = new Mydata();

            添加用户ToolStripMenuItem.Enabled = mydata1.getqxnum(glyy);
            账单修改ToolStripMenuItem1.Enabled = mydata1.getqxnum(glyy);
            账单结算ToolStripMenuItem.Enabled = mydata1.getqxnum(glyy);
            帮助ToolStripMenuItem1.Enabled = mydata1.getqxnum(glyy);
            台号设置ToolStripMenuItem.Enabled = mydata1.getqxnum(glyy);
            mydata1.closedb();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Form10 myfrm = new Form10();
            myfrm.textBox6.Text = this.glyy;
            myfrm.ShowDialog();
        }

        private int[] csh_th(string loushu,string tabstr)
        {
            Mydata mydata1 = new Mydata();
            DataSet ds = mydata1.taihaonumber(loushu,tabstr);

            int[] shuzu = new int[2];

            shuzu[0] = Int32.Parse( ds.Tables[tabstr].Rows[0][0].ToString());
            shuzu[1] = Int32.Parse(ds.Tables[tabstr].Rows[0][1].ToString());

            return shuzu;
        }


        private void scbuttonone(int qss, int zs)
        {
            Mydata data1 = new Mydata();
            int numx, numy, numrow, numcol, numsum;
            numrow = groupBox4.Height / 33;
            numcol = groupBox4.Width / 83;
            numx = 1; numy = 1;
            numsum = qss;
            for (int i = 1; i <numrow  ; i++)
            {
                for (int j = 1; j < numcol; j++)
                {

                    if (numsum <= zs )
                    {
                        string strlen;
                        if ((numsum.ToString().Substring(numsum.ToString().Length - 1)) == "4")
                        {
                            numsum += 1;
                        }



                        Button b = new Button();
                       // b.Name = "mybutton" + numsum.ToString();
                        // b.Text = "第" + numsum.ToString() + "台桌";


                        switch (numsum.ToString().Length)
                        {
                            case 1: strlen = "000" + numsum.ToString(); break;
                            case 2: strlen = "00" + numsum.ToString(); break;
                            case 3: strlen = "0" + numsum.ToString(); break;
                            default: strlen = numsum.ToString(); break;

                        }
                        b.Name ="MyButton"+ strlen;
                        b.Text = strlen;

                        b.Enabled= data1.truebutton(strlen);

                        if (j == 1)
                        {
                            numx = (15 * j );
                            numy = (i * 30 );

                        }
                        else
                        {
                            numx = (15 * j + (j-1)*70);
                            numy = (i * 30 );
                        }
                        //MessageBox.Show(numx.ToString(),numy.ToString());
                        //MessageBox.Show(groupBox4.Left.ToString());
                        if (numy <= groupBox4.Width-75)
                        {
                            b.Location = new Point(numx, numy);

                            //   b.Location = new Point((i * 75 + i * 10), 23 + i * 10);
                            this.groupBox4.Controls.Add(b);


                        }


                        b.Click += new EventHandler(myButton_Click);
                        numsum += 1;
                    }
                    else
                    {
                        numsum += 1;
                    }

                }
            }

            data1.closedb();
            // MessageBox.Show(numrow.ToString(),numcol.ToString());
        }

        private void 台号设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form12 myfrm = new Form12();
            myfrm.ShowDialog();
        }

        private int get_button_num(int qss, int zs)
        {
            for (int i = qss; i <= zs; i++)
            {
                if ((i.ToString().Substring(i.ToString().Length - 1)) == "4")
                {
                    numms += 1;
                }
            }

            return numms;
        }

        private void 帮助ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            AboutBox1 mf = new AboutBox1();
            mf.ShowDialog();
        }
    }
}
搜索更多相关主题的帖子: Button 经验 分享 
2010-10-20 10:51
novker
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
收藏
得分:0 
恩的,哈哈。我刚学c#,继续努力!
2010-11-07 08:27
快速回复:分享关于Button的一点小小小经验
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021025 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved