简体   繁体   English

我将如何创建文本框项目列表以用于其他方法?

[英]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. 我在winforms表单上有一系列5个文本框,用于将数据收集为字符串。 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 为了做到这一点,我计划创建一个List<Textbox> ,它实际上是所有5个框的列表,因此我以后可以使用诸如

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'' '错误1'Clear_button_Click'没有重载匹配委托'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; 创建一个私有类成员,该私有成员属于文本框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 然后在调用InitializeComponents之后(因为在此之前,如果使用IDE创建文本框,则textboxex不存在),放置代码以将文本框添加到列表中

 _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 现在您可以在方法中使用_textBoxes,其中包含所有已创建的TextBox

your public List Clear_entered_data() became 您的公开列表Clear_entered_data()成为

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": 如果使用命名约定,则可以像这样遍历表单的控件集合-在我的示例中,名称都以“ SpecialTextBox”开头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何创建外部网址监视器? - How would i go about creating a external url monitor? 我将如何创建全局通知类? - how would I go about creating a global notification class? 我将如何创建ReturnAlteredObject扩展方法? - How would I go about creating a ReturnAlteredObject extension method? 如果不使用 .trim 或 .replace 等方法,您将如何从字符串中删除空格? - How would you go about removing whitespace from a string without the use of methods such as .trim or .replace? 我 go 如何将另一个 Class 中的方法添加到我的 WindowsForms Class 中? - How would I go about adding methods from another Class into My WindowsForms Class? 我该如何进行单元测试呢? - How would I go about unit testing this? Unity:我将如何为无尽的亚军游戏创建一个加电系统? [等候接听] - Unity: How would I go about creating a power up system into my endless runner game? [on hold] 我将如何在 EF Core 中创建这种多对多关系? - How would I go about creating this many-to-many relationship in EF Core? 我将如何使用OpenTK C#创建六边形棱镜? - How would I go about creating a hexagonal prism using OpenTK C#? 我将如何使用数据库创建登录表单以进行检查? - How would I go about creating a login form using a database for a check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM