Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom textboxes in VB.Net
    text
    copied!<p>tldr - Made a subclass of Textbox, text looks screwy when it has focus. What's the proper way to handle it?</p> <p>For my company's VB.Net application, I've been asked to make our textboxes behave like Google's textboxes, ie they need to have a blue-ish border around them when they have focus and a gray-ish border when they do not. I can already accomplish this by setting a textbox's BorderStyle to 'None', then drawing the appropriate rectangle within a form's Paint event. However, I have to do this for each and every single textbox that I use. And our application has quite a few of them. Needless to say, this is a pain and I'd rather have one piece of code that I can call upon.</p> <p>So I figured that I have two options; I can either make a user control that contains a single textbox which uses the above method, or I can write my own class that inherits from the TextBox class and makes this behavior standard. I have elected to use the latter approach, and via overriding the OnPaint method I have achieved the desired behavior. But now I'm encountering some new pitfalls.</p> <p>The main problem that I'm having is that text within the textbox is not rendered correctly when the textbox has focus. The text takes on a different font, appears bold, and highlighting looks wonky. If the textbox loses focus, the text looks correct. I suspect that I need to handle drawing for highlighted text differently, but I'm not sure what I need to do. Do I handle it in the OnPaint method or do I need to catch it somewhere else? Do I need to abandon this approach altogether and just make a user control?</p> <p>Bonus question: for anyone with experience making custom textboxes, are there any tips or gotchas that I need to know about? This is my first time making a custom control, so I don't really know what all to expect.</p> <p>edit: forgot to mention that I'm able to override OnPaint because I set the UserPaint flag to true. I'm guessing this was obvious, but I just want to be thorough.</p> <p>edit2: Here's the class in its entirety.</p> <pre><code>Imports System.Drawing Public Class MyCustomTextBox Inherits TextBox Public Sub New() MyBase.New() Me.BorderStyle = BorderStyle.None SetStyle(ControlStyles.UserPaint, True) End Sub Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs) 'I want these textboxes to highlight all text by default Me.SelectAll() MyBase.OnGotFocus(e) End Sub Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs) Me.SelectionLength = 0 MyBase.OnLostFocus(e) End Sub Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim p As Pen = Nothing 'MyBase.OnPaint(e) e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle) If Me.Focused Then p = New Pen(Brushes.CornflowerBlue) Else p = New Pen(Brushes.Gainsboro) End If e.Graphics.DrawRectangle(p, 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1) e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), Me.ClientRectangle) End Sub End Class </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload