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

G

Graham Blandford

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
 
K

Ken Tucker [MVP]

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
 
G

Graham Blandford

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
 
M

Mick Doherty

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.
 
G

Graham Blandford

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"
 
M

Mick Doherty

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
///
 
G

Graham Blandford

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"
 
M

Mick Doherty

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top