Resolved Click event handlering from form to usercontrol

Beji

New member
Joined
Aug 10, 2021
Messages
4
Programming Experience
Beginner
Hey guys I wanna ask how forward event handler for click on dynamic generate buttons. This buttons I generate to flowlayout panel in Form1.cs
I generate usercontrol "btnMini.cs" where I have it 2 button.
How can i forward click event for these buttons from Form1 to UserControl - "btnMini.cs" ?
I wanna after click on button forward and get messagebox.show where is "Parent name of button" and other show me "button name". How can I do it from Form1?

Button btn = (Button)sender; MessageBox.Show("Click btn: " + btn.Name + ", Parent name control: " + btn.Parent.Name);

I try this but I dont know how do it when I need create MessageBox.Show in Form1 after Clicking....
Thank you for advice??. I am little hopeless how do it :/ so thanks very body for tips.
asasdasdaa.png


C#:
 public partial class btnMini : UserControl
            {
                public btnMini()
                {
                    InitializeComponent();
                }
                [Browsable(true)]
                [Category("ActionClick")]
                [Description("Invoke when user clicks on button")]
                public event EventHandler ButtonClick;//ok
             
                private void TestingClicks(object sender, EventArgs e)
                {
                    ButtonClick?.Invoke(sender, e);
                }
                public void OnClick(object sender, EventArgs e)
                {
                    TestingClicks(sender, e);         
                }
        }
 
        public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                #region variables
                private int counter = 1;
                private btnMini btnM = new btnMini();
                private List<Control> control = new List<Control>();
                //public event EventHandler ClickTest;
                #endregion
     
                //Using this method I adding userControl to list
                private void button1_Click(object sender, EventArgs e)
                {
                    //Button btn = (Button)sender;
                    //MessageBox.Show("Click btn: " + btn.Name + ", Parent name control: " + btn.Parent.Name); //Here I tried do this for get Name of parent and button UserControl - btnMini.cs
     
                    btnM = new btnMini();
                    btnM.Name = "btn_" + counter;
                    //btnM.OnClick += userControl11_CloseButtonClicked;
                    //btnM.Click += (sender, args) => btnM.OnClick(this, e);
     
                    //btnM.ButtonClick += new EventHandler(Btn_Click);;
                    //btnM.Click += (sender, args) => btnM.OnClick(this, e);
                    //btnM.ButtonClick += btn;
                    //btnM.Click += (sender, args) => btnM.ButtonClick?.Invoke(sender, args);
                    btnM.OnClick+ new EventHandler(??????);
                    counter++;
                    control.Add(btnM);
     
                }
               //method for adding UserControl from List.
                private void button2_Click(object sender, EventArgs e)
                {
                    foreach (var b in control)
                    {
                        this.flowLayoutPanel1.Controls.Add(b);
                    }
                }
        }
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
In the future, please use CODE tags (the button the tool bar looks like </>) instead of ICODE tags (the button on the tool bar looks like >_). I know that it's confusing because the common convention is that the first symbol looks like HTML embedding, and the second symbol is the same symbol used by StackOverflow.

Also, I'm moving your thread to WinForms...
 

Beji

New member
Joined
Aug 10, 2021
Messages
4
Programming Experience
Beginner
In the future, please use CODE tags (the button the tool bar looks like </>) instead of ICODE tags (the button on the tool bar looks like >_). I know that it's confusing because the common convention is that the first symbol looks like HTML embedding, and the second symbol is the same symbol used by StackOverflow.

Also, I'm moving your thread to WinForms...
Thanks and sorry :)
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
I'm glad you resolved your issue. In the future, please post your code in code tags instead of a screenshot of your code. It's hard to read code on small devices, and screenshots also eats into people's data quotas.
 

Beji

New member
Joined
Aug 10, 2021
Messages
4
Programming Experience
Beginner
I'm glad you resolved your issue. In the future, please post your code in code tags instead of a screenshot of your code. It's hard to read code on small devices, and screenshots also eats into people's data quotas.
Ok sorry :) my bad. Next time I will be careful posting code block and not image screen of code. :)
 
Top Bottom