Why Doesn't Control.Show Invoke OnPaint?

E

eBob.com

I'm trying to develop a text box type control and have written the simple
code below as an initial step. But I have learned that the .Show method is
not invoking the OnPaint method (which is overridden in my control). Maybe
I could directly invoke the OnPaint method but since it's Protected in the
Control class I figure that's the way it should be.

For my simple sandbox, code below, I have put two NumericUpDowns and a
button on Form1 using the IDE (numWidth, numHeight, and btnDraw). My text
box type control is implemented by the SpecialTextBox class (instantiated as
"stb" in the code and placed on Form1 via code). If I adjust the width of
the SpecialTextBox control to say 80 pixels and click on Draw the control's
width does appear to be narrowed - the white box on the form becomes much
more narrow, but the text is truncated not wrapped (the OnPaint code would
have caused it to wrap). And using Debug I have confirmed that the OnPaint
method is not called when I click on the Draw button - and therefore that
stb.Show is not causing the OnPaint method to be invoked. If I then
minimize the form and restore it the OnPaint method is invoked and the text
is wrapped.

I don't understand why I should have to do more than stb.Show to get the
SpecialTextBox redrawn by my OnPaint code. (And indeed stb.Show does seem
to, somehow, get the width changed - but without invoking my OnPaint code).
What should I have to do to get my control redrawn?

I think that I must be missing something basic here in how controls work, so
I will be most grateful for some help.

(code is below) Thanks, Bob

Imports System.ComponentModel
Imports System.drawing.drawing2d

Public Class Form1
Inherits System.Windows.Forms.Form

Public sampstring As String = "The quick brown fox jumped over the lazy
dog."

Dim stb As New SpecialTextBox


#Region " Windows Form Designer generated code "
#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

stb.Text = sampstring
stb.Location = New Point(100, 100)
stb.Width = 300
nudWidth.Value = stb.Width
stb.Height = 200
nudHeight.Value = stb.Height
Controls.Add(stb)

End Sub

Private Sub btnDraw_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDraw.Click
stb.Width = nudWidth.Value
stb.Height = nudHeight.Value
stb.Show()
End Sub
End Class

Public Class SpecialTextBox
Inherits System.Windows.Forms.Control

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

Dim rectf As RectangleF
Dim grfx As Graphics = e.Graphics
grfx.FillRectangle(Brushes.White, Me.ClientRectangle)
'grfx.FillRectangle(Brushes.Yellow, 0, 0, 90, 20)
rectf = New RectangleF(New PointF(0, 0), New
SizeF(Me.ClientRectangle.Width, Me.ClientRectangle.Height))
grfx.DrawString(Me.ClientRectangle.Width.ToString & "/" &
Me.ClientRectangle.Height.ToString _
& " The quick brown fox jumped over the lazy dog.", Font,
Brushes.Black, rectf)
MyBase.OnPaint(e)

End Sub
End Class
 
A

Armin Zingler

eBob.com said:
I'm trying to develop a text box type control and have written the
simple code below as an initial step. But I have learned that the
.Show method is not invoking the OnPaint method (which is overridden
in my control). Maybe I could directly invoke the OnPaint method
but since it's Protected in the Control class I figure that's the
way it should be.

Maybe I didn't understand the whole problem, but I think you want to
call the Control's Refresh method. Show indeed only does anything if
it's not yet visible because it's equal to setting visible=true.
Assuming that the UI thread isn't busy for a long time, you can also
call it's invalidate method instead or Refresh.


Armin
 
E

eBob.com

Armin,

Thanks so much for your very prompt and complete reply. It was exactly what
I needed. I realize now that it was sort of a dumb question, but this is
the first time I have inherited from Control.

Thanks again, Bob
 

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