Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i have a main form and some user control that i add to main form
for each user control i add a button in main form dynamically .
now how i can create click event for each created button that show a added user control
example :
i added 2 user control to main form controls so i have 2 button on main form. so i click first button and i want show first user control that added
Posted

First of all, why don't you add the button to your user control instead of on the form?

if this is really your business case:
Add controls to form[^]
then you'll need to attach an event handler to it.

I suggest you make sure the Button has a unique name or id in it's properties and let all buttons call the same handler. In that handler you check who called it and act accordingly.
Example[^]

in this example you could modify the code similar to this:
C#
public partial class RoutedEventAddRemoveHandler {
    void MakeButton(object sender, RoutedEventArgs e)
    {
        Button b1 = new Button();
        Button b2 = new Button();
        b1.Content = "New Button 1";
        b2.Content = "New Button 2";
        
        // Associate event handler to the button. You can remove the event  
        // handler using "-=" syntax rather than "+=".
        b1.Click  += new RoutedEventHandler(OnbClick);
        b2.Click  += new RoutedEventHandler(OnbClick);
        //set some properties like location etc here.
        //make sure to ADD the button to a control!
        text1.Text = "Now click the second button...";
       
    }
    void OnbClick(object sender, RoutedEventArgs e)
    {
        if(  ((Button)sender).Content == "New Button 1"){
          text1.Text = "New Button (b1) Was Clicked!!";
        }
        else{
          text1.Text = "New Button (b2) Was Clicked!!";
        }
    }


perhaps "Content" is not the best property to set uniquely, so find another like Name or Tag (depending on what is available)

hope this helps.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 22-Jan-14 9:35am    
5
V. 22-Jan-14 11:30am    
thx :-)
As you mentioned that you are adding user controls and buttons dynamically then you will have to attach an event handler for the click event of the button just after you are creating the instance of the button and user controls.

C#
UserControl control1 = new UserControl(); // create the instance of your user control here.
Button button1 = new Button(); // create the instance of button here.

//Implement button click event handler which will show the user control.
button1.Click += (sender, eventArgs) => 
{
    //Show the user control here. 
};
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 22-Jan-14 9:35am    
5
Assuming:

1. you add both Buttons and UserControls at run-time.

2. UserControls and Buttons are initially visible.

3. clicking the Button associated with each UserControl switches the visibility of the UserControl from hidden to shown, and shown to hidden.
C#
// test adding eight UserControls and buttons.

// this dictionary enables easy look-up of
// the UserControl associated with each Button
private Dictionary<Button, UserControl1> dctBtnToUC = new Dictionary<Button, UserControl1>();

private void addUCs()
{
    UserControl1 uc1;
    Button btn;
    int offH;

    for (int i = 0; i < 8; i++)
    {
        offH = 80 + (i * 90);

        btn = new Button { Text = "Hide UC", Location = new Point(offH, 100) };
        uc1 = new UserControl1 { Name = "uc_" + i.ToString(), Location = new Point(offH, 200)};

        this.Controls.Add(btn);
        this.Controls.Add(uc1);

        dctBtnToUC.Add(btn, uc1);

        btn.Click += btn_Click;
    }
}

// Button Click EventHandler
private void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;

    // get the associated UserControl
    // from the Dictionary using the
    // Button as the Key
    UserControl1 uc = dctBtnToUC[btn];

    uc.Visible = !uc.Visible;

    btn.Text = uc.Visible ? "Hide UC" : "Show UC";
}
 
Share this answer
 

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