Hello!

Sorry for the bad article title, was not sure what to name it since I couldn't figure out anything good to name it, that would be near my request.

Let me begin..

I code with Visual Studio 2010 with C# Windows Form Application.

I code everything in combobox1 and have no idea how to ingage another combobox, let me try to explain..

This is my following code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string textfileopen = "New Folder\\testfile.txt";
    try
    {   
        StreamReader DataIn = File.OpenText(textfileopen); // Opening testfile.txt
        string DataStr;
        int count = 0;

        double result0; 
        double result1;
        double CalculateResult;
        double combobox2Number;

        string[] result; //Give each split a value of result[x]
        char[] separ = new char[] { '"' }; // split words in text file
        while ((DataStr = DataIn.ReadLine()) != null)
        {
            count++;
            result = DataStr.Split(separ);

            textBox1.Text = result[0]; //write out result to textbox
            textBox2.Text = result[1];
            textBox3.Text = result[2];

            double.TryParse(result[0], NumberStyles.Any, NumberFormatInfo.CurrentInfo, out result0); //Converts string to double
            double.TryParse(result[1], NumberStyles.Any, NumberFormatInfo.CurrentInfo, out result1); //Converts string to double
            double.TryParse(comboBox2.SelectedItem.ToString(), NumberStyles.Any, NumberFormatInfo.CurrentInfo, out combobox2Number); //Converts selected number (string) in combobox2 to double

            CalculateResult = result0 + combobox2Number * result1;

            textBox4.Text = CalculateResult.ToString();
        }
    }
    catch (InvalidCastException ex)
    {
        MessageBox.Show("error:" + ex.Message);
    }
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}

Now in combobox2 I got number 1 to 100 listed.

Even thought I select a new number in combobox2, it wont recalculate since I did no change in combobox1..

How can I make it re-calculate when I change in combobox2?

Is there a way to do that at all?

Please ask any questions and I'll try to answer them.

Recommended Answers

All 5 Replies

You can set SelectedIndex, if that's what you're asking.

Thanks for your answer, but does not matter if it is selectedindex or selecteditem since it does not re-calculate because I don't choose anything new in combobox1..

I guess what I am asking is that..

When I change combobox2 I would want:

double.TryParse(comboBox2.SelectedItem.ToString(), NumberStyles.Any, NumberFormatInfo.CurrentInfo, out combobox2Number);

to reload to the new selected number, but that can't happen since the code above is written in combobox1.

If comboBox2 is tied to comboBox1 without binding, you'd need to programmatically select the new value in comboBox1 when comboBox2 changes.

hmm I think it is the binding I Want to do, but have no clue on how to do it, could you help me with that? Want to try that out.

Oh and, combobox1 got different data listed

combobox1 got 3 different words
combobox2 got numbers 1 to 100

if that changes anything

Figured it out, not sure if this was what you meant..

The code above for reading a file, I wrote it in combobox2 which in combobox1 I got

comboBox2.SelectedIndex = comboBox2.Items.IndexOf("1");

which makes combobox2 reload when selecting something in combobox1 and read the file and whatnot..

You gave me the idea to try this out and it worked haha. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.