-1

I am confronting with an issue with the ComboBox in a WinForms app. The combo box contains a list with items that are grabbed from a TXT file. To read and update the list I added the following code to Form_Load.

string[] lineOfContents = File.ReadAllLines(Application.StartupPath + @"Items.txt");
        foreach(var line in lineOfContents)
        {
            string[] tokens = line.Split(',');
            ComboBox.Items.Add(tokens[0]);
        }

All nice and good, the list does update. However, I also have some TextBoxes that are changing their text based on the string of the selected item in the ComboBox. For example, when the selected item in the ComboBox is "Example", the text in the first text box should change from empty to "I am an example!", but it doesn't and remains empty. The code for it is:

if(ComboBox.SelectedItem == "Example")
        {
            TextBox.Text = "I am an example!";
         }

At first I though it's a conversion issue so I tried to use Convert.ToString(tokens[0]); but it did not work as well.

Any suggestions on what I should try next to fix this issue?

1 Answers1

0

You are describing bound behavior so the first thing to check is whether the TextBox is properly connected to the event fired by the ComboBox. Something like this would be the first step to getting the behavior you describe.

public MainForm()
{
    InitializeComponent();
    // Subscribe to the event here OR using the Designer
    comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.Text == "Example")
    {
        textBox1.Text = "I am an example!";
    }
    else textBox1.Text = comboBox1.Text;
}

But the missing step in the code you posted is actually selecting a value in the combo box after the tokens are loaded into it. This leaves the text box blank as demonstrated here:

private void buttonLoad_Click(object sender, EventArgs e)
{
    loadMockFile();
}

private void loadMockFile()
{
    string[] lineOfContents = MockFile;
    foreach (var line in lineOfContents)
    {
        var token = 
            line.Split(',')
            .Select(_ => _.Trim())
            .Distinct()
            .First();
        if (!comboBox1.Items.Contains(token))
        {
            comboBox1.Items.Add(token);
        }
    }
}

string[] MockFile = new string []
{
    "Example, A, B,",
    "Other, C, D,",
};

selection-needed


So the solution is to make sure that you make a selection after the tokens are loaded into the ComboBox, for example as shown in the handler for the [Load + Select] button:

private void buttonLoadPlusSelect_Click(object sender, EventArgs e)
{
    loadMockFile();
    var foundIndex = comboBox1.FindStringExact("Example");
    comboBox1.SelectedIndex = foundIndex;
}

after load and select

IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • 1
    Thanks you very much! Detailed overview and various solutions on how to fix it. I used the comboBox.text instead of selectedItem and it appears that this fixed it. – Str1ng1737 Aug 12 '22 at 08:44