2015-04-12 53 views
0

代码: -使文本框的列表

public partial class Players : Form 
{ 
    public TextBox[] spelers = new TextBox[7]; 
    List<string> spelersSpel = new List<string>(); 
    public Form1 game = new Form1(); 
    public Players() 
    { 
     InitializeComponent(); 
     spelers[0] = inputSpeler1; 
     spelers[1] = inputSpeler2; 
     spelers[2] = inputSpeler3; 
     spelers[3] = inputSpeler4; 
     spelers[4] = inputSpeler5; 
     spelers[5] = inputSpeler6; 
    } 

    private void btnSpelers_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < spelers.Length; i++) 
     { 
      if (spelers[i] != null) 
       spelersSpel.Add(spelers[i].Text); 

     } 

     foreach (TextBox item in spelers) 
     { 
      if (item != null) 
       spelersSpel.Add(item.Text); 
     } 
     MessageBox.Show(spelersSpel.Count.ToString()); 
     game.ShowDialog(); 
    } 
} 

我有6个文本框,我已经投入到一个数组。然后我想把这个数组放到一个表单中。因为我不知道从文本框得到多少输入,所以我不使用数组。但是当我想看看我的名单有多大。这是说6.如果我只在第一个文本框中键入文本,列表应该是1.

我在做什么错了?

+0

为什么使用数组?只需创建一个列表并使用foreach循环或LINQ语句迭代它即可 –

回答

0

您正在将TextBox对象与其Text属性混淆。
如果我理解正确的话,你希望List<string>到只包含非空的文本框的文本,所以你的代码应该是这样的

spelersSpel.Clear(); 
    foreach (TextBox item in spelers) 
    { 
     if (item != null && !string.IsNullOrWhiteSpace(item.Text)) 
      spelersSpel.Add(item.Text); 
    } 

请注意,您声明数组包含7个文本框(指数从0到6),但是你只添加了6个文本框,所以你总是需要检查NULL,因为当你遍历你的元素时,你的数组在索引6处有一个NULL条目。所以,如果你有只有6文本框最好是阵列与

public TextBox[] spelers = new TextBox[6]; 

接下来声明,在点击事件,你执行两个循环来填充列表,这当然会复制列表内容。

但是,我认为,更好的办法是直接使用List<TextBox>,与它合作,摆脱阵列和List<string>

public partial class Players : Form 
{ 
    public List<TextBox> spelers; 
    public Form1 game = new Form1(); 

    public Players() 
    { 
     InitializeComponent(); 
     spelers = new List<TextBox>() 
     { 
      inputSpeler1, inputSpeler2, inputSpeler3, 
      inputSpeler4, inputSpeler5, inputSpeler6 
     } 
    } 

    private void btnSpelers_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show(spelers 
        .Count(x => !string.IsNullOrWhiteSpace(x.Text)) 
        .ToString()); 
     game.ShowDialog(); 
    } 
} 
0

因为(spelers [I]!= NULL)和( item!= null)不检查文本框是否为空;这意味着数组元素为空或不是。

如果你想检查你的文本框是否为空,你应该找到属性Text;

if (item.Text != "") 

我无法使用我的IDE,但它不确定,但试试这个。

public partial class Players : Form 
{ 
    public TextBox[] spelers = new TextBox[6]; // [7]; 
    List<string> spelersSpel = new List<string>(); 
    public Form1 game = new Form1(); 
    public Players() 
    { 
     InitializeComponent(); 
     spelers[0] = inputSpeler1; 
     spelers[1] = inputSpeler2; 
     spelers[2] = inputSpeler3; 
     spelers[3] = inputSpeler4; 
     spelers[4] = inputSpeler5; 
     spelers[5] = inputSpeler6; 
    } 
    private void btnSpelers_Click(object sender, EventArgs e) 
    { 
     /* 
     // 1. 
     for (int i = 0; i < spelers.Length; i++) 
     { 
      if (spelers[i] != null) { 
       throw new Exception("NullTextBoxException"); 
      } 
      else if (spelers[i].Text == "") { 
       // skip when the textbox is empty 
       continue; 
      } 
      spelersSpel.Add(spelers[i].Text); 
     } 
     */ 

     foreach (TextBox item in spelers) 
     { 
      if (item != null) { 
       // throw exception when referring textbox is null 
       throw new Exception("NullTextBoxException"); 
      } 
      else if (item.Text == "") { 
       // skip when the textbox is empty 
       continue; 
      } 
      spelersSpel.Add(item.Text); 
     } 

     MessageBox.Show(spelersSpel.Count.ToString()); 
     game.ShowDialog(); 
    } 
} 
+0

感谢您的回答! – Jenssen

0

你可以通过这种方式输入计数:

spelersSpel.Clear(); 
foreach (TextBox item in spelers) 
{ 
    if (item != null && item.Text.Trim() != "") //check if null or empty 
    spelersSpel.Add(item.Text); 
}