简体   繁体   中英

How would I go about creating a list of Textbox items to use in other methods?

I have a series of 5 text boxes on a winforms form which are used to collect data as strings. I intend to use these boxes in multiple ways from a method to clear the contents of each, to a method which takes the data from each to check before exporting to a text file.

In order to do this I planned to create a List<Textbox> which would essentially be a list of all 5 boxes so I could later use code such as

foreach(Texbox box in *texboxList*)
{
    box.Clear()
} 

etc.

My only idea so far is to create a seperate method which adds all of the boxes to a list and then somehow pass the result of the method as a parameter to the relevant methods such as those that clear the boxes. The code I currently have is displayed below.

public List<TextBox> Clear_entered_data()
{
    List<TextBox> textBoxes = new List<TextBox>();
    textBoxes.Add(tbox1);
    textBoxes.Add(tbox2);
    textBoxes.Add(tbox3);
    textBoxes.Add(tbox4);
    textBoxes.Add(tbox5);
    return textBoxes;
}

This is the code I'm using to generate the list of textboxes to use. I think the problem I'm having is understanding how this can be passed to other methods through parameters. The method I'd like to make use of the list is shown below here as I have it so far.

private void Clear_button_Click(object sender, EventArgs e, List<TextBox> textBoxes)
  {

    DialogResult Clear_validation = MessageBox.Show("Are you sure you would like to clear all data from the form?","Clear data?", MessageBoxButtons.YesNo);

    if(Clear_validation == DialogResult.Yes)
    {
        foreach (TextBox box in textBoxes)
        {
            box.Clear();
        }
    }
}

With the code above I get the error upon running :

'Error 1 No overload for 'Clear_button_Click' matches delegate 'System.EventHandler''

But I've had no luck, please try and explain whats going wrong and help me find more appropriate solution!

Thanks

create a private class member that of list of textboxes List<TextBox> _textBoxes;

then after you call InitializeComponents(because before that your textboxex doesn't exists if you used the IDE to create them) place your code to add the textboxes into the list

 _textBoxes = new List<TextBox>();
 _textBoxes.Add(tbox1);
 _textBoxes.Add(tbox2);
 _textBoxes.Add(tbox3);
 _textBoxes.Add(tbox4);
 _textBoxes.Add(tbox5);

now you can use in your methods the _textBoxes that contains all your created TextBox

your public List Clear_entered_data() became

public List<TextBox> ClearTextBoxes() {
    foreach(var textBox in _textBoxes){
       textBox.Clear();
    }
}

If you used a naming convention, you could just iterate over the form's control collection like this - in my example, the names all start with "SpecialTextBox":

foreach(Control c in someFormReference.Controls)
{
    if (c.GetType() == typeof(TextBox) && c.Name.StartsWith("SpecialTextBox"))
    {
        // do your thing here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM