If someone can help me I will appreciate it. Thanks in advance, I have a combobox with the values databinding inside a database, I want when I move the records next/previous base on what combobox value is a picturebox display a picture I have stored in my resources.
When I change with mouse combobox value the picture change but when I move between records not. What am I missing there?
Private Sub TypeComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TypeComboBox.SelectedIndexChanged
If TypeComboBox.SelectedItem().ToString() = "1" Then
PictureBox1.Image = My.Resources.image1
End if
If TypeComboBox.SelectedItem().ToString() = "2" Then
PictureBox1.Image = My.Resources.image2
End if
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'detect left arrow key
If keyData = Keys.Left Then
If Me.ToolsBindingSource.Position = 0 Then
Me.ToolsBindingSource.MoveLast()
Return True
Else
Me.ToolsBindingSource.MovePrevious()
Return True
End If
End If
'detect right arrow key
If keyData = Keys.Right Then
If Me.ToolsBindingSource.Position = Me.ToolsBindingSource.Count - 1 Then
Me.ToolsBindingSource.MoveFirst()
Return True
Else
Me.ToolsBindingSource.MoveNext()
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I would suggest that you create an array containing the Images
at the start. You should not repeatedly access the same property of My.Resources
because it will create a new object each time. E.g.
Private images As Image() = {My.Resources.image1, My.Resources.image2}
You can then simply use the SelectedIndex
of the ComboBox
as an index into that array. As long as the two lists are in the same order, the actual values in the ComboBox
are irrelevant. E.g.
PictureBox1.Image = images(TypeComboBox.SelectedIndex)
EDIT: Another option would be to create a list that relates the Images
and the text, bind that to the ComboBox
and then use the SelectedValue
, e.g.
Dim imagesAndText = {Tuple.Create("1", My.Resources.image1),
Tuple.Create("2", My.Resources.image2)}
With TypeComboBox
.DisplayMember = "Item1"
.ValueMember = "Item2"
.DataSource = imagesAndText
End With
By passing a String
and an Image
to Tuple.Create
, you get a Tuple(Of String, Image)
. Tuples have properties of the types specified and named Item1
, Item2
, etc. In this case, Item1
would return the String
value and Item2
the Image
. After binding, you can access the selected Image
via the SelectedValue
property, e.g.
PictureBox1.Image = DirectCast(TypeComboBox.SelectedValue, Image)