Get text of Selected item in C# ComboBox

Viewed 19

I have a comboBox with a ValueMember and a DisplayMember. This is how I am binding the data.

 cbActiveMainEvents.ValueMember = "MainEventID";
 cbActiveMainEvents.DisplayMember = "MainEvents";
 cbActiveMainEvents.DataSource = ActiveMainEvents;

When we load for the first time is there is more than 1 item we display "Select an Event" So now when I click the dropdown and select an item, I am able to grab the correct ValueMember, but I still need the text of the newly selected item.

private void cbActiveMainEvents_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Program.ComboActiveMainSelectedIndex = cbActiveMainEvents.SelectedIndex;
            string pattern = @"\d+";
            Regex rg = new Regex(pattern);
            MatchCollection mainEventNumber = rg.Matches(cbActiveMainEvents.Text);
            MainEvent.Text = (mainEventNumber[0].Value);
            txtMainEventID.Text = cbActiveMainEvents.SelectedValue.ToString();
        }

I have tried .Text, .SelectedText but no matter what I am always getting the previous text, not the new one. I also tried changing the DropDownStyle to DropDownList. But when I do that, it always displays the first entry even though it has more than one item. So it's not displaying "Select an Event"

I have also tried

MatchCollection mainEventNumber = rg.Matches(cbActiveMainEvents.SelectedItem.ToString());

but the string it returns is "UpLoadImages.Models.MainEvent"

0 Answers
Related