VB.Net通过选择Combobox项目来更改按钮图像

时间:2011-08-01 17:31:07

标签: vb.net image button combobox

我很确定必须工作,但我不能让它工作。

所以也许你可以帮助我。

我有一个Combobox,其中包含一些项目(item1-item7)和54个按钮(sButton1-sButton54)。还有一个叫做Buttons的数组(53)。数组中填充了要选择的项目,例如:Buttons(0)=“item 1,item 2”

每次选择Combobox中的另一个项目时,我只想要一些按钮来更改它们的图像。因此我得到了阵列。如果选择了项目1并且按钮(0)包含项目1,我想要更改Button1的图像。

编辑:它适用于更改每个按钮的图像:

sButton1.Image = My.Resources.image1

但我更愿意改为一步到位(循环)。

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
                            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

2 个答案:

答案 0 :(得分:0)

首先我要声明一个表单范围变量

Dim imglist As New ImageList

表单加载事件:

call LoadImageList:

将使用图像和每个图像的键为imageList填充

在LoadImageList中,为您希望可用于该按钮的每个图像添加一个条目。

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

接下来,你需要一个处理你的组合框点击工具。

      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

如果您需要帮助实现此功能,请与我们联系。

答案 1 :(得分:0)

我的解决方案是使用列出每个按钮状态的结构填充ComboBox。

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