Home > other >  Interacting between items in the same list C# (win forms)
Interacting between items in the same list C# (win forms)

Time:09-08

I'm trying to create a list of buttons and a list of textboxes, and I need to interact between these elements. I mean, whenever I click on the button, I need to do some action with the textbox in the same line. I tried to use delegate on the click event, but I can't really find a way to point the textbox I want to target.

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Multiselect = true;
        openFileDialog.ShowDialog();

        string[] files = openFileDialog.FileNames.ToArray();

        int index = 0;

        List<Button> EditButtons = new List<Button>();
        List<TextBox> TextBoxes = new List<TextBox>();

        foreach (var file in files)
        {
            EditButtons.Add(new Button() { Text = "Edit", Top = index  50, Left = 150 });
            TextBoxes.Add(new TextBox() { Text = file, Top = index  50, Left = 5 });
            index = index   25;
        }

        foreach (Button button in EditButtons)
        {
            Controls.Add(button);
        }

        foreach (TextBox textBox in TextBoxes)
        {
            Controls.Add(textBox);
        }
    }

CodePudding user response:

The tag approach works as described on the previous answer, this snippet should work, just change the pnlTextBox and pnlButtons to your needs:

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Multiselect = true;
        openFileDialog.ShowDialog();
        string[] files = openFileDialog.FileNames.ToArray();

        int index = 0;

        
        foreach (var file in files)
        {
            var button = new Button()  { Text = "Edit", Top = index  50, Left = 150 };
            var textBox = new TextBox() { Text = file, Top = index  50, Left = 5 };

                button.Tag = textBox;
                button.Click  = Button_Click;

            pnlTextBox.Controls.Add(textBox);//Add the textbox to the container i used a panel here
            pnlButtons.Controls.Add(button);//Add the button to the container i used a panel here


            index = index   25;
        }
       
    }

private void Button_Click(object sender, EventArgs e)
   {
    var button=((Button)sender);
    var textBox = ((TextBox)(button.Tag));
    textBox.Text = button.Text;//example to access the proper textbox
   }

CodePudding user response:

There are a number of ways to handle this. You could stick the text box into the Tag property of the button as you create them like this:

foreach (var file in files)
{
    var txt = new TextBox() { Text = file, Top = index  50, Left = 5 };
    var btn = new Button() { Text = "Edit", Top = index  50, Left = 150, Tag = 
        txt };
    EditButtons.Add(btn);
    TextBoxes.Add(txt);
    index = index   25;
}

Then when you hanlde the click event get the text box using this line:

var txt = (sender as Button).Tag

Obviously there are other ways like putting the button and textbox in a dictionary, but I think this is the simplest approach.

  • Related