ActivityBar控件
最后我们建立ActivityBar控件来显示动态点。
给项目添加一个叫ActivityBar的用户控件,将该控件调整到大约宽110,高20,可以拖拉边界或在属性窗口中设定Size值来实现。
其它的在代码中实现。为了建立一系列动态闪烁的"亮点",我们将Timer控件与一组PictureBox控件一起使用。每次Timer控件到期,我们将下一个PictureBox设为绿色,将已经是绿色的变成窗体色。
放置Timer控件,将它的名字改为tmAnim,Interval属性设为300。
另外,在Components页上有一个不同的Timer控件。这是个多线程时钟。换句话说,它在后台线程上建立Elapsed事件,不同于UI线程上的Windows窗体时钟。在建立UI时这明显达不到目标,因为Elapsed事件中的代码明显不能直接与UI交互。
现在向控件中添加下列代码:
| Private mBoxes As New ArrayList() Private mCount As Integer Private Sub ActivityBar_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim index As Integer If mBoxes.Count = 0 Then For index = 0 To 6 mBoxes.Add(CreateBox(index)) Next End If mCount = 0 End Sub Private Function CreateBox(ByVal index As Integer) As PictureBox Dim box As New PictureBox() With box SetPosition(box, index) .BorderStyle = BorderStyle.Fixed3D .Parent = Me .Visible = True End With Return box End Function Private Sub GrayDisplay() Dim index As Integer For index = 0 To 6 CType(mBoxes(index), PictureBox).BackColor = Me.BackColor Next End Sub Private Sub SetPosition(ByVal Box As PictureBox, ByVal Index As Integer) Dim left As Integer = CInt(Me.Width / 2 - 7 * 14 / 2) Dim top As Integer = CInt(Me.Height / 2 - 5) With Box .Height = 10 .Width = 10 .Top = top .Left = left + Index * 14 End With End Sub Private Sub tmAnim_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles tmAnim.Tick CType(mBoxes((mCount + 1) Mod 7), PictureBox).BackColor = _ Color.LightGreen CType(mBoxes(mCount Mod 7), PictureBox).BackColor = Me.BackColor mCount += 1 If mCount > 6 Then mCount = 0 End Sub Public Sub Start() CType(mBoxes(0), PictureBox).BackColor = Color.LightGreen tmAnim.Enabled = True End Sub Public Sub [Stop]() tmAnim.Enabled = False GrayDisplay() End Sub Private Sub ActivityBar_Resize(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Resize Dim index As Integer For index = 0 To mBoxes.Count - 1 SetPosition(CType(mBoxes(index), PictureBox), index) Next End Sub |
窗体的Load事件建立PictureBox控件并把它们放入一个数组,这样容易循环。Timer控件的Tick事件按次序循环使每个点变为绿色。
这些都由Start事件启动,由Stop事件停止。由于Stop是保留字,该方法的名称加上了方括号:[Stop]。Stop方法不仅停止定时器,而且使所有的方框变为灰色以显示没有当前活动。
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页