Samentu

Hi everybody. I have a brand new, most likely, stupid question. Is it possible to detect when I click some controls on a form, controls which have been added at runtime The controls are situated in a FlowLayoutPanel but when I try to use flowLayoutPanel1_MouseClick it just seems to detect mouse clicks outside the dynamically added control and does not detect when I actually click the control. Any ideas

Re: Windows Forms General Detect clicks on dynamically added controls (controls added at runtime in fact)

Rizshe

Hi ya,


That's because when you dynamically added a control or controls by programming then you would have to declare there event handlers. In simple words for a dynamic button you would need a click event if you want mouse click.

Here is small code for how to define an event handler

Code Snippet

AddHandler Button4.Click, AddressOf Button4_Click

Then you would need to write Button4_Click method by yourself.


Hope that helps


Cheers

Rizwan






Re: Windows Forms General Detect clicks on dynamically added controls (controls added at runtime in fact)

Samentu

I forgot to mention that my code is C# but I got it to work like this:

Code Snippet
this
.myFlowLayoutPanel.Click += new EventHandler(this.MyFlowLayoutPanel_click);

and then I did my job in the method:

Code Snippet

void MyFlowLayoutPanel_click(object sender, EventArgs e)

{

}

This approach is perfect only if MyFlowLayoutPanel has no controls in it.

Still .... I should add that the dynamically added control is a FlawLayoutPanel (with AutoSize = true) which in turn has in it a PictureBox and two Label controls. Your code works only if I click the FlowLayoutPanel just near it's edges, so neither the PictureBox nor any of the Label controls gets clicked. So I tried adding the event handlers for each control in the FlowLayoutPanel but then it won't even add the FlowLayoutPanel.

Shouldn't the Click event cover the whole area of the FlowLayoutPanel regardless of the fact that the panel is empty or not





Re: Windows Forms General Detect clicks on dynamically added controls (controls added at runtime in fact)

Milo7874

In the case of overlapping controls, the click event only works on the topmost control in the z-order. There is no such thing as a having a parent control handle all of the click events for the child controls. The only way to do that would be to detect what coordinates the user clicked in and do a coordinate map to the appropriate control. As for the events on the buttons and labels themselves, this routine will recursively add the click event handler to every control in the panel.

Code Snippet

public void Control_Click(object sender, EventArgs e)

{

//do something here

}

public void AddEventHandlers(Control.ControlCollection Controls)

{

foreach (Control ctl in Controls)

{

ctl.Click += new EventHandler(Control_Click);

if (ctl.Controls.Count > 0) AddEventHandlers(ctl.Controls);

}

}

All you need to do is call it after the form has been initialized.

Code Snippet

public Form1()

{

InitializeComponent();

AddEventHandlers(this.myFlowLayoutPanel.Controls);

}





Re: Windows Forms General Detect clicks on dynamically added controls (controls added at runtime in fact)

Rizshe

Well not really. Create a dynamic control click event and in form_load try using this code

Code Snippet
yourctl.Click += formLayoutPanel1_Click;

Make sure that you define you own event for the formLayoutPanel1_Click as well.

Cheers

Rizwan






Re: Windows Forms General Detect clicks on dynamically added controls (controls added at runtime in fact)

Samentu

I've done it Big Smile Thank you both for your answers, you guys showed me the way Smile I've learned from Rizshe how to handle a dynamically added control and Milo7874 succesfully showed me how to recursively handle all "subcontrols" of a certain control. As for getting the position of the parent control, I did the followings: I've named the parent control and at each click on that control or on a sub control I ckecked the name of the clicked control. If one of the child control was clicked, I checked it's parent name and so on until I got the coordinates of the "parent control". Thank you both again.