I'm pretty sure that it must work but I don't get it working.
So maybe you can help me.
I got a Combobox with some items (item1-item7) to select and 54ish Buttons(sButton1-sButton54). Also an array(53) called Buttons. The array is filled with the items to select, like: Buttons(0) = "item 1, item 2"
Everytime another item in the Combobox is selected, I'd like only some of the Buttons to change their Image. Therefore I got the array. If Item 1 is selected and Buttons(0) contains the item1 I'd like to change the Button1's image.
edit: It works with changing the image of every Button:
sButton1.Image = My.Resources.image1
But I'd prefer to change all in one go (Loop) instead.
sButton(0) = "item1, item2, item3"
sButton(1) = "item2, item3"
sButton(2) = "item1, item3"
...
Select Case ComboBox.SelectedItem
Case "item1"
For i = 0 To 53
If sButton(i).Contains("item1") Then
'sButton1.Image = My.Resources.image1
开发者_JAVA技巧 Me.Controls("sButton" & ((i + 1).ToString)).Enabled = True
Me.Controls("sButton" & ((i + 1).ToString)).Visible = True
Else
Me.Controls("sButton" & ((i + 1).ToString)).Enabled = False
Me.Controls("sButton" & ((i + 1).ToString)).Visible = False
End If
Next
First I would declare a form scope variable
Dim imglist As New ImageList
on the form load event:
call LoadImageList:
Which will populate you imageList with images and a key for each image
In LoadImageList add an entry for each image you wish to have available for the button.
Private Sub LoadImageList()
Me.ImageList1.Images.Add("key that matches the text in your combobox (Frog)", New Bitmap("c:\pathtoyourimages.jpg"))
Me.ImageList1.Images.Add("Dog", New Bitmap("c:\dog.jpg"))
'Do this for each images and you will have an image list that can be used to change you button.
End Sub
Next you will need a handler for your combobox click.
Private Sub cmbYourCombobox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbUserPassword.SelectedIndexChanged
Dim cmb As ComboBox = TryCast(sender, ComboBox)
If cmb IsNot Nothing Then ' This line checks to ensure that only a combobox is able to trigger this event.
Me.btnSave.Image = imglist.Images(cmb.Text)
End If
End Sub
If you need help implementing this, let me know.
My solution would be to populate the ComboBox with structures that list the state of each button.
Public Class Form1
Private Structure ButtonConfig
Public Text As String
Public ButtonStates As Boolean()
Public Sub New(Text As String, ButtonStates As Boolean())
Me.Text = Text
Me.ButtonStates = ButtonStates
End Sub
Public Overrides Function ToString() As String
Return Text
End Function
End Structure
Public Sub New()
InitializeComponent()
ComboBox1.Items.Add(New ButtonConfig("Config 1", New Boolean() {True, True, False, False, ...}))
ComboBox1.Items.Add(New ButtonConfig("Config 2", New Boolean() {True, False, True, False, ...}))
ComboBox1.Items.Add(New ButtonConfig(...)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex <> -1 Then
Dim ButtonStates As Boolean() = CType(ComboBox1.SelectedItem, ButtonConfig).ButtonStates
Dim ButtonState As Boolean
For Index As Integer = 0 To ButtonStates.Length - 1
ButtonState = ButtonStates(Index)
With Me.Controls("sButton" & ((Index + 1).ToString))
.Enabled = ButtonState
.Visible = ButtonState
End With
Next
End If
End Sub
End Class
精彩评论