Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change textbox border color and width in winforms?

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.

like image 361
coder Avatar asked Dec 30 '11 12:12

coder


People also ask

How do I change the border color in Windows?

The border settings of the TextBoxExt control are discussed in this section. You can change the color and styles of the border of the TextBoxExt control by using Border3DStyle, BorderColor, BorderSides and BorderStyle properties.

How do I remove the TextBox border in Visual Studio?

While viewing the form in designer mode you can right click one of the textboxes and select Properties. This should open a pane that will permit you to change the BorderStyle property to None.

How do you add a border to a TextBox in HTML?

You can use the CSS border property to add a border around your HTML textboxes. You can also use border-width , border-style , and border-color , but the border property covers all of these anyway.


2 Answers

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.

like image 176
Uwe Keim Avatar answered Sep 20 '22 02:09

Uwe Keim


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.

like image 35
Fluffy Sebbert Avatar answered Sep 19 '22 02:09

Fluffy Sebbert