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

label control text rotation (paint) revisited.

P: n/a
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.ClientSi ze.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



Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2

P: n/a
That's way outside of my capabilities. I don't understand.

John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #3

P: n/a
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 [MVP]" <hi***************@gmx.at> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4

P: n/a
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" <me> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [MVP]" <hi***************@gmx.at> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 20 '05 #5

P: n/a
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
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" <me> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [MVP]" <hi***************@gmx.at> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 20 '05 #6

P: n/a
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
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" <me> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [MVP]" <hi***************@gmx.at> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
* "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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.