使用动态按钮 [英] Working with Dynamic buttons

查看:65
本文介绍了使用动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

现在,我想了解一些有关动态按钮问题的信息.在此之前,请清楚地解释我的问题.我有100个按钮.并且每个按钮具有相同的属性和事件 BUT 不同 NAME .除了按钮 名称之外,按钮点击事件中的所有代码均相同.在这里我该如何解决这个问题,我不喜欢编写程序更多的页面.欢迎所有答复.

在此先感谢

Dhinesh Kumar.V

Dear All,

Now i wanna to know something about dynamic buttons problem. Before that clearly explain my problem. I have 100 buttons. and each button is have same property and event BUT different NAME. Am writing all codes are same inside button click event except BUTTON Name. Here how can i solve this problem, i dont like to write program more pages. All replies are welcome.

Thanks in Advance

Dhinesh Kumar.V

推荐答案

1-选择所有按钮,然后转到属性选项卡
2-选择事件图标.
3- on Click事件下拉列表旁边的并选择包含操作的方法
1- select all buttons then go to property tab
2- select event icon.
3- on Click event drop down the list beside and select the method that contains the action


您可以在类似这样的函数中获取按钮的文本
You can get the Text of button in the function like this
 private void OnPress(object sender, System.EventArgs e)
        {
            ((Button)sender).Text; // to get Text inside

//((Button)sender).Name; to get name
        }


我认为您想要以下内容
I think you want something like following
private void Common_btn_click_event_Click(object sender, EventArgs e)
       {
           Button btn=((Button)sender);
           string btn_text= btn.text;
           if (btn.Text == "Room 1")
           {
                    Frm_Customer _Frm_Customer = new Frm_Customer();
                    _Frm_Customer.refToFrm_Rooms = this;
                    _Frm_Customer.ButtonBase = btn.text;
                    btn_Room1.BackColor = Color.Red;
                    this.Visible = true;
                   _Frm_Customer.Show();
           }
           else if (btn.Text == "Reserved")
           {
                   DialogResult dlgResult = MessageBox.Show("You Have More Time?", "Do You wnat to Vocate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
           if (dlgResult == DialogResult.Yes)
           {
           {
                   btn.BackColor = Color.Red;
                   if(btn.Name=="btn_Room1")
                      btn.Text = "Room 1";
                   else if(btn.Name=="btn_Room2")
                      btn.Text = "Room 2";  
                   SqlConnection con = new SqlConnection();
                   string[] conString = DBSettings.connectionStringValues();
                   string connectionString = string.Format(@"server=" + conString[0] + ";database=Restaurant;uid=" + conString[2] + ";pwd=" + conString[3]);
                   con.ConnectionString = connectionString;
                   con.Open();
                   SqlCommand cmd = new SqlCommand("update AllRooms set RunFlag=0 where RoomName='"+ btn.Text +"'", con);
                   SqlDataReader _sqldatareader = cmd.ExecuteReader();
                   _sqldatareader.Read();
                   con.Close();
             }
             }
             else if(dlgResult==DialogResult.No)
             {
             }
           }
       }


这篇关于使用动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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