TextBox边框为空时为蓝色,然后边框为红色vb.net

时间:2019-10-06 09:36:52

标签: vb.net textbox

文本框为空时为蓝色边框,然后为边框红色vb.net我正在使用以下代码,但不起作用

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim p As New Pen(Color.Blue, 2)
        e.Graphics.DrawRectangle(p, New Rectangle(TextBox1.Location + New Size(1, 1), TextBox1.Size - New Size(2, 2)))
        p.Dispose()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then

            TextBox1.Region = New Region(New Rectangle(2, 2, TextBox1.Width - 4, TextBox1.Height - 4))
        Else
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

这是您的代码:

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    With TextBox1
        Dim p As New Pen(If(.Text = "", Color.Red, Color.Blue), 2)
        e.Graphics.DrawRectangle(p, .Left - 1, .Top - 1, .Width + 2, .Height + 2)
        p.Dispose()
    End With
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.Invalidate()
End Sub