在C#Windows应用程序中创建动态按钮 [英] Create Dynamic buttons in C# windows application

查看:61
本文介绍了在C#Windows应用程序中创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我必须根据我的studentDataBase中学生的姓名在Windows窗体控制面板旁边创建一些动态按钮.
以下是我的代码

Hi All
I have to create number of dynamic buttons in side the windows form control panel according to the name of student in my studentDataBase.
Below is my code

private void Form1_Load(object sender, EventArgs e)
        {
            string qry = "select rack from Report ";
            OleDbCommand cmd = new OleDbCommand();
            cmd = new OleDbCommand(qry, conn);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string count = "Select COUNT(Name) FROM studentDataBase";
                cmd = new OleDbCommand(count, conn);
                dr = cmd.ExecuteReader();
                int x;
                int k;
                if(dr.HasRows==true)
                {
                    createButton();
                }
                
                
            }
        }


private void createButton()
           {
               //This block dynamically creates a Button and adds it to the form
               Button btn = new Button();
               btn.Name = "btn1";
               btn.Location = new Point(3, 14);
               btn.BackColor = System.Drawing.Color.White;
               //Hook our button up to our generic button handler
               btn.Click += new System.EventHandler(this.btn_Click);
               panel1.Controls.Add(btn);
           }




但这对我不起作用.谁能帮我吗




感谢All




But it is not working for me. Can anyone help me Please




Thanks to All

推荐答案


您的代码很好,仅在创建任何按钮的位置出现问题的原因才出现,这就是为什么您看不到它的原因.
Hi ,
Your code is fine the problem only on location any button create appear in the same point that''s why you can''t see it .
int i = 0;
      int x = 0;
      private void createButton()
      {
          //This block dynamically creates a Button and adds it to the form
          Button btn = new Button();
          btn.Name = "btn1";
          btn.Location = new Point(3+i, 14 + x);
          btn.BackColor = System.Drawing.Color.White;
          //Hook our button up to our generic button handler
          btn.Click += new EventHandler(btn_Click);
          panel1.Controls.Add(btn);
          i += 10;
          x += 10;

      }

      void btn_Click(object sender, EventArgs e)
      {
          MessageBox.Show("Test");
      }


在此代码中,有可能使多个按钮具有相同的名称,这会产生编译错误.因此请注释该行代码,或尝试为按钮指定其他名称.

In this code there is a chance of getting same name to more than one button and this wil create compile error. so comment that line of code or try to give different name to the buttons.

private void createButton()
           {
               //This block dynamically creates a Button and adds it to the form
               Button btn = new Button();
             //  btn.Name = "btn1";
               btn.Location = new Point(3, 14);
               btn.BackColor = System.Drawing.Color.White;
               //Hook our button up to our generic button handler
               btn.Click += new System.EventHandler(this.btn_Click);
               panel1.Controls.Add(btn);
           }


设置动态按钮的ID

set an id for your dynamic button

Button bt = new Button();
        bt.ID = "btn_Dynamic1";


然后在css文件或内部样式中添加"btn_Dynamic1"的条目,以使其位置和样式完美...

如果要创建无限数量的按钮,则最好在创建控件时尝试使用代码来设置样式..


then add an entry for "btn_Dynamic1" in css file or internal style to make its position and style perfect...

if you want to create an indefinite number of buttons then its better to try styling from code while creating the control..

Button bt = new Button();
       bt.ID = "bt1";
       bt.Style.Add("position","absolute");
       bt.Style.Add("top","50px");
       bt.Style.Add("left","50px");
       bt.Style.Add("width","100px");
       this.form1.Controls.Add(bt);


这篇关于在C#Windows应用程序中创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆