制作空文本框

Making empty textbox

当我要清除以下数组时:

userInfo[0] = txtPassword.Text;
userInfo[1] = txtVoornaam.Text;
userInfo[2] = txtAchternaam.Text;
userInfo[3] = txtWoonplaats.Text;
userInfo[4] = txtPostcode.Text;
userInfo[5] = txtTelnr.Text;

我正在执行以下操作:

for (int i = 0; i < 5; i++)
{
  userInfo[i] = "";
}

这显然行不通,当我尝试这样做时:

foreach (string item in userInfo)
{
  item = "";
}

也不行。我怎么能不说就做到这一点:

userinfo[0] = txtPassword; 

如果你想像对待数组一样对待它们,你需要将它们添加到一个数组中,或者更确切地说 much 更好 List<TextBox>。做一次一次,你可以循环任何或你的需要..

做一次..:[=​​19=]

List<TextBox> boxes = new List<TextBox>()
           { txtPassword, txtVoornaam, txtAchternaam, txtWoonplaats, txtPostcode, txtTelnr};

现在您可以将值提取到字符串数组中

string[] myTexts = boxes.Select(x => x.Text).ToArray();

..或列表:

List<string> myTexts = boxes.Select(x => x.Text).ToList();

或者您可以全部清除它们:

foreach (TextBox tb in boxes) tb.Text = "";

如果需要,您可以通过变量访问 List 元素:

boxes.Find(x => x == textBox2).Text = "12345";

..或 Name

boxes.Find(x => x.Name == "txtVoornaam").Text = "Jens";

(省略所有检查..)