Mohammed Turki
Mohammed Turki

Reputation: 17

Updating a Listbox Automatically

I have 2 listboxes. The first one is enabled and the second one is disabled. The second listbox will get its data from the first one.

For example, let's say I entered 2 in listbox1. listbox2 will get 2 + 1 = 3
Thing is, I want to update the second listbox automatically without clicking any button.
Is that even possible?

I wrote the following code in a button:

    private void btnResult_Click(object sender, EventArgs e)
    {
        int WantedYear = Convert.ToInt32(txtForecastingYear.Text);

        int FirstYearValue = WantedYear - 4;  
        txtFirstYear.Text = FirstYearValue.ToString();

        int SecondYearValue = WantedYear - 3; 
        txtSecondYear.Text = SecondYearValue.ToString();

        int ThirdYearValue = WantedYear - 2; 
        txtThirdYear.Text = ThirdYearValue.ToString();

        int FourthYearValue = WantedYear - 1;
        txtFourthYear.Text = FourthYearValue.ToString();
    }

I want to do it without having to use a button to update. How can I do that?
Note: I'm using Windows Forms.

Upvotes: 0

Views: 127

Answers (1)

Tanuj Wadhwa
Tanuj Wadhwa

Reputation: 2045

You can write your code in the SelectedIndexChanged event of the listbox. This event would be automatically fired as soon as the IndexChanges.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

You can read more about it here.

Upvotes: 1

Related Questions