On my winform I have usercontrols that are created dynamically at every button click. I want at runtime to be able to select them just by clicking once on them and then having ctrl button pressed. I managed to do it ,but just for one . How can I make to work for all of them? My code:
private void TControl_Click(object sender, EventArgs e) //TControl is the name of usercontrol
{
TControl tc = new TControl();
Control ctrl = sender as Control;
if (ctrl != null)
tc = ctrl;//it doesn't work like this.
You can have list of selected controls. Just determine if Ctrl was pressed when you clicked on control and add it to selected list (you can also remove it if control was added before):
List<TControl> selectedControls = new List<TControl>();
private void TControl_Click(object sender, EventArgs e)
{
if ((ModifierKeys & Keys.Control) == 0)
return;
TControl tc = (TControl)sender;
if (selectedControls.Contains(tc))
return; // you can remove control here
selectedControls.Add(tc);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With