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?
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.
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.
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: