日期:2014-05-18 浏览次数:21088 次
private void Form1_Load(object sender, EventArgs e) { TextBox textBox1 = new TextBox(); textBox1.Name = "X1"; TextBox textBox2 = new TextBox(); textBox2.Name = "X2"; TextBox textBox3 = new TextBox(); textBox3.Name = "X3"; TextBox textBox4 = new TextBox(); textBox4.Name = "X4"; List<TextBox> list = new List<TextBox> { textBox1, textBox2, textBox3, textBox4 }; int x = list.FindIndex(GetTextBox); MessageBox.Show(x.ToString()); } private static bool GetTextBox(TextBox s) { if (s.Name == "X2") { return true; } else { return false; } }
------解决方案--------------------
List<TextBox> list = new List<TextBox>{textBox1、textBox2、textBox3、textBox4}
int index = list.IndexOf(textBox3,0,0);
------解决方案--------------------
IndexOf貌似是有问题的
list.IndexOf(textBox3,0,0)返回-1
写成
list.IndexOf(textBox3)
即可