我有一个表单,在Paint事件中,一些带有一些信息的标签被绘制在一个面板中 - 这很好用。但是,我想根据放在同一表单上的Trackbar的值来更改标签上的文本。这是Trackbar-Scroll事件,它应该刷新整个表单:
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
Me.Refresh()
End Sub
这是在表单上绘制标签的代码:
Public Sub Form_Paint(sender As Object, e As PaintEventArgs)
For i = 0 To 10
Dim tb As New Label
tb.Name = "tb" & CStr(i)
If Me.TrackBar1.Value = 1 Then tb.Text = "sometext"
If Me.TrackBar1.Value = 0 Then tb.Text = "anothertext"
tb.Location = New Point(i, i * 2)
Me.Panel1.Controls.Add(tb)
Next
End Sub
然而,无论轨道栏处于什么状态,标签中显示的文字都是“anothertext”。当我更改轨迹栏的值时,我会告诉Paint事件,但是如何强制标签更新呢?
答案 0 :(得分:1)
只需添加一次标签即可。将创建和更改逻辑分为两种方法
Private prefix As String = "tb"
Private factor As Integer = 10
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
addLabels()
changeLabels()
End Sub
Private Sub addLabels()
For i = 0 To 10
Dim tb As New Label()
tb.Name = prefix & CStr(i)
tb.Location = New Point(factor * i, factor * i * 2)
Me.Panel1.Controls.Add(tb)
Next
End Sub
Private Sub changeLabels()
For i = 0 To 10
Dim tb As Label = CType(Panel1.Controls(prefix & CStr(i)), Label)
If Me.TrackBar1.Value = 1 Then tb.Text = "sometext"
If Me.TrackBar1.Value = 0 Then tb.Text = "anothertext"
Next
End Sub
现在,在TrackBar1_Scroll中,您只需更改它们(而不是重新创建它们)
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
changeLabels()
End Sub
由于标签值取决于TrackBar值,因此没有理由在Paint中更新它们,这比TrackBar更新频繁发生。
添加新标签并删除Paint中的旧标签似乎很多的额外处理。
答案 1 :(得分:-2)
我认为你需要设置TrackBar的LargeChange属性,因为Scroll Event被认为是一个很大的变化,但是LargeChange默认为0,因此当你滚动Value时只增加/减少0,保持0 < / p>