首页 > 解决方案 > 有没有办法根据 TrackBar 中的值范围指示 if-then 语句?(VB.NET)

问题描述

我想在我的 Visual Studio 项目中使用 TrackBar。我的目标是让用户滚动 TrackBar 的指示器,并根据它所在的值范围,更改标签的文本。

这是我如何尝试完成此操作的示例:

Private Sub ScrollBarProgress() Handles MyBase.Load
        If SelfEvaluationReportBAR.Value = (0) Then
            FeelingLBL.Text = "Please select a value."
        End If
        If SelfEvaluationReportBAR.Value = (1, 25) Then
            FeelingLBL.Text = "I am starting to develop my ability to perform this task."
        End If
        If SelfEvaluationReportBAR.Value = (26, 50) Then
            FeelingLBL.Text = "I feel improvement in my ability to perform this task."
        End If
        If SelfEvaluationReportBAR.Value = (51, 75) Then
            FeelingLBL.Text = "My confidence in my ability to perform this task is substantial."
        End If
        If SelfEvaluationReportBAR.Value = (76, 100) Then
            FeelingLBL.Text = "I feel fully confident in my ability to efficiently and accurately perform the day to day tasks that are assigned to me."
        End If
    End Sub

问题是,每当我尝试设置范围时,都会出现以下错误:

错误 BC30452 运算符 '=' 没有为类型 'Integer' 和 '(Integer, Integer)' 定义。

我认为我的格式错误。有没有人对范围可以/应该如何格式化有任何想法?

这是我当前对 TrackBar 的设置:

Private Sub SelfEvaluationReportBAR_Scroll(sender As Object, e As EventArgs) Handles MyBase.Load
        SelfEvaluationReportBAR.Minimum = 0
        SelfEvaluationReportBAR.Maximum = 100
        SelfEvaluationReportBAR.SmallChange = 1
        SelfEvaluationReportBAR.LargeChange = 5
        SelfEvaluationReportBAR.TickFrequency = 5
    End Sub
End Class

标签: vb.netvisual-studioif-statementrangetrackbar

解决方案


有很多方法可以完成你的任务

第一的

If SelfEvaluationReportBAR.Value >= 1 AndAlso SelfEvaluationReportBAR.Value <= 25) Then
    FeelingLBL.Text = "I am starting to develop my ability to perform this task."
End If

第二

Select case SelfEvaluationReportBAR.Value
   Case 0
      ....
   Case 1 To 25
      FeelingLBL.Text = "I am starting to develop my ability to perform this task."
   Case 26 To 50
      ...
   ' Other case follow
End Select

第三

If Enumerable.Range(1, 25).Contains(c) Then
    FeelingLBL.Text = "I am starting to develop my ability to perform this task."
End If

这些是想到的方式,可能还有其他方式。

前两个示例是最基本的方法,只有五个范围,我会使用简单的 If 一个。第三个只是显示有多少种方法存在,但我真的不建议建立一个可枚举的范围来检查是否包含一个数字。


推荐阅读