[英]C#, send a value to a textbox
我有字符串列表框。 当我选择其中一个字符串时,我将其拆分。
我想将此字符串的拆分值发送到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#代码textBox1.Text = Code;
没有将任何文本发送到文本框,如何为文本框分配字符串?
如果words.Length == 6
则变量Code
仅包含一个值。 确保变量包含一个值。
使用它,查看文本是否保存到文本框中:
textBox1.Text = "test";
将您的Code
变量分配给TextBox
时,它仍然为null。
将其更改为:
string Code = string.Empty;
// etc.
但是,根据示例代码,您不需要任何这些字符串变量或ForEach。 只需将其直接分配给您的TextBoxes。
textBox1.Text = words[0];
textBox2.Text = words[1];
textBox3.Text = words[2];
textBox4.Text = words[3];
textBox5.Text = words[4];
textBox6.Text = words[5];
并尝试给控件命名。 textBox4不会告诉您它与成本有关。
尝试使用像直接分配它这样的语句,例如textBox1.Text = Code.ToString(); 或textBox1.Text = words [0] .ToString(); 也许对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.