Retrieving SelectedValue from ComboBox programmatically?

Ok,

Here is a strange request but I am not really sure how to do this. I have a ComboBox (CB) that has two items in it. It's a DataBound CB so it has ValueMember/DisplayMember properties set. There is a default value selected. When certain processing is done within the app, the SelectedValue needs to change to the other Item in the CB without the user manually selecting it. Is there a way of Looping through the CB's Items?

Thanks!
BlakeMcKennaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
you can loop through the items property
Éric MoreauSenior .Net ConsultantCommented:
Do you have the value of the other ValueMember? In that case, you can just set the SelectedValue property:

Public Class Form1

	Private mdt As DataTable

	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
		FillCombo()
	End Sub

	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
		ComboBox1.SelectedValue = 3
	End Sub

	Private Sub FillCombo()
		mdt = New DataTable("test")
		With mdt
			.Columns.Add("ID", GetType(Integer))
			.Columns.Add("Description", GetType(String))

			For i As Integer = 1 To 5
				.Rows.Add(New Object() {i, "Row " + i.ToString()})
			Next
		End With

		ComboBox1.DataSource = mdt
		ComboBox1.ValueMember = "ID"
		ComboBox1.DisplayMember = "Description"
	End Sub
End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
I do have the other ValueMember, however, I want to keep it as dynamic as possible. I'd rather not hard-code anything because the ValueMember values come from a Database.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Éric MoreauSenior .Net ConsultantCommented:
I don't mean to hardcode. You can surely retrieve the second value and use it to set the selectedvalue property. You could also set the selectedindex property which is the position
BlakeMcKennaAuthor Commented:
Eric,

I found a solution. I actually add the ValueMember (the primary key) to an array variable when the CB was first loaded. At the point when I need to change the value, I simply looped thru the 2 element array and chose the other value. It's rude and crude but it works.

Thanks for your posts!
BlakeMcKennaAuthor Commented:
Thanks again for your input!
Éric MoreauSenior .Net ConsultantCommented:
Is the combobox the ideal solution if you have only 2 values? wouldn't 2 option buttons be better?
BlakeMcKennaAuthor Commented:
Using Option Buttons would make sense with the way things are now, however, since it is database driven, it is conceivably possible for there to be more than two values. If that were the case, then I would need to create these controls dynamically on the form and unfortunately the real estate is pretty tight.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.