By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Cannot change Font in custom textbox at runtime

P: 2
I am using VB.NET VS2010

This seems like a trivial problem but I am unable to get past it. I have a very simple custom textbox class (full code below) that draws a rounded corner border around the textbox. This works as I expected. However, the font that appears in the textbox at runtime is not what I would like, so, I put a line of code in the Form that set it to a different font - no change. I tried setting the font using the properties window in design mode. It accepted the change there, but, at runtime the old default font was used. I am unable to control the font of the custom textbox either at runtime or design time. I did find that if I do not set ControlStyles.UserDraw to TRUE then the custom textbox behaves normally w/r/t font change but does not draw the custom border - but this defeats the purpose of making the custom textbox in the first place. I am sure I must be missing something obvious here. Help?

Expand|Select|Wrap|Line Numbers
  1. Imports System.Drawing.Drawing2D
  2. Public Class RoundedTextbox
  3.     Inherits TextBox
  4.     Public Sub New()
  5.         MyBase.New()
  6.         SetStyle(ControlStyles.UserPaint, True)
  7.         SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  8.         BorderStyle = BorderStyle.FixedSingle
  9.     End Sub
  10.     Private Function ShrunkenBox() As RectangleF
  11.         Return New RectangleF(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1)
  12.     End Function
  13.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  14.         e.Graphics.FillRectangle(Brushes.White, ClientRectangle)
  15.         e.Graphics.DrawPath(Pens.LightGray, RoundCornerRectangle(ShrunkenBox))
  16.     End Sub
  17.     Private Function RoundCornerRectangle(ByVal Box As RectangleF) As Drawing2D.GraphicsPath
  18.         Dim Xcorner As Single = 6
  19.         Dim Ycorner As Single = 6
  20.         Dim result As New Drawing2D.GraphicsPath
  21.         '--- UL corner
  22.         result.AddArc(New RectangleF(Box.X, Box.Y, Xcorner, Ycorner), 180, 90)
  23.         '--- UR corner
  24.         result.AddArc(New RectangleF(Box.X + Box.Width - Xcorner, Box.Y, Xcorner, Ycorner), 270, 90)
  25.         '--- LR corner
  26.         result.AddArc(New RectangleF(Box.X + Box.Width - Xcorner, Box.Y + Box.Height - Ycorner, Xcorner, Ycorner), 0, 90)
  27.         '--- LL corner
  28.         result.AddArc(New RectangleF(Box.X, Box.Y + Box.Height - Ycorner, Xcorner, Ycorner), 90, 90)
  29.  
  30.         result.CloseFigure()
  31.         Return result
  32.     End Function
  33. End Class
  34.  
Feb 28 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.