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

New problem with User control...derived class..

P: n/a
OK, having answered my questions in a previous feed about pulling my derived
class into the IDE....

Here's a new problem that I'm hoping can be fixed and isn't a .NET bug....

I have created an inherited class based on the textbox... which, all I am
doing is allowing a forecolor to be set when .enabled is set to true.

it works nicely, EXCEPT that when I click into the box, the font (set as
default MS Sans 8.25pt) does not appear to be used and instead what looks
like an Arial 10pt or larger is used - and there is some messy painting
going on.....doesn't clear the original text and the 2 fonts run into each
other...

Here's the entire class;

Public Class LinkMAN_TextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)

End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

End Class

I suspect this may be a bug, but if anyone can help I'd appreciate it.

Thanks,
Graham
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

Try this. I solved some of the problems by only drawing the textbox
when it is readonly. I also invalidate when it recieves the wm_setcursor
message. Hope this helps.

Public Class ExtendedTextbox

Inherits TextBox

Public Sub New()

setstyle(ControlStyles.UserPaint, True)

End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)

If Me.ReadOnly Then

pevent.Graphics.Clear(Me.BackColor)

Else

MyBase.OnPaintBackground(pevent)

End If

End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim br As New SolidBrush(Me.ForeColor)

Dim sf As New StringFormat

Dim rDraw As RectangleF = RectangleF.op_Implicit(Me.ClientRectangle)

rDraw.Inflate(-5, 0)

Select Case Me.TextAlign

Case HorizontalAlignment.Center

sf.Alignment = StringAlignment.Center

Case HorizontalAlignment.Left

sf.Alignment = StringAlignment.Near

Case HorizontalAlignment.Right

sf.Alignment = StringAlignment.Far

End Select

If Me.ReadOnly Then

e.Graphics.DrawString(Me.Text, Me.Font, br, rDraw, sf)

Else

MyBase.OnPaint(e)

End If

End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Const WM_SETCURSOR As Integer = &H20

If m.Msg = WM_SETCURSOR And Me.ReadOnly Then

Me.Invalidate()

Else

MyBase.WndProc(m)

End If

End Sub

End Class

Ken

--------------------------------

"Graham Blandford" <gr**************@sympatico.ca> wrote in message
news:b7*********************@news20.bellglobal.com ...
OK, having answered my questions in a previous feed about pulling my
derived
class into the IDE....

Here's a new problem that I'm hoping can be fixed and isn't a .NET bug....

I have created an inherited class based on the textbox... which, all I am
doing is allowing a forecolor to be set when .enabled is set to true.

it works nicely, EXCEPT that when I click into the box, the font (set as
default MS Sans 8.25pt) does not appear to be used and instead what looks
like an Arial 10pt or larger is used - and there is some messy painting
going on.....doesn't clear the original text and the 2 fonts run into each
other...

Here's the entire class;

Public Class LinkMAN_TextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)

End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

End Class

I suspect this may be a bug, but if anyone can help I'd appreciate it.

Thanks,
Graham

Nov 20 '05 #2

P: n/a
Hi Ken,

Appears to raise more issues than resolving...

My disabled boxes show empty, and nothing changes on the enabled box - I'm
gonna try and play with performing the Mybase.?? methods and replace them
one at a time to see what happens - or indeed see if executing the mybase
methods still causes the problem - in which case it is safe to say these are
bugs???

Thanks,
Graham

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
Hi,

Try this. I solved some of the problems by only drawing the textbox when it is readonly. I also invalidate when it recieves the wm_setcursor
message. Hope this helps.

Public Class ExtendedTextbox

Inherits TextBox

Public Sub New()

setstyle(ControlStyles.UserPaint, True)

End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)

If Me.ReadOnly Then

pevent.Graphics.Clear(Me.BackColor)

Else

MyBase.OnPaintBackground(pevent)

End If

End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim br As New SolidBrush(Me.ForeColor)

Dim sf As New StringFormat

Dim rDraw As RectangleF = RectangleF.op_Implicit(Me.ClientRectangle)

rDraw.Inflate(-5, 0)

Select Case Me.TextAlign

Case HorizontalAlignment.Center

sf.Alignment = StringAlignment.Center

Case HorizontalAlignment.Left

sf.Alignment = StringAlignment.Near

Case HorizontalAlignment.Right

sf.Alignment = StringAlignment.Far

End Select

If Me.ReadOnly Then

e.Graphics.DrawString(Me.Text, Me.Font, br, rDraw, sf)

Else

MyBase.OnPaint(e)

End If

End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Const WM_SETCURSOR As Integer = &H20

If m.Msg = WM_SETCURSOR And Me.ReadOnly Then

Me.Invalidate()

Else

MyBase.WndProc(m)

End If

End Sub

End Class

Ken

--------------------------------

"Graham Blandford" <gr**************@sympatico.ca> wrote in message
news:b7*********************@news20.bellglobal.com ...
OK, having answered my questions in a previous feed about pulling my
derived
class into the IDE....

Here's a new problem that I'm hoping can be fixed and isn't a .NET bug....
I have created an inherited class based on the textbox... which, all I am doing is allowing a forecolor to be set when .enabled is set to true.

it works nicely, EXCEPT that when I click into the box, the font (set as
default MS Sans 8.25pt) does not appear to be used and instead what looks like an Arial 10pt or larger is used - and there is some messy painting
going on.....doesn't clear the original text and the 2 fonts run into each other...

Here's the entire class;

Public Class LinkMAN_TextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)

End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

End Class

I suspect this may be a bug, but if anyone can help I'd appreciate it.

Thanks,
Graham


Nov 20 '05 #3

P: n/a
Hi Graham

This looks like the same problem that I am having with custom controls with
style set to userpaint.
I have been trying to solve the problem of TabItems not resizing to
accomodate the set Font in a userpaint TabControl, and it looks like this is
the problem. If I find the answer I'll post it. A quick look at MSDN shows
that when WM_GETFONT returns 0 the system default font is used to draw
text(which would probably be Arial 10) I cannot catch a WM_GETFONT or
WM_SETFONT message though, unless I remove the userpaint style.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Graham Blandford" <gr**************@sympatico.ca> wrote in message
news:b7*********************@news20.bellglobal.com ...
OK, having answered my questions in a previous feed about pulling my derived class into the IDE....

Here's a new problem that I'm hoping can be fixed and isn't a .NET bug....

I have created an inherited class based on the textbox... which, all I am
doing is allowing a forecolor to be set when .enabled is set to true.

it works nicely, EXCEPT that when I click into the box, the font (set as
default MS Sans 8.25pt) does not appear to be used and instead what looks
like an Arial 10pt or larger is used - and there is some messy painting
going on.....doesn't clear the original text and the 2 fonts run into each
other...

Here's the entire class;

Public Class LinkMAN_TextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)

End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

End Class

I suspect this may be a bug, but if anyone can help I'd appreciate it.

Thanks,
Graham

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004
Nov 20 '05 #4

P: n/a
Thanks Mick - if you do manage to find a solution, feel free to let me know.

In the meantime, any ideas on my recourse - should this be reported as a bug
to MS - if it hasn't already - and is there a 'correct' way to reporting
these?
Thanks,
Graham

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:eQ*************@TK2MSFTNGP10.phx.gbl...
Hi Graham

This looks like the same problem that I am having with custom controls with style set to userpaint.
I have been trying to solve the problem of TabItems not resizing to
accomodate the set Font in a userpaint TabControl, and it looks like this is the problem. If I find the answer I'll post it. A quick look at MSDN shows
that when WM_GETFONT returns 0 the system default font is used to draw
text(which would probably be Arial 10) I cannot catch a WM_GETFONT or
WM_SETFONT message though, unless I remove the userpaint style.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Graham Blandford" <gr**************@sympatico.ca> wrote in message
news:b7*********************@news20.bellglobal.com ...
OK, having answered my questions in a previous feed about pulling my

derived
class into the IDE....

Here's a new problem that I'm hoping can be fixed and isn't a .NET bug....
I have created an inherited class based on the textbox... which, all I am doing is allowing a forecolor to be set when .enabled is set to true.

it works nicely, EXCEPT that when I click into the box, the font (set as
default MS Sans 8.25pt) does not appear to be used and instead what looks like an Arial 10pt or larger is used - and there is some messy painting
going on.....doesn't clear the original text and the 2 fonts run into each other...

Here's the entire class;

Public Class LinkMAN_TextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)

End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

End Class

I suspect this may be a bug, but if anyone can help I'd appreciate it.

Thanks,
Graham

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004

Nov 20 '05 #5

P: n/a
I can't get it to play nicely. I can change the font, but it's not quite the
right size.

In sub New() add the line:
\\\
Font = New Font(Font, Font.Size)
///

Then add:
\\\
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
MyBase.OnFontChanged(e)
Const WM_SETFONT As Integer = &H30
WndProc(Message.Create(Handle, WM_SETFONT, Font.ToHfont, IntPtr.Zero))
End Sub
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004
Nov 20 '05 #6

P: n/a
Hi Mick,

It certainly put me on the right track - and think I've cracked it. Well ...
at least for this particular scenario. I can change the forecolour of my
disabled boxes...

Notice I used Font.Style instead of Font.Size in the Font assign.

Anyway, this is what I have ended up with...

Public Class ExtendedTextBox

Inherits System.Windows.Forms.TextBox

Public Sub New()
MyBase.New()
Font = New Font(Font, Font.Style)
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim drawBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, drawBrush, 0.0F, 0.0F)
End Sub

Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
MyBase.OnFontChanged(e)
Const WM_SETFONT As Integer = &H30
WndProc(Message.Create(Handle, WM_SETFONT, Font.ToHfont,
IntPtr.Zero))
End Sub

Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
If Not Me.Enabled Then
Me.SetStyle(ControlStyles.UserPaint, True)
End If
End Sub

End Class

Thanks for all your help guys!
Graham


"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:ee**************@TK2MSFTNGP09.phx.gbl...
I can't get it to play nicely. I can change the font, but it's not quite the right size.

In sub New() add the line:
\\\
Font = New Font(Font, Font.Size)
///

Then add:
\\\
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
MyBase.OnFontChanged(e)
Const WM_SETFONT As Integer = &H30
WndProc(Message.Create(Handle, WM_SETFONT, Font.ToHfont, IntPtr.Zero))
End Sub
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004

Nov 20 '05 #7

P: n/a
Yes, I started with Font.Style and made several changes whilst trying to get
the correct size. There's not much of a difference, but there is one.

What I have found, is that EnableVisualStyles messes up the TabSize in my
TabControl, but it didn't appear to make any difference to the TextBox. I
have disabled Visual Styles in my TabControl and Tabs now seem to size
correctly if they have a relatively short Text (Items with a long Text give
me some curious results).

I would probably never have thought to look at WM_SETTEXT if I hadn't seen
your post, so Thankyou for finding this problem.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Graham Blandford" <gr**************@sympatico.ca> wrote in message
news:Sw********************@news20.bellglobal.com. ..
Hi Mick,

It certainly put me on the right track - and think I've cracked it. Well .... at least for this particular scenario. I can change the forecolour of my
disabled boxes...

Notice I used Font.Style instead of Font.Size in the Font assign.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004
Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.