将名称存储在数组中并按字母顺序显示

本文关键字:顺序 显示 存储 数组 | 更新日期: 2023-09-27 18:13:25

为什么richTextBox1.Text在运行代码时不显示名称?我需要按字母顺序显示这些名字。这段代码不能工作是因为Array.Sort(nameArray);还是名称本身没有被存储?

string[] nameArray = new string[5];
private void button6_Click(object sender, EventArgs e)
    {
        textBoxName1.Text = nameArray[0];
        textBoxName2.Text = nameArray[1];
        textBoxName3.Text = nameArray[2];
        textBoxName4.Text = nameArray[3];
        textBoxName5.Text = nameArray[4];   
    }
    private void button9_Click(object sender, EventArgs e)
    {
        Array.Sort(nameArray);
        foreach(string s in nameArray)
        {
            richTextBox1.Text += s + " ";
        }
    }

将名称存储在数组中并按字母顺序显示

我想这对你有帮助

string[] nameArray = new string[5];
void CopyTextBoxesToArray()
{
    nameArray[0] = textBoxName1.Text;
    nameArray[1] = textBoxName2.Text;
    nameArray[2] = textBoxName3.Text;
    nameArray[3] = textBoxName4.Text;
    nameArray[4] = textBoxName5.Text;
}
private void button9_Click(object sender, EventArgs e)
{
    CopyTextBoxesToArray();
    Array.Sort(nameArray);
    foreach(string s in nameArray)
    {
        richTextBox1.Text += s + " ";
    }
}

你的数组没有任何值尝试通过添加值到你的字符串数组像下面

string[] nameArray = new string[5]
    {
        "Egyptian",
        "Indian",
        "American",
        "Chinese",
        "Filipino",
    };

如果你想从文本框中获取数组值你必须将文本框值赋给Arrau

private void button6_Click(object sender, EventArgs e)
    {
        nameArray[0] =textBoxName1.Text ;  
    }