vb.net 通过单击按钮在图像之间切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20025489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:51:38  来源:igfitidea点击:

vb.net Switch between images on button click

vb.netimagebuttononclick

提问by user2782104

So I have 2 PNGs that I am using instead of the button. I set the button image as image 1 and have both the images in my resources. How do I switch them back and forth when I click the button?

所以我有 2 个 PNG 代替按钮。我将按钮图像设置为图像 1,并且在我的资源中都有这两个图像。单击按钮时如何来回切换它们?

Lets call the images lunch.png and breakfast.png

让我们调用图片lunch.png 和breakfast.png

I tried toying around with the select case and If statement...

我试着玩弄选择案例和 If 语句......

Please nothing too complicated as this is I am just learning VB and would like to understand what I am writing in.

请不要太复杂,因为这是我刚刚学习 VB 并想了解我在写什么。

回答by user3170908

Private Sub picboxSub_Ass_Detail_Click(sender As Object, e As EventArgs) Handles picboxSub_Ass_Detail.Click

    If  picboxSub_Ass_Detail.Tag = 0 Then
        picboxSub_Ass_Detail.Image = My.Resources.Tamp02
         picboxSub_Ass_Detail.Tag = 1
        GoTo a
    ElseIf  picboxSub_Ass_Detail.Tag = 1 Then
        picboxSub_Ass_Detail.Image = My.Resources.Tamp01
         picboxSub_Ass_Detail.Tag = 0
        GoTo a
    End If
    a:
End Sub

I have used the picture to click and change here, the name is just the name is what i have called it it could easily just be picturebox1. Make sure to set the picture tag to 0 first :)

我已经使用图片在此处单击并更改,名称只是名称,我称之为它很容易就是picturebox1。确保先将图片标签设置为 0 :)

回答by Luc_user3648159

The code below worked for me:

下面的代码对我有用:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.CheckedChanged

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.CheckedChanged

    If Button1.CheckState = CheckState.Checked Then
        Button1.Image = My.Resources.icon1
    Else
        Button1.Image = My.Resources.icon2
    End If

End Sub

结束子

回答by Idle_Mind

Use a variable to track the currently selected meal type. In the example below I've setup an enum to represent the two different meal types, and a public property with a private backing field to represent the current state:

使用变量来跟踪当前选择的膳食类型。在下面的示例中,我设置了一个枚举来表示两种不同的膳食类型,以及一个带有私有支持字段的公共属性来表示当前状态:

Public Class Form1

    Public Enum MealType
        Breakfast
        Lunch
    End Enum

    Private _Meal As MealType = MealType.Breakfast

    Public Property Meal As MealType
        Get
            Return _Meal
        End Get
        Set(value As MealType)
            _Meal = value
            Select Case _Meal
                Case MealType.Breakfast
                    Button1.Image = My.Resources.Breakfast

                Case MealType.Lunch
                    Button1.Image = My.Resources.Lunch

            End Select
        End Set
    End Property

    Private Sub SwapMeal()
        If Meal = MealType.Breakfast Then
            Meal = MealType.Lunch
        Else
            Meal = MealType.Breakfast
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Meal = MealType.Breakfast
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SwapMeal()
    End Sub

End Class

Here's a shorter, less beautiful version, keeping the boolean for each Button in its Tag() property. Note that the btn_Click() method is handling Button1, Button2 and Button3:

这是一个较短、不太美观的版本,将每个 Button 的布尔值保留在其 Tag() 属性中。请注意, btn_Click() 方法正在处理 Button1、Button2 和 Button3:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Button1.Tag = False
        Button2.Tag = False
        Button3.Tag = False
        Button1.PerformClick()
        Button2.PerformClick()
        Button3.PerformClick()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Dim btn As Button = DirectCast(sender, Button)
        btn.Tag = Not CBool(btn.Tag)
        btn.Image = If(CBool(btn.Tag), My.Resources.Breakfast, My.Resources.Lunch)
    End Sub

End Class

回答by ShadowLiberal

You mean how do you change an image when the user presses a button? Just use the below code to change the image to lunch.png.

您的意思是当用户按下按钮时如何更改图像?只需使用下面的代码将图像更改为lunch.png。

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("lunch")

and for breakfast.png use this.

和早餐.png 使用这个。

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("breakfast")