[英]How can I shorten this repetitive C# TextBox.Clear code?
Been reading through the threads a lot recently as I am learning to code! 在我学习编码的同时,最近也正在仔细阅读所有线程!
In essence all I know is self taught and very basic, so I would like to start becoming more professional 从本质上讲,我所知道的是自学且非常基础的,所以我想开始变得更加专业
I would like to shorten this code down (in the simplest way possible) since I often end up with very repetitive code! 我想缩短这段代码(以最简单的方式),因为我经常会得到非常重复的代码! Here is a primary example 这是一个主要的例子
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}
For context I can't clear all textboxes on my form since textBox13
always has a useful value that I can't have deleted! 对于上下文,我无法清除表单上的所有文本框,因为textBox13
始终具有我无法删除的有用值! EDIT * textBoxes 1 to 6 are in groupBox1
, and textBoxes 7-12 are in groupBox2
. 编辑*文本框1到6在groupBox1
,文本框7-12在groupBox2
。 This appears to be significant; 这似乎很重要; I was just using the groupbox tool to make the program clear! 我只是使用groupbox工具来使程序清晰!
If anyone can help I would be truly grateful! 如果有人可以帮助我,我将不胜感激! Please keep in mind I am still a coding novice, so some features I am unfamiliar with. 请记住,我仍然是编码新手,所以有些功能我不熟悉。
Here is an image of the program to help! 这是对程序有帮助的图像!
Assuming all your textboxes name start in the format you displayed, you could use this: 假设所有文本框名称都以您显示的格式开头,则可以使用以下命令:
foreach (Control control in Controls)
{
if (control.Name.StartsWith("textBox") && !control.Name.EndsWith("13") && control.GetType() == typeof(TextBox))
{
TextBox textBox = (TextBox)control;
textBox.Clear();
}
}
If you want it to work with group boxes use this code by calling ClearTextBoxes (just write "ClearTextBoxes();") 如果希望它与组框一起使用,请通过调用ClearTextBoxes使用此代码(只需编写“ ClearTextBoxes();”)。
private void ClearTextBoxes()
{
foreach (Control control in Controls)
{
ClearTextBox(control);
}
}
private void ClearTextBox(Control control)
{
if (control.GetType() == typeof(TextBox))
{
if (control.Name.StartsWith("textBox") && !control.Name.EndsWith("13"))
{
TextBox textBox = (TextBox)control;
textBox.Clear();
}
}
else if (control.GetType() == typeof(GroupBox))
{
GroupBox groupBox = (GroupBox)control;
foreach (Control groupBoxControl in groupBox.Controls)
{
ClearTextBox(groupBoxControl);
}
}
}
The current setting doesn not allow you to clear everything quickly because each variable, even if they're all of the same type and with similar name, is a different entity and you cannot iterate through them. 当前设置不允许您快速清除所有内容,因为每个变量(即使它们都是相同的类型并且具有相似的名称)都是不同的实体,并且您无法遍历它们。
Anyway, you could group them in an array and iterate through them like this: 无论如何,您可以将它们分组为一个数组并像这样遍历它们:
// Fill this array in an Init function
YourType[] textBoxes;
private void button2_Click(object sender, EventArgs e)
{
// Iterate through the whole array except for the last element
for(int i=0; i<textBoxes.Length-1; i++)
{
textBoxes[i].Clear();
}
}
One possible solution would be store textboxes in list 一种可能的解决方案是将文本框存储在列表中
List<TextBox> textBoxes
And then iterate through this collection using for or foreach loop and calling Clear() method. 然后使用for或foreach循环并调用Clear()方法遍历此集合。
Or use extension method ForEach() 或使用扩展方法ForEach()
textBoxes.ForEach(x => x.Clear());
The simplest way is to prepare a collection containing all of your textbox objects 最简单的方法是准备一个包含所有文本框对象的集合
var textBoxes = new List<TextBox> { textBox1,textBox2,textBox3};// fill in the rest
and just iterate with a 'foreach' on it 并在其上进行“ foreach”迭代
textBoxes.ForEach(textBox => textBox.Clear());
This is a simple suggestion. 这是一个简单的建议。 You can also use reflection to make it more implicit but it doesn't worth it 您也可以使用反射使它更加隐含,但不值得
Usually having too many of certain control can be a sign that you can consider a different type of control, but that doesn't seem to be the case based on the screenshot. 通常,某些控件的数量过多可能表明您可以考虑使用其他类型的控件,但是根据屏幕截图来看,情况似乎并非如此。 You can use array: 您可以使用数组:
foreach (var t in new[] { textBox1, textBox2, textBox3 })
t.Clear();
If the controls are directly in the form then something like: 如果控件直接在表单中,则类似于:
foreach (Control c in this.Controls)
if (c is TextBox && c.Name != "textBox13")
c.Text = ""; // .Text = "" is what TextBox.Clear() does
but because the controls are inside other controls: 但是由于这些控件位于其他控件内部:
foreach (Control c in this.groupBox1.Controls) if (c is TextBox) c.Text = "";
foreach (Control c in this.groupBox2.Controls) if (c is TextBox) c.Text = "";
or: 要么:
foreach (var gb in new[] { groupBox1, groupBox2 })
foreach (Control c in gb.Controls)
if (c is TextBox) c.Text = "";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.