label control text rotation (paint) revisited.

J

jcrouse

I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax. I
don't really understand the event. Where it works fine, it seems to fire
when the form changes visibility. Here is the code.

Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
If lblP1JoyUp.Visible = True Then

Dim myFontBrush As New SolidBrush(lblP1JoyUp.ForeColor)

If frm1.intPriHP1 = frm1.intPriVP1 Then

If strLay1RotAngle = "90" Then

Dim y As Integer =
CInt(e.Graphics.MeasureString(Label1.Text, lblP1JoyUp.Font).Width)

Dim x As Integer =
CInt(e.Graphics.MeasureString(Label1.Text, lblP1JoyUp.Font).Height)

lblP1JoyUp.Text = ""

e.Graphics.TranslateTransform((lblP1JoyUp.ClientSize.Width +
x) \ 2, (lblP1JoyUp.ClientSize.Height + y) \ 2)

e.Graphics.RotateTransform(90)

e.Graphics.DrawString(Label1.Text, lblP1JoyUp.Font,
myFontBrush, -y, 0)

End If

myFontBrush.Dispose()

End If

End Sub


However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click event, or
better yet, maybe inside a sub of its own. I need to call the above code for
30 label controls so having it in it's own sub would probably be the way to
go, right?

Help,
John
 
H

Herfried K. Wagner [MVP]

* "jcrouse said:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click event, or
better yet, maybe inside a sub of its own. I need to call the above code for
30 label controls so having it in it's own sub would probably be the way to
go, right?

I would derive a class from 'System.Windows.Forms.Label' and override
its 'OnPaint' sub. Then I would add a property 'Angle' that determines
the angle in which the text should be drawn. In the setter for this
property you can call 'Me.Invalidate' to redraw the text in the right
angle. Then you can use this control instead of the standard label
control.
 
J

jcrouse

That's way outside of my capabilities. I don't understand.

John

Herfried K. Wagner said:
* "jcrouse said:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click event, or
better yet, maybe inside a sub of its own. I need to call the above code for
30 label controls so having it in it's own sub would probably be the way to
go, right?

I would derive a class from 'System.Windows.Forms.Label' and override
its 'OnPaint' sub. Then I would add a property 'Angle' that determines
the angle in which the text should be drawn. In the setter for this
property you can call 'Me.Invalidate' to redraw the text in the right
angle. Then you can use this control instead of the standard label
control.
 
J

jcrouse

I can't give up my standard label controls. I have too much code going on
with them. I have mouse ups, mouse downs, click events, mouse moves and all
kinds of settable properties. They are part of a form designer and are
movable at runtime. also, the font, forecolor, backcolor and size are all
settable with menus. It just seems like too much to change.

John

Herfried K. Wagner said:
* "jcrouse said:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click event, or
better yet, maybe inside a sub of its own. I need to call the above code for
30 label controls so having it in it's own sub would probably be the way to
go, right?

I would derive a class from 'System.Windows.Forms.Label' and override
its 'OnPaint' sub. Then I would add a property 'Angle' that determines
the angle in which the text should be drawn. In the setter for this
property you can call 'Me.Invalidate' to redraw the text in the right
angle. Then you can use this control instead of the standard label
control.
 
J

Jay B. Harlow [MVP - Outlook]

John,
When you derive from Label the control you create is a standard label, which
means you continue to use the same mouse ups, mouse downs, click events,
mouse moves and all kinds of the same settable properties.

You continue using the same form designer to set the same properties, the
only difference is, you new Label now has 1 more property an Angle
property...

I too would recommend you derive from Label and add an Angle Property, as
once you learn how I suspect your forms will suddenly become simpler and
easier to create, understand, and maintain later.

For a good intro explanation of OO in VB.NET I would recommend Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

To answer your original question you do not really want to duplicate the
paint logic, as this is highly error prone. Instead you simply want to have
windows call the existing paint event when needed. To do this from a button
Click event handler, simply call Label.Invalidate from your button click
event.

Something like the following:

Private Sub Button1_Click(...

lblP1JoyUp.Invalidate()

End Sub

This will cause your label control to be repainted.

There are three interrelated methods on having a control repaint itself.
Invalidate, Update, and Refresh.

' request that the object repaint itself
lblP1JoyUp.Invalidate()

' cause the object to repaint itself right now!
lblP1JoyUp.Update()

' request that the object repaint itself, and do it right now!
lblP1JoyUp.Refresh()

If you simply call Invalidate, the Paint event of the control will be called
when all other events (such as your button click) have finished processing.
Update causes the Paint event to be called directly, while Refresh
effectively does an Invalidate followed by Refresh. Most of the time calling
Invalidate is all you really should do, otherwise you may wind up repainting
the control multiple times...

Hope this helps
Jay


jcrouse said:
I can't give up my standard label controls. I have too much code going on
with them. I have mouse ups, mouse downs, click events, mouse moves and all
kinds of settable properties. They are part of a form designer and are
movable at runtime. also, the font, forecolor, backcolor and size are all
settable with menus. It just seems like too much to change.

John

Herfried K. Wagner said:
* "jcrouse said:
I am rotating some text is some label controls. In the one place I use it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click event, or
better yet, maybe inside a sub of its own. I need to call the above
code
way
 
J

jcrouse

That helps a lot. I know my code is nowhere near as clean and efficient as
it should be. I guess that's part of the learning curve. We tend to use what
we are comfortable with. I have been looking for a few good reads. I'll pick
up the one you recommended and start there.

Thank you,

John

Jay B. Harlow said:
John,
When you derive from Label the control you create is a standard label, which
means you continue to use the same mouse ups, mouse downs, click events,
mouse moves and all kinds of the same settable properties.

You continue using the same form designer to set the same properties, the
only difference is, you new Label now has 1 more property an Angle
property...

I too would recommend you derive from Label and add an Angle Property, as
once you learn how I suspect your forms will suddenly become simpler and
easier to create, understand, and maintain later.

For a good intro explanation of OO in VB.NET I would recommend Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

To answer your original question you do not really want to duplicate the
paint logic, as this is highly error prone. Instead you simply want to have
windows call the existing paint event when needed. To do this from a button
Click event handler, simply call Label.Invalidate from your button click
event.

Something like the following:

Private Sub Button1_Click(...

lblP1JoyUp.Invalidate()

End Sub

This will cause your label control to be repainted.

There are three interrelated methods on having a control repaint itself.
Invalidate, Update, and Refresh.

' request that the object repaint itself
lblP1JoyUp.Invalidate()

' cause the object to repaint itself right now!
lblP1JoyUp.Update()

' request that the object repaint itself, and do it right now!
lblP1JoyUp.Refresh()

If you simply call Invalidate, the Paint event of the control will be called
when all other events (such as your button click) have finished processing.
Update causes the Paint event to be called directly, while Refresh
effectively does an Invalidate followed by Refresh. Most of the time calling
Invalidate is all you really should do, otherwise you may wind up repainting
the control multiple times...

Hope this helps
Jay


jcrouse said:
I can't give up my standard label controls. I have too much code going on
with them. I have mouse ups, mouse downs, click events, mouse moves and all
kinds of settable properties. They are part of a form designer and are
movable at runtime. also, the font, forecolor, backcolor and size are all
settable with menus. It just seems like too much to change.

John

Herfried K. Wagner said:
* "jcrouse" <me> scripsit:
I am rotating some text is some label controls. In the one place I
use
it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click
event,
or
better yet, maybe inside a sub of its own. I need to call the above
code
for
30 label controls so having it in it's own sub would probably be the
way
to
go, right?

I would derive a class from 'System.Windows.Forms.Label' and override
its 'OnPaint' sub. Then I would add a property 'Angle' that determines
the angle in which the text should be drawn. In the setter for this
property you can call 'Me.Invalidate' to redraw the text in the right
angle. Then you can use this control instead of the standard label
control.
 
J

jcrouse

Well, the problem is this. The control paint event seems to fire on visible
change. I don't want that. My label is draggable at runtime and when I drag
it across the form, the paint event seems to run continually until I stop
dragging the control. The makes the drag very slow and choppy. I want to
only call the paint event when needed, like inside my mouse up or mouse move
event.

Thanks,

John

Jay B. Harlow said:
John,
When you derive from Label the control you create is a standard label, which
means you continue to use the same mouse ups, mouse downs, click events,
mouse moves and all kinds of the same settable properties.

You continue using the same form designer to set the same properties, the
only difference is, you new Label now has 1 more property an Angle
property...

I too would recommend you derive from Label and add an Angle Property, as
once you learn how I suspect your forms will suddenly become simpler and
easier to create, understand, and maintain later.

For a good intro explanation of OO in VB.NET I would recommend Robin A.
Reynolds-Haertle's book "OOP with Microsoft Visual Basic .NET and Microsoft
Visual C# .NET - Step by Step" from MS Press.

To answer your original question you do not really want to duplicate the
paint logic, as this is highly error prone. Instead you simply want to have
windows call the existing paint event when needed. To do this from a button
Click event handler, simply call Label.Invalidate from your button click
event.

Something like the following:

Private Sub Button1_Click(...

lblP1JoyUp.Invalidate()

End Sub

This will cause your label control to be repainted.

There are three interrelated methods on having a control repaint itself.
Invalidate, Update, and Refresh.

' request that the object repaint itself
lblP1JoyUp.Invalidate()

' cause the object to repaint itself right now!
lblP1JoyUp.Update()

' request that the object repaint itself, and do it right now!
lblP1JoyUp.Refresh()

If you simply call Invalidate, the Paint event of the control will be called
when all other events (such as your button click) have finished processing.
Update causes the Paint event to be called directly, while Refresh
effectively does an Invalidate followed by Refresh. Most of the time calling
Invalidate is all you really should do, otherwise you may wind up repainting
the control multiple times...

Hope this helps
Jay


jcrouse said:
I can't give up my standard label controls. I have too much code going on
with them. I have mouse ups, mouse downs, click events, mouse moves and all
kinds of settable properties. They are part of a form designer and are
movable at runtime. also, the font, forecolor, backcolor and size are all
settable with menus. It just seems like too much to change.

John

Herfried K. Wagner said:
* "jcrouse" <me> scripsit:
I am rotating some text is some label controls. In the one place I
use
it it
works fine. In the other place I use it I can't figure out the syntax.
[...]
However, I now also need to place the same type code under a control event
such as a button click event. I am struggling trying to figure out the
syntax, mostly the "ByVal e" part and the handler. What would the proper
syntax be to either place the above syntax under a button click
event,
or
better yet, maybe inside a sub of its own. I need to call the above
code
for
30 label controls so having it in it's own sub would probably be the
way
to
go, right?

I would derive a class from 'System.Windows.Forms.Label' and override
its 'OnPaint' sub. Then I would add a property 'Angle' that determines
the angle in which the text should be drawn. In the setter for this
property you can call 'Me.Invalidate' to redraw the text in the right
angle. Then you can use this control instead of the standard label
control.
 

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