问题
I would like to know how do I change the border color and border width of textbox as something shown below
If it is mouse hover I need to display one colour and on mouse down I need to display another colour.
Can anyone explain me the detailed process with the source if available.
回答1:
You could do the following:
- Place the
TextBox
inside aPanel
- Give the panel 1 pixel padding
- Set the text dock to
Fill
- Make the text box to have no border
Then, handle mouse events on the text box, switch the background color of the panel between your two colors, when the mouse enters/leaves.
This isn't the most elegant approach in terms of using resources/handles but it should work without any custom drawing.
回答2:
Same as above with a little twist. Unfortunately I can't comment due to reputation.
- Make a
UserControl
- Set usercontrol padding on all to 1
- Put a
Panel
inside the usercontrol - Set panel dock style to fill
- Set panel padding to
6, 3, 6, 3
(left, top, right, bottom) - Put a
TextBox
inside the panel - Set textbox dock style to fill
- Set textbox borderstyle to None
...then for border colour changing properties, you could use this
Dim tbxFocus As Boolean = False
Private Sub tbx_GotFocus(sender As Object, e As EventArgs) Handles tbx.GotFocus
tbxFocus = True
Me.BackColor = Color.CornflowerBlue
End Sub
Private Sub tbx_LostFocus(sender As Object, e As EventArgs) Handles tbx.LostFocus
tbxFocus = False
Me.BackColor = SystemColors.Control
End Sub
Private Sub tbx_MouseEnter(sender As Object, e As EventArgs) Handles tbx.MouseEnter
If tbxFocus = False Then Me.BackColor = SystemColors.ControlDark
End Sub
Private Sub tbx_MouseLeave(sender As Object, e As EventArgs) Handles tbx.MouseLeave
If tbxFocus = False Then Me.BackColor = SystemColors.Control
End Sub
It's pretty self-explanatory.
来源:https://stackoverflow.com/questions/8679308/how-to-change-textbox-border-color-and-width-in-winforms