HOW TO GET INDEX VALUE IN COMBO BOX?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1200  View(s)
Ratings:
Rate this:

I have a ComboBox with two values 'cash' and 'bank' if the user selects bank I need to enable two controls. I've tried below code:

private void comboBox_pay_type_SelectedIndexChanged(object sender, EventArgs e)

    {

        if (comboBox_bank.SelectedIndex == 1)

        {

            comboBox_bank.Enabled = true;

            textBox_recp_no.Enabled = true;

        }

    }

Problem is the index value is -1 for both cash and bank. Where have I made an error?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to get index value in combo box?

    Hi Ankita,


    You're just referencing the wrong combo box:

    private void comboBox_pay_type_SelectedIndexChanged(object sender, EventArgs e)

    {

        if (comboBox_pay.SelectedIndex == 1)

        {

            comboBox_bank.Enabled = true;

            textBox_recp_no.Enabled = true;

        }

    }

    You need to reference comboBox_pay here.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!