vb.net按钮单击时切换图像

时间:2013-11-17 00:00:52

标签: vb.net image button onclick

所以我使用2个PNG而不是按钮。我将按钮图像设置为图像1,并将两个图像都放在我的资源中。单击按钮后如何来回切换?

让我们来电图片lunch.png and breakfast.png

我尝试使用select case和If语句......

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

5 个答案:

答案 0 :(得分:1)

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

我已经使用图片点击并在此更改,名称就是我所称的名称,它可以很容易地只是 picturebox1 。确保首先将图片标记设置为0:)

答案 1 :(得分:0)

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

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

这是一个更短,更不漂亮的版本,在其Tag()属性中保留每个Button的布尔值。请注意,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

答案 2 :(得分:0)

以下代码对我有用:

私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.CheckedChanged

source 'https://github.com/CocoaPods/Specs.git'

plugin 'cocoapods-fix-react-native'

platform :ios, '11.0'
inhibit_all_warnings!

pod 'RxSwift'
pod 'RxCocoa'

target 'AppName' do
  pod 'RxDataSources'
  pod 'R.swift'
  pod 'Kingfisher'
  pod 'KDEAudioPlayer', :git => 'https://github.com/delannoyk/AudioPlayer.git', :branch => 'master'
  pod 'Firebase/Performance'
  pod 'Firebase/Core'
  pod 'Firebase/RemoteConfig'
  pod 'Firebase/Messaging'
  pod 'Fabric'
  pod 'Crashlytics'
  pod 'GoogleMaps'
  pod 'VIMVideoPlayer'
  pod 'ReachabilitySwift'
  pod 'React', :podspec => '../../react/external/React.0.54.4.podspec.json', :subspecs => [
    'Core',
    'cxxreact',
    'DevSupport',
    'fishhook',
    'RCTLinkingIOS',
    'RCTWebSocket',
    'RCTVibration',
    'RCTText',
    'RCTSettings',
    # Contains deprecated UIKit stuff...
    # 'RCTPushNotification',
    'RCTNetwork',
    'RCTImage',
    'RCTGeolocation',
    'RCTBlob',
    'RCTAnimation',
    'RCTActionSheet',
    'ART',
    'PrivateDatabase',
    'jsinspector',
    'jschelpers',
    'CxxBridge'
  ]
  pod 'yoga', :podspec => '../../react/external/yoga.0.54.4.podspec.json'
  pod 'Folly', :podspec => '../react/node_modules/react-native/third-party-podspecs/Folly.podspec'
  pod 'glog', :podspec => '../react/node_modules/react-native/third-party-podspecs/glog.podspec'
  pod 'react-native-video', :path => '../react/node_modules/react-native-video/react-native-video.podspec'

end

结束子

答案 3 :(得分:0)

在此解决方案中,使用默认值为 0 的整数变量会在按钮点击时触发 if 条件,a 的值变为 1。然后,在第二次点击时,if 条件失败,并且 else 触发。它将 a 的值更改为 0。同样,每次单击按钮时都会重复该过程。

dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles Button1.Click
   If a=0 Then
      Button1.image = My.Resources.image1
      a=1
   Else
      Button1.image = My.Resources.image2
      a=0
   End If
End Sub

答案 4 :(得分:-1)

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

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

and for breakfast.png使用此功能。

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("breakfast")
相关问题