Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Comments
Sangramsingh Pawar 4-Aug-12 7:02am    
please improve your question I can't understand your problem
Dhinesh kumar.V 4-Aug-12 7:40am    
sure sir. For ex i have 5 buttons. btn 1, btn 2...btn5. Here my all buttons are used to extract data from datbase. But inside of my coding contain BUTTON NAME. That place only i wanna change my button name. All other places are same coding.
Sangramsingh Pawar 4-Aug-12 7:47am    
can you post your code?
Dhinesh kumar.V 7-Aug-12 0:48am    
private void btn_Room1_Click(object sender, EventArgs e)
{
if (btn_Room1.Text == "Room 1")
{
Frm_Customer _Frm_Customer = new Frm_Customer();
_Frm_Customer.refToFrm_Rooms = this;
_Frm_Customer.ButtonBase = "Room1";
btn_Room1.BackColor = Color.Red;
this.Visible = true;
_Frm_Customer.Show();
}
else if (btn_Room1.Text == "Reserved")
{
DialogResult dlgResult = MessageBox.Show("You Have More Time?", "Do You wnat to Vocate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgResult == DialogResult.Yes)
{
{
btn_Room1.BackColor = Color.Red;
btn_Room1.Text = "Room 1";
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='Room1'", con);
SqlDataReader _sqldatareader = cmd.ExecuteReader();
_sqldatareader.Read();
con.Close();
}
}
else if(dlgResult==DialogResult.No)
{
}
}
}

private void btn_Room2_Click(object sender, EventArgs e)
{
if (btn_Room2.Text == "Room 2")
{
Frm_Customer _Frm_Customer = new Frm_Customer();
_Frm_Customer.refToFrm_Rooms = this;
_Frm_Customer.ButtonBase = "Room2";
btn_Room1.BackColor = Color.Red;
this.Visible = true;
_Frm_Customer.Show();
}
else if (btn_Room2.Text == "Reserved")
{
DialogResult dlgResult = MessageBox.Show("You Have More Time?", "Do You wnat to Vocate?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgResult == DialogResult.Yes)
{
{
btn_Room2.BackColor = Color.Red;
btn_Room2.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='Room2'", con);
SqlDataReader _sqldatareader = cmd.ExecuteReader();
_sqldatareader.Read();
con.Close();
}
}
else if (dlgResult == DialogResult.No)
{
}
}
}



Here Room1-> Button1(btn_Room1), Room2->Button2(btn_Room2)...etc
AbO_OsAmH 4-Aug-12 7:06am    
Do you wont to make all buttons have the same code ????
if this is true look for answer below.

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
 
Share this answer
 
Comments
Dhinesh kumar.V 7-Aug-12 5:57am    
Dear All,
please explain clearly.
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
        }
 
Share this answer
 
I think you want something like following
C#
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)
             {
             }
           }
       }
 
Share this answer
 
Comments
Dhinesh kumar.V 7-Aug-12 5:58am    
But where i get Common_btn_Click_event.
Sangramsingh Pawar 7-Aug-12 8:10am    
copy and paste above code in private void btn_Room1_Click(object sender, EventArgs e)
and bind others button's click event to this event method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900