| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 847 人关注过本帖
标题:分享关于Button的一点小小小经验
只看楼主 加入收藏
novker
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
结帖率:100%
收藏
 问题点数:0 回复次数:9 
分享关于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
hxxiaoxiao
Rank: 2
等 级:论坛游民
威 望:1
帖 子:29
专家分:81
注 册:2010-10-13
收藏
得分:0 
恕我无看懂
2010-10-20 19:18
何事惊慌
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:220
专家分:499
注 册:2008-7-2
收藏
得分:0 
最好是直接上传个编译好的!大家好直接到效果

QQ:860234001
编程交流群:236949758
2010-10-22 22:28
moridiansha
Rank: 6Rank: 6
来 自:承德
等 级:侠之大者
威 望:4
帖 子:254
专家分:417
注 册:2009-10-21
收藏
得分:0 
看着头晕

www.qysy.tk
2010-10-23 10:37
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
收藏
得分:0 
的确很长的代码啊!!
2010-10-23 11:11
windowswind
Rank: 1
等 级:新手上路
帖 子:39
专家分:3
注 册:2010-3-16
收藏
得分:0 
虽然看的不太明白,不过还是谢谢楼主分享了,等有能力看懂了再回来看。。。哈哈!
2010-11-04 19:27
欲与天斗
Rank: 4
等 级:业余侠客
帖 子:88
专家分:270
注 册:2010-10-6
收藏
得分:0 
路过
2010-11-05 08:34
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
推荐楼主 看看代码规范相关的书籍 比方说c#编程风格

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-11-05 08:54
novker
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2008-10-20
收藏
得分:0 
恩的,哈哈。我刚学c#,继续努力!
2010-11-07 08:27
方杰斌
Rank: 2
等 级:论坛游民
帖 子:42
专家分:45
注 册:2010-10-26
收藏
得分:0 
  好!支持一下!  表我决心!  好好努力!

邻家货站
2010-11-08 16:21
快速回复:分享关于Button的一点小小小经验
数据加载中...
 
   



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

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