2015-07-02 220 views
1

我有代碼,以便在鼠標改變文本框的邊框顏色單擊改變文本框的邊框顏色

,但我無法獲取如何實現它,並在那裏實現它

這裏是代碼:

using controlpaint.DrawBorder ,you can draw with penwidth greater than 1 

Public Class HighlightTextBox 
    Inherits System.Windows.Forms.TextBox 

    'Default Highlight color is red.>> 
    Private Highlight_Color As Color = Color.Red 

    Public Property HighLightColor() As Color 
     Get 
      Return Me.Highlight_Color 
     End Get 
     Set(ByVal value As Color) 
      Me.Highlight_Color = value 
     End Set 
    End Property 

    Private Pen_Width As Integer = 1 

    Public Property PenWidth() As Integer 
     Get 
      Return Me.Pen_Width 
     End Get 
     Set(ByVal value As Integer) 
      Me.Pen_Width = value 
     End Set 
    End Property 

    Private Sub HiLightTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus 

     Dim g As Graphics = Me.Parent.CreateGraphics 

     Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2) 

     Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, _ 

     Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid) 
    End Sub 

    Private Sub HiLightTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus 

     Dim g As Graphics = Me.Parent.CreateGraphics 
     Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width + Me.Pen_Width, Me.Width, Me.Height + Me.Pen_Width) 
     g.DrawRectangle(New Pen(Parent.BackColor, Me.Pen_Width), Rect) 
     Parent.Refresh() 

    End Sub 

    Private Sub HiLightTextBox_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged 

     Me.Refresh() 
     Call HiLightTextBox_GotFocus(Nothing, Nothing) 

    End Sub 
End Class 

我Form1並在世界上只有文本框,所以它在哪裏實現

幫助我..

+0

要在哪個控件上更改顏色? – hdkhardik

+0

如果有人點擊文本框,它應該變成綠色,如果沒有點擊,它應該變成藍色 – Aditya

+0

HiLightTextBox_GotFocus應該可能處理HiLightTextBox.GotFocus。並且其中的一些「我」應該是HiLighttextBox(例如位置/大小)。 – Fruitbat

回答

1

假設您創建了Windows窗體項目,請打開Form1.vb並在現有的End Class語句(即文件底部)之後經過此類(從Public Class HighlightTextBoxEnd Class)。

接下來,您需要刪除以Windows.Forms.ButtonBorderStyle開頭的行上的多餘換行。您將看到的上一行以下劃線結尾,這意味着下一行是代碼的延續,因此需要刪除額外的換行符,以便行繼續。

複製以下代碼粘貼Public Class Form1行之後:

Dim t1 As New HighlightTextBox 
Dim t2 As New HighlightTextBox 

現在,複製並粘貼Private Sub Form1_Load...End Sub線之間的下面的代碼。

t1.Name = "MyHTB1" 
Me.Controls.Add(t1) 
t1.Top = 20 
t1.Left = 20 

t2.Name = "MyHTB2" 
Me.Controls.Add(t2) 
t2.Top = 50 
t2.Left = 20 

這會將兩個HighlightTextBoxes添加到窗體中。當你點擊沒有焦點的那個時,邊框將按預期變成紅色。當表單打開時,如果表單上沒有任何其他內容首先獲取焦點,則默認情況下t1將具有焦點。但是,當表單第一次打開時,它不會有紅色邊框 - 不確定爲什麼我還沒有處理這個問題,但是這回答瞭如何實現這個類並創建它的實例的問題。

另請參閱How To Put a Usercontrol into Visual Studio Toolbox。我使用的是VS 2013,不需要做這個改變,但是一旦你卸載/重新加載項目,HighlightTextBox就會出現在工具箱中,所以你可以很容易地將它們添加到設計器中。

+1

您可以將我的指示與Mark Hall在其他答案中提供的課程相結合,以獲得您在評論中提到的綠色和藍色效果。我的理解是,你不知道該把代碼放在哪裏或如何創建HighlightTextBox對象,所以這就是我所關注的。 –

+0

非常感謝......你讓我明白了。現在我得到了實現的地方以及如何實現代碼。再次感謝夥計:) – Aditya

1

不確定您是否想讓顏色跟隨控件的焦點事件。按照您的示例代碼僅設置突出顯示顏色,正常狀態基於父級的BackColor。我修改了你的控件來設置兩種顏色,它是基於控件是否被聚焦,我還添加了一個計時器來檢查控件是否有焦點,在父指定後,這將允許它有選定的邊框在Form初始加載時的顏色。

看看這是否做你想要的。

Public Class HighlightTextBox 
    Inherits System.Windows.Forms.TextBox 

    'Default Highlight color is red.>> 
    Private Highlight_Color As Color = Color.Green 
    Private Normal_Color As Color = Color.Blue 
    Private WithEvents tmr As Timer = New Timer 

    Public Property HighLightColor() As Color 
     Get 
      Return Me.Highlight_Color 
     End Get 
     Set(ByVal value As Color) 
      Me.Highlight_Color = value 
     End Set 
    End Property 
    Private Property NormalColor() As Color 
     Get 
      Return Normal_Color 
     End Get 
     Set(value As Color) 
      Normal_Color = value 
     End Set 
    End Property 

    Private Pen_Width As Integer = 1 

    Public Property PenWidth() As Integer 
     Get 
      Return Me.Pen_Width 
     End Get 
     Set(ByVal value As Integer) 
      Me.Pen_Width = value 
     End Set 
    End Property 


    Private Sub SetHiLight() 
     Dim g As Graphics = Me.Parent.CreateGraphics 
     Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2) 
     Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Highlight_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid) 
    End Sub 
    Private Sub SetNormal() 
     Dim g As Graphics = Me.Parent.CreateGraphics 
     Dim Rect As New Rectangle(Me.Location.X - Me.Pen_Width, Me.Location.Y - Me.Pen_Width, Me.Width + Me.Pen_Width * 2, Me.Height + Me.Pen_Width * 2) 
     Windows.Forms.ControlPaint.DrawBorder(g, Rect, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid, Me.Normal_Color, Me.Pen_Width, Windows.Forms.ButtonBorderStyle.Solid) 
    End Sub 

    Private Sub HiLightTextBox_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged 

     Me.Refresh() 
     Call SetHiLight() 

    End Sub 

    Protected Overrides Sub OnParentChanged(e As EventArgs) 
     MyBase.OnParentChanged(e) 
     tmr.Start() 
    End Sub 

    Protected Overrides Sub OnGotFocus(e As EventArgs) 
     MyBase.OnGotFocus(e) 
     SetHiLight() 
    End Sub 
    Protected Overrides Sub OnLostFocus(e As EventArgs) 
     MyBase.OnLostFocus(e) 
     SetNormal() 
    End Sub 

    Public Sub New() 
     tmr.Interval = 10 
     AddHandler tmr.Tick, AddressOf Delay 
    End Sub 
    Private Sub Delay(sender As Object, e As EventArgs) 
     tmr.Stop() 
     If Me.Focused = True Then 
      SetHiLight() 
     Else 
      SetNormal() 
     End If 

    End Sub 

End Class