sau*_*auk 0 .net c# arrays textbox
有没有办法在用逗号分隔的文本框中显示字符串数组项.我无法做到正确,经历了大量的试验和错误.
任何帮助或建议,高度赞赏.
private void button9_Click(object sender, EventArgs e)
{
int lineNum = 1;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.Title = "Select a Text file";
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
string[] text = System.IO.File.ReadAllLines(file);
foreach (string line in text)
{
if (lineNum <= 30)
{
textBox1.Text = Convert.ToString( text);
}
else
{
textBox2.Text = Convert.ToString(text);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何建议请
当然,只需使用 String.Join
textBox1.Text = string.Join(",", text);
Run Code Online (Sandbox Code Playgroud)
如果要NewLine 在每个逗号使用后追加:
textBox1.Text = string.Join("," + Environment.NewLine, text);
Run Code Online (Sandbox Code Playgroud)
您也不需要使用该foreach循环.
编辑:根据您的评论,您可以使用以下内容:
textBox1.Text = string.Join("," + Environment.NewLine, text.Take(30));
if(text.Length > 30)
textBox2.Text = string.Join("," + Environment.NewLine, text.Skip(30));
Run Code Online (Sandbox Code Playgroud)
注意:为了使用LINQ方法(例如,Take和Skip),您需要System.Linq在项目中包含名称空间.
using System.Linq;
Run Code Online (Sandbox Code Playgroud)