Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi select ctrl+button click at runtime

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.
like image 509
Viva Avatar asked Nov 04 '22 01:11

Viva


1 Answers

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);
}
like image 118
Sergey Berezovskiy Avatar answered Nov 08 '22 09:11

Sergey Berezovskiy