How to change textbox border color and width in winforms?

我是研究僧i 提交于 2021-02-07 07:27:20

问题


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.

enter image description here

Can anyone explain me the detailed process with the source if available.


回答1:


You could do the following:

  • Place the TextBox inside a Panel
  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!