【发布时间】:2011-10-10 20:14:43
【问题描述】:
我有字符串列表框。当我选择其中一个字符串时,我将其拆分。
我想将这个字符串的拆分值发送到textboxes。如何将值发送到文本框?
我有这个 C# 代码:
private void button8_Click(object sender, EventArgs e)
{
string Code;
string Name;
string PName;
string Cost;
string Num;
string Level;
using (var streamReader = new StreamReader(filePath, Encoding.Default))
{
if (!streamReader.EndOfStream)
{
Items.Add(streamReader.ReadLine());//list Items
}
}
string z = listBox1.SelectedItem.ToString();
string[] words = x.Split(',');
foreach (string word in words)
{
if (words.Length == 6)
{
Code = words[0];
Name = words[1];
PName = words[2];
Cost = words[3];
Num = words[4];
Level = words[5];
}
}
textBox1.Text = Code; //This does not send anything to the textbox
textBox2.Text = Name;
textBox3.Text = PName;
textBox4.Text = Cost;
textBox5.Text = Num;
textBox6.Text = Level;
using (var streamWriter = new StreamWriter(
filePath, false, Encoding.Default))
{
foreach (string op in Items)
{
streamWriter.WriteLine(op);
}
}
}
textBox1.Text = Code; 的 C# 代码不会向文本框发送任何文本,如何将字符串分配给文本框?
【问题讨论】:
-
你应该为你的控件命名。
-
您得到的确切错误是什么?看起来如果您的拆分失败,您将输出默认值,但 String.Empty 对于 TextBox.Text 应该没问题...
-
应该... string[] words = x.Split(',');是... string[] words = z.Split(','); ?
-
foreach 的目的是什么,这是多余的,因为 foreach 的内部引用了 words 数组
-
错误是什么?您是否使用 Visual Studio 进入过代码?