在一定条件下禁用c#中的按钮

本文关键字:按钮 条件下 | 更新日期: 2023-09-27 17:58:42

所以,我有一个数组,它通过按下按钮一个接一个地添加元素,它添加的元素如下:arreglo.Add(textBox1.Text.ToString());

我只想将它可以添加到数组中的元素数量限制为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();
    }

顺便说一句,我还需要用这个数组做一些计算,但我已经涵盖了。

在一定条件下禁用c#中的按钮

您可以简单地将其解决为:

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到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;
    }
}

只需在添加数组的元素数量后进行检查,并将Button enabled属性设置为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;
   }
}

为什么选择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;
    }