无法使用Visual Basic 2010在Groupbox中的文本框周围绘制矩形.-

时间:2014-08-26 12:55:13

标签: vb.net

我有一个vb形式的两个文本框控件.-如果它们不在组框中,我可以绘制关于两个文本框的矩形.-只有当文本框得到焦点时才会绘制矩形.-代码可以工作,但不知何故,当我把文本框放在一个组框中时,矩形不会被绘制.-

the rectangle only is drawed when textbox is focused

这是我一直在使用的代码.-

Public Class Form1
    Dim curControl As TextBox
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
    If Not curControl Is Nothing Then
        If curControl.Name = "TextBox1" Then
            Dim g As Graphics = e.Graphics
            Dim pen As New Pen(Color.Lime, 2.0)
            g.DrawRectangle(pen, New Rectangle(TextBox1.Location, TextBox1.Size))
            pen.Dispose()
        End If
    End If
    If Not curControl Is Nothing Then
        If curControl.Name = "TextBox2" Then
            Dim g As Graphics = e.Graphics
            Dim pen As New Pen(Color.Red, 2.0)
            g.DrawRectangle(pen, New Rectangle(TextBox2.Location, TextBox2.Size))
            pen.Dispose()
        End If
    End If
End Sub
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
    curControl = DirectCast(sender, TextBox)
    Me.Invalidate()
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave
    curControl = Nothing
    Me.Invalidate()
End Sub

0 个答案:

没有答案