I have listbox of strings. When I select one of this strings, I split it.
I want to send to the textboxes the split values of this string. How do I send the values to the textboxes?
I have this C# code:
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);
}
}
}
The C# code that does textBox1.Text = Code; does not send any text to the textbox, how do I assign a string to a textbox?
Your
Codevariable is still null when you are assigning it to theTextBox.Change it to:
Based on your sample code though, you shouldn’t need any of those string variables or your ForEach. Just assign it straight to your TextBoxes.
And try giving your controls names, too. textBox4 doesn’t tell you it has anything to do with cost.