如何在图像之间切换,但使用昏暗的声明

时间:2014-08-20 15:49:41

标签: vb.net

我有以下代码......

'------------------------------------------------------------------------------------------
Dim MMButtonClicked As Boolean = False
Private Sub MainMenuTabBtn_Click(sender As Object, e As EventArgs) Handles MainMenuTabBtn.Click
    MainMenuTabBtn.Image = My.Resources.MainMenuTab_Selected
    MMButtonClicked = True
    TabControl1.SelectedTab = TabPage1
End Sub
' MainMenuTab Down
Private Sub MainMenuTabBtn_MouseEnter(sender As Object, e As EventArgs) Handles MainMenuTabBtn.MouseEnter
    If Not MMButtonClicked Then
        MainMenuTabBtn.Image = My.Resources.MainMenuTab_Down
    End If
End Sub
' MainMenuTab Up
Private Sub MainMenuTabBtn_MouseLeave(sender As Object, e As EventArgs) Handles MainMenuTabBtn.MouseLeave
    If Not MMButtonClicked Then
        MainMenuTabBtn.Image = My.Resources.MainMenuTab_Norm
    End If
End Sub
'------------------------------------------------------------------------------------------
Dim IbuttonClicked As Boolean = False
Private Sub ItemsTabBtn_Click(sender As Object, e As EventArgs) Handles ItemsTabBtn.Click
    ItemsTabBtn.Image = My.Resources.ItemsTab_Selected
    IbuttonClicked = True
    TabControl1.SelectedTab = TabPage2
End Sub
' ItemsTab Down
Private Sub ItemsTabBtn_MouseEnter(sender As Object, e As EventArgs) Handles ItemsTabBtn.MouseEnter
    If Not IbuttonClicked Then
        ItemsTabBtn.Image = My.Resources.ItemsTab_Down
    End If
End Sub
' ItemsTab Up
Private Sub ItemsTabBtn_MouseLeave(sender As Object, e As EventArgs) Handles ItemsTabBtn.MouseLeave
    If Not IbuttonClicked Then
        ItemsTabBtn.Image = My.Resources.ItemsTab_Norm
    End If
End Sub

这是MainMenuTabBtn和ItemsTabBtn的翻转,但是当它们被点击时,它们会改为" MainMenuTab_Selected.jpg"或" ItemsTab_Selected.jpg"取决于按钮。但是,当点击ItemsTab时,如何实现它,MainMenuTab将从" MainMenuTab_Selected.jpg" (其选定的州)到#34; MainMenuTab_Normal.jpg"。 (未选中)但选择了ItemsTab。

1 个答案:

答案 0 :(得分:0)

您可以拥有任意数量的按钮。只需相应更改每个按钮事件中的WhichBtnSelected即可。

Dim WhichBtnSelected as Integer = 0 'none selected

Private Sub MainMenuTabBtn_Click(sender As Object, e As EventArgs) Handles MainMenuTabBtn.Click
    If WhichBtnSelected = 1 then
        Return
    End If

    If WhichBtnSelected = 2 then
        ItemsTabBtn.Image = My.Resources.ItemsTab_Norm
    End If

    WhichBtnSelected = 1
    MainMenuTabBtn.Image = My.Resources.MainMenuTab_Selected
    TabControl1.SelectedTab = TabPage1
End Sub

' MainMenuTab Enter
Private Sub MainMenuTabBtn_MouseEnter(sender As Object, e As EventArgs) Handles MainMenuTabBtn.MouseEnter
    If WhichBtnSelected = 1 then
        Return
    End If

    MainMenuTabBtn.Image = My.Resources.MainMenuTab_Enter
End Sub
' MainMenuTab Down
Private Sub MainMenuTabBtn_MouseDown(sender As Object, e As EventArgs) Handles MainMenuTabBtn.MouseDown
    If WhichBtnSelected = 1 then
        Return
    End If

    MainMenuTabBtn.Image = My.Resources.MainMenuTab_Down
End Sub

Private Sub MainMenuTabBtn_MouseLeave(sender As Object, e As EventArgs) Handles MainMenuTabBtn.MouseLeave
    If WhichBtnSelected = 1 then
        Return
    End If

    MainMenuTabBtn.Image = My.Resources.MainMenuTab_Norm
End Sub

“---------------------------------------------- --------------------------------------------

Private Sub ItemsTabBtn_Click(sender As Object, e As EventArgs) Handles ItemsTabBtn.Click
    If WhichBtnSelected = 2 then
        Return
    End If

    If WhichBtnSelected = 1 then
        MainMenuTabBtn.Image = My.Resources.MainMenuTab_Norm
    End If

    WhichBtnSelected = 2

    ItemsTabBtn.Image = My.Resources.ItemsTab_Selected

    TabControl1.SelectedTab = TabPage2
End Sub

' ItemsTab Enter
Private Sub ItemsTabBtn_MouseEnter(sender As Object, e As EventArgs) Handles ItemsTabBtn.MouseEnter
    If WhichBtnSelected = 2 then
        Return
    End If

    ItemsTabBtn.Image = My.Resources.ItemsTab_Enter
End Sub

' ItemsTab Down
Private Sub ItemsTabBtn_MouseDown(sender As Object, e As EventArgs) Handles ItemsTabBtn.MouseDown
    If WhichBtnSelected = 2 then
        Return
    End If

    ItemsTabBtn.Image = My.Resources.ItemsTab_Down
End Sub

Private Sub ItemsTabBtn_MouseLeave(sender As Object, e As EventArgs) Handles ItemsTabBtn.MouseLeave
    If WhichBtnSelected = 2 then
        Return
    End If

    ItemsTabBtn.Image = My.Resources.ItemsTab_Norm
End Sub

当我尝试制作自定义按钮时,我没有使用Button控件而是使用Label

修改

如果你有5个按钮:

Dim bttns(4) As Button
Dim imgs(4) As Image

在表单中加载

bttns(0) = MainMenuTabBtn
bttns(1) = ItemsTabBtn
...
bttns(4) = ....

imgs(0) = My.Resources.MainMenuTab_Norm
imgs(1) = My.Resources.ItemsTab_Norm
...
imgs(4) = ....

在每个按钮点击事件中,用

替换第二个If语句
If WhichBtnSelected <> 0 then
    bttns(WhichBtnSelected - 1).Image = imgs(WhichBtnSelected - 1)
End If

当然,您还必须根据按钮更改数字:

If WhichBtnSelected = ? then '1, 2, 3, ...
    Return
End If

瓦尔特