从文本框到字符串数组的C#文本

本文关键字:文本 数组 字符串 | 更新日期: 2023-09-27 18:01:07

我想通过按下按钮3将文本从文本框添加到string[]数组,I值将添加到新数组并清除文本框。

Button1表示在阵列中向上,Button2表示向下。

我做了这个,但它不起作用:

namespace Test
{
    public partial class Form1 : Form
    {
        public int arr1 = 0;
        public int arr2 = 0;
        public string[] array = new string[100];
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (arr1 < array.Length - 1)
            {
                if (array[arr1] != "")
                {
                    arr1++;
                    textBox1.Text = array[arr1];
                }
            }
            else
            {
                arr1 = 0;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (arr1 < array.Length - 1)
            {
                if (array[arr1] != "")
                {
                    arr1--;
                    textBox1.Text = array[arr1];
                }
            }
            else
            {
                arr1 = 0;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            array[arr1] = textBox1.Text;
            listBox1.Items.Add(array[arr1]);
            arr2++;
            textBox1.Text = "";
        }
    }
}

有人能帮我吗?感谢

从文本框到字符串数组的C#文本

arr1++替换arr2++,如下

private void button3_Click(object sender, EventArgs e)
    {
        array[arr1] = textBox1.Text;
        listBox1.Items.Add(array[arr1]);
        arr1++;
        textBox1.Text = "";
    }