2013-02-12 58 views
0

所以,我在此数组由一个添加元素一个按下一个按钮,并把它添加元素与此: arreglo.Add(textBox1.Text.ToString());禁用的条件下在C#中的按钮

我只是想限制量它可以添加到数组中的元素为10个。它最多可以有10个元素,不会再有。我怎么做?

如果有帮助,这些都是我的代码部分,我认为它可以帮助:

ArrayList arreglo; 
    public Form1() 
    { 
     InitializeComponent(); 
     arreglo = new ArrayList(); 
    } 

 private void button5_Click(object sender, EventArgs e) 
    { 
     //Agregar 
     arreglo.Add(textBox1.Text.ToString()); 
     /*if (arreglo.Count > 10) 
     { 
      listBox1.Items.Add("No more than ten elements"); 
     }*/ 
     this.textBox1.Clear(); 
     this.textBox1.Focus(); 
    } 

而且顺便说一句,我也需要做一些计算与数组,但我已经有了。

+0

你想禁用一个按钮或限制数组长度? – mihirj 2013-02-12 08:08:17

+0

@mihirj一旦数组的极限被击中,我想禁用一个按钮。 – user2058292 2013-02-12 08:09:16

+0

请看下面的答案适用于你的情况 – mihirj 2013-02-12 08:12:37

回答

1

可以简单地解决这个为:

private void button5_Click(object sender, EventArgs e) 
    { 
     //Agregar 
     arreglo.Add(textBox1.Text.ToString()); 
     if (arreglo.Count > 10) 
     { 
      button5.Enabled = false; 
     } 
     this.textBox1.Clear(); 
     this.textBox1.Focus(); 
    } 
+0

谢谢你的帮助:D – user2058292 2013-02-12 08:14:50

+0

如果这解决了你的问题,你可以把这个标记为答案吗? – mihirj 2013-02-12 08:15:56

+0

其实,我用这两个问题作为答案。但是,这对我最有帮助,所以这将是。 – user2058292 2013-02-12 08:22:34

0

只需在添加阵列中有多少元素后进行检查,并将Button启用属性设置为false - 它将禁用按钮。

private void button5_Click(object sender, EventArgs e) 
{ 
    arreglo.Add(textBox1.Text.ToString()); 
    this.textBox1.Focus();  
    this.textBox1.Clear(); 

    if (arreglo.Count >= 10) 
    { 
     button5.Enabled = false; 
    } 
} 
+0

谢谢你的时间,但我已经有了答案。谢谢! – user2058292 2013-02-12 08:27:55

1

这里是改变阵列容量,同时的方式,在这种情况下,阵列能够从0到9(10种元素)

class Program 
{ 
    static void Main(string[] args) 
    { 
     ArrayList list = new ArrayList(); 
     for(int i = 1; i < 20; i++) 
     { 
      try 
      { 
       list.Capacity = 9; 
      } 
      catch (Exception) 
      { button5.Enabled = false; } 
      list.Add("teststring"); 
     } 
     list = list; 
    } 
} 
+0

呃,这很好!我也喜欢这个答案。谢谢! – user2058292 2013-02-12 08:23:34

0

为什么ArrayList?你的目的是什么?

private void button10_Click(object sender, EventArgs e) 
    { 
     if (arreglo.Count < 10) 
     { 
      arreglo.Add(textBox1.Text); 
      this.textBox1.Clear(); 
      this.textBox1.Focus(); 
     } 
     else 
      button10.Enable = false; 
    } 
+0

感谢您的时间,但我已涵盖。再次感谢! – user2058292 2013-02-12 08:27:30

+0

欢迎,你可以__upvote__答案,如果你认为这对你有用,你可以接受为__answer__如果这个答案是最适合你的......欢迎来到stackoveflow':D' – spajce 2013-02-12 08:30:36