且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#中使用inputBox填充字符串数组

更新时间:2023-02-21 09:22:59

你可以使用string.Split()。

http://msdn.microsoft.com/en-us/library/system .string.split.aspx [ ^ ]



祝你好运!


如果您想要添加 InputBox 到数组,然后代替 foreach 循环,你可以使用代替 loop。

  for  int  i =  0 ; i <  Names.Length; i ++)
{
string prompt = Interaction.InputBox( 请输入一个string 输入字符串);
姓名[i] =提示;

// 此处循环中的其他代码
}


private void btnButton_Click(object sender, EventArgs e)
        {
            int array;

            string value = Interaction.InputBox("Enter array size", "Array Size");
            if (int.TryParse(value, out array))
            {
                string[] Names = new string[array];

                txtOutput.Text = "Unsorted array: \r\n";

                foreach (string name in Names)
                {
                    string prompt = Interaction.InputBox("Please enter a string" , "Enter Strings");
                    if (prompt == "")
                    {
                        txtOutput.Text += names +"\t";
                    }
                }

You can use string.Split() for that.
http://msdn.microsoft.com/en-us/library/system.string.split.aspx[^]

Good luck!


If you want to add the values from the InputBox to the array, then instead of a foreach loop, you can use a for loop.
for (int i = 0; i < Names.Length; i++)
{
    string prompt = Interaction.InputBox("Please enter a string" , "Enter Strings");
    Names[i] = prompt;
    
    // your other code in the loop here
}