I have a program that is an external viewer. It is launched with a hotkey
from within another application. The program has 30 labels that can be
displayed. Since the labels may not always be oriented the same as the
Operating System I needed to use the Label_Paint event to PAINT the labels
text if it not oriented the same as the operating system. I am trying to
figure out a way to make this event happen faster or optimize it if
possible. I am new to this code. I think the idea was Herfreid's, can't even
remember. Here is the code:
Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim strLabelName As String
Select Case myPLabel.Name
Case "lblP1JoyUp"
strLabelName = Label1.Text
Case "lblP1JoyLeft"
strLabelName = Label2.Text
Case "lblP1JoyDown"
strLabelName = Label3.Text
Case "lblP1JoyRight"
strLabelName = Label4.Text
Case "lblP1B1"
strLabelName = Label5.Text
Case "lblP1B2"
strLabelName = Label6.Text
Case "lblP1B3"
strLabelName = Label7.Text
Case "lblP1B4"
strLabelName = Label8.Text
Case "lblP1B5"
strLabelName = Label9.Text
Case "lblP1B6"
strLabelName = Label10.Text
Case "lblP1B7"
strLabelName = Label11.Text
Case "lblP1B8"
strLabelName = Label12.Text
Case "lblP1JoyType"
strLabelName = Label25.Text
Case "lblGameName"
strLabelName = Label27.Text
Case "lblNumPlayers"
strLabelName = Label28.Text
Case "lblHistory"
strLabelName = Label29.Text
Case "lblUndocumented"
strLabelName = Label30.Text
End Select
If myPLabel.Visible = True Then
Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)
If frm1.intPriHP1 = frm1.intPriVP1 Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width
+ x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width,
myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
lblP1JoyUp.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width -
x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
End If
ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then
If strGameO = "h" Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2,
(myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width,
myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2,
(myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
End If
ElseIf strGameO = "v" Then
If strLay2RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay2RotAngle = "90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2,
(myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
ElseIf strLay2RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width,
myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay2RotAngle = "-90" Then
Dim y As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer =
CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2,
(myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font,
myFontBrush, -y, 0)
End If
End If
End If
myFontBrush.Dispose()
End If
End Sub
Any ides how I might speed this up? Currently, if then user is displaying
all 30 labels I need am calling this code 30 times.
Thnaks,
John 5 1285
John,
If your call your code 30 times in a loop or 1 time one by one, will not
speed up your code, it makes only your program easier to maintanance.
It is possible that Herfried has pointed you on the invalidate method http://msdn.microsoft.com/library/de...idatetopic.asp
However from that not sure because I am not really a painter.
Just my thought,
Cor
John,
The paint event should be doing as little as possible.
How is myPaint1 sub being called?
How is myPLabel being set?
Based on what you posted I would make the following recommendations:
1. Use variables of the correct type (I would expect strLay1RotAngle &
strLay2RotAngle would be integers not strings).
2. Seriously (very seriously) consider using a class derived from Label
instead of all the code in your form.
For example rather then having 30 Labels on my form I would have 30
SpecializedLabels on my form. Where SpecializedLabel inherits from Label,
and has properties, methods & event handlers specific to each label.
Within the SpecializedLabel class I would override the OnPaint method to
handle painting that specific label. I would have properties on
SpecializedLabel to handle the rotation & what to display.
I would use the ControlStyles.UserPaint to handle all the painting myself,
then simply use the Text property for what to paint (rather then the select
case myPLabel.Name). Alternatively I would have a property for LabelName to
display...
I will try to post a start of a sample later today.
Hope this helps
Jay
"jcrouse" <me> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl... I have a program that is an external viewer. It is launched with a hotkey from within another application. The program has 30 labels that can be displayed. Since the labels may not always be oriented the same as the Operating System I needed to use the Label_Paint event to PAINT the labels text if it not oriented the same as the operating system. I am trying to figure out a way to make this event happen faster or optimize it if possible. I am new to this code. I think the idea was Herfreid's, can't
even remember. Here is the code:
Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs) Dim strLabelName As String
Select Case myPLabel.Name
Case "lblP1JoyUp"
strLabelName = Label1.Text
Case "lblP1JoyLeft"
strLabelName = Label2.Text
Case "lblP1JoyDown"
strLabelName = Label3.Text
Case "lblP1JoyRight"
strLabelName = Label4.Text
Case "lblP1B1"
strLabelName = Label5.Text
Case "lblP1B2"
strLabelName = Label6.Text
Case "lblP1B3"
strLabelName = Label7.Text
Case "lblP1B4"
strLabelName = Label8.Text
Case "lblP1B5"
strLabelName = Label9.Text
Case "lblP1B6"
strLabelName = Label10.Text
Case "lblP1B7"
strLabelName = Label11.Text
Case "lblP1B8"
strLabelName = Label12.Text
Case "lblP1JoyType"
strLabelName = Label25.Text
Case "lblGameName"
strLabelName = Label27.Text
Case "lblNumPlayers"
strLabelName = Label28.Text
Case "lblHistory"
strLabelName = Label29.Text
Case "lblUndocumented"
strLabelName = Label30.Text
End Select
If myPLabel.Visible = True Then
Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)
If frm1.intPriHP1 = frm1.intPriVP1 Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
lblP1JoyUp.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then
If strGameO = "h" Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf strGameO = "v" Then
If strLay2RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay2RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay2RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay2RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
End If
End If
myFontBrush.Dispose()
End If
End Sub Any ides how I might speed this up? Currently, if then user is displaying all 30 labels I need am calling this code 30 times.
Thnaks, John
The myPaint1 sub is being called 30 times. Once from each Label_Paint event.
I agree about the user defined controls. You guys suggested that a month or
two ago, however, it is over my head and I'm still trying to figure out how
to do it. Here is one of my Label_Paint events:
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
myPLabel = lblP1JoyUp
myPaint1(e)
End Sub
Thanks,
John
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Ol**************@TK2MSFTNGP10.phx.gbl... John, The paint event should be doing as little as possible.
How is myPaint1 sub being called? How is myPLabel being set?
Based on what you posted I would make the following recommendations: 1. Use variables of the correct type (I would expect strLay1RotAngle & strLay2RotAngle would be integers not strings). 2. Seriously (very seriously) consider using a class derived from Label instead of all the code in your form.
For example rather then having 30 Labels on my form I would have 30 SpecializedLabels on my form. Where SpecializedLabel inherits from Label, and has properties, methods & event handlers specific to each label.
Within the SpecializedLabel class I would override the OnPaint method to handle painting that specific label. I would have properties on SpecializedLabel to handle the rotation & what to display.
I would use the ControlStyles.UserPaint to handle all the painting myself, then simply use the Text property for what to paint (rather then the
select case myPLabel.Name). Alternatively I would have a property for LabelName
to display...
I will try to post a start of a sample later today.
Hope this helps Jay
"jcrouse" <me> wrote in message news:Oz**************@TK2MSFTNGP09.phx.gbl... I have a program that is an external viewer. It is launched with a
hotkey from within another application. The program has 30 labels that can be displayed. Since the labels may not always be oriented the same as the Operating System I needed to use the Label_Paint event to PAINT the
labels text if it not oriented the same as the operating system. I am trying to figure out a way to make this event happen faster or optimize it if possible. I am new to this code. I think the idea was Herfreid's, can't even remember. Here is the code:
Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs) Dim strLabelName As String
Select Case myPLabel.Name
Case "lblP1JoyUp"
strLabelName = Label1.Text
Case "lblP1JoyLeft"
strLabelName = Label2.Text
Case "lblP1JoyDown"
strLabelName = Label3.Text
Case "lblP1JoyRight"
strLabelName = Label4.Text
Case "lblP1B1"
strLabelName = Label5.Text
Case "lblP1B2"
strLabelName = Label6.Text
Case "lblP1B3"
strLabelName = Label7.Text
Case "lblP1B4"
strLabelName = Label8.Text
Case "lblP1B5"
strLabelName = Label9.Text
Case "lblP1B6"
strLabelName = Label10.Text
Case "lblP1B7"
strLabelName = Label11.Text
Case "lblP1B8"
strLabelName = Label12.Text
Case "lblP1JoyType"
strLabelName = Label25.Text
Case "lblGameName"
strLabelName = Label27.Text
Case "lblNumPlayers"
strLabelName = Label28.Text
Case "lblHistory"
strLabelName = Label29.Text
Case "lblUndocumented"
strLabelName = Label30.Text
End Select
If myPLabel.Visible = True Then
Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)
If frm1.intPriHP1 = frm1.intPriVP1 Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
lblP1JoyUp.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then
If strGameO = "h" Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf strGameO = "v" Then
If strLay2RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay2RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay2RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay2RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName,
myPLabel.Font, myFontBrush, -y, 0)
End If
End If
End If
myFontBrush.Dispose()
End If
End Sub Any ides how I might speed this up? Currently, if then user is
displaying all 30 labels I need am calling this code 30 times.
Thnaks, John
I'm sure here I could have also use some sort of DirectCast(sender, Label)
but don't really understand that syntax either. I'll get there eventually.
It's just a slow process.
Thanks,
John
"jcrouse" <me> wrote in message
news:ea**************@TK2MSFTNGP15.phx.gbl... The myPaint1 sub is being called 30 times. Once from each Label_Paint
event. I agree about the user defined controls. You guys suggested that a month
or two ago, however, it is over my head and I'm still trying to figure out
how to do it. Here is one of my Label_Paint events:
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
myPLabel = lblP1JoyUp
myPaint1(e)
End Sub Thanks, John
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Ol**************@TK2MSFTNGP10.phx.gbl... John, The paint event should be doing as little as possible.
How is myPaint1 sub being called? How is myPLabel being set?
Based on what you posted I would make the following recommendations: 1. Use variables of the correct type (I would expect strLay1RotAngle & strLay2RotAngle would be integers not strings). 2. Seriously (very seriously) consider using a class derived from Label instead of all the code in your form.
For example rather then having 30 Labels on my form I would have 30 SpecializedLabels on my form. Where SpecializedLabel inherits from
Label, and has properties, methods & event handlers specific to each label.
Within the SpecializedLabel class I would override the OnPaint method to handle painting that specific label. I would have properties on SpecializedLabel to handle the rotation & what to display.
I would use the ControlStyles.UserPaint to handle all the painting
myself, then simply use the Text property for what to paint (rather then the select case myPLabel.Name). Alternatively I would have a property for LabelName to display...
I will try to post a start of a sample later today.
Hope this helps Jay
"jcrouse" <me> wrote in message news:Oz**************@TK2MSFTNGP09.phx.gbl... I have a program that is an external viewer. It is launched with a hotkey from within another application. The program has 30 labels that can be displayed. Since the labels may not always be oriented the same as the Operating System I needed to use the Label_Paint event to PAINT the labels text if it not oriented the same as the operating system. I am trying
to figure out a way to make this event happen faster or optimize it if possible. I am new to this code. I think the idea was Herfreid's,
can't even remember. Here is the code:
Public Sub myPaint1(ByVal e As
System.Windows.Forms.PaintEventArgs) Dim strLabelName As String
Select Case myPLabel.Name
Case "lblP1JoyUp"
strLabelName = Label1.Text
Case "lblP1JoyLeft"
strLabelName = Label2.Text
Case "lblP1JoyDown"
strLabelName = Label3.Text
Case "lblP1JoyRight"
strLabelName = Label4.Text
Case "lblP1B1"
strLabelName = Label5.Text
Case "lblP1B2"
strLabelName = Label6.Text
Case "lblP1B3"
strLabelName = Label7.Text
Case "lblP1B4"
strLabelName = Label8.Text
Case "lblP1B5"
strLabelName = Label9.Text
Case "lblP1B6"
strLabelName = Label10.Text
Case "lblP1B7"
strLabelName = Label11.Text
Case "lblP1B8"
strLabelName = Label12.Text
Case "lblP1JoyType"
strLabelName = Label25.Text
Case "lblGameName"
strLabelName = Label27.Text
Case "lblNumPlayers"
strLabelName = Label28.Text
Case "lblHistory"
strLabelName = Label29.Text
Case "lblUndocumented"
strLabelName = Label30.Text
End Select
If myPLabel.Visible = True Then
Dim myFontBrush As New SolidBrush(myPLabel.ForeColor)
If frm1.intPriHP1 = frm1.intPriVP1 Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
lblP1JoyUp.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then
If strGameO = "h" Then
If strLay1RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay1RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay1RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay1RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
ElseIf strGameO = "v" Then
If strLay2RotAngle = "0" Then
myPLabel.Text = strLabelName
ElseIf strLay2RotAngle = "90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
ElseIf strLay2RotAngle = "180" Then
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
myPLabel.Text = ""
e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, myPLabel.ClientSize.Height)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf)
ElseIf strLay2RotAngle = "-90" Then
Dim y As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width)
Dim x As Integer = CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height)
myPLabel.Text = ""
e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, (myPLabel.ClientSize.Height - y) \ 2)
e.Graphics.RotateTransform(270)
e.Graphics.DrawString(strLabelName, myPLabel.Font, myFontBrush, -y, 0)
End If
End If
End If
myFontBrush.Dispose()
End If
End Sub Any ides how I might speed this up? Currently, if then user is displaying all 30 labels I need am calling this code 30 times.
Thnaks, John
John,
Unfortunately I do not have time to come up with a meaningful sample for
you.
You can shorten the following to: Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
myPLabel = DirectCast(sender, Label)
myPaint1(e)
End Sub
If you do the above all 30 times, then you can simply reduce it to once,
with 30 controls on the Handles.
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint, lblP2JoyUp.Paint...
myPLabel = DirectCast(sender, Label)
myPaint1(e)
End Sub
I would recommend you start with Robin A. Reynolds-Haertle's book "OOP -
with Microsoft Visual Basic .NET and Microsoft Visual C# .NET - Step by
Step" from MS Press. As its a good book on the how of OO. Taking an OO
approach with your app should significantly reduce the amount of code you
have, which should also cause it to run quicker (you would not be jumping
through hopes to get to select case statements to get back to the control
that you started with.
Hope this helps
Jay
"jcrouse" <me> wrote in message
news:ea**************@TK2MSFTNGP15.phx.gbl... The myPaint1 sub is being called 30 times. Once from each Label_Paint event. I agree about the user defined controls. You guys suggested that a month or two ago, however, it is over my head and I'm still trying to figure out how to do it. Here is one of my Label_Paint events:
Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles lblP1JoyUp.Paint
myPLabel = lblP1JoyUp
myPaint1(e)
End Sub Thanks, John
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Ol**************@TK2MSFTNGP10.phx.gbl... John, The paint event should be doing as little as possible.
How is myPaint1 sub being called? How is myPLabel being set?
Based on what you posted I would make the following recommendations: 1. Use variables of the correct type (I would expect strLay1RotAngle & strLay2RotAngle would be integers not strings). 2. Seriously (very seriously) consider using a class derived from Label instead of all the code in your form.
For example rather then having 30 Labels on my form I would have 30 SpecializedLabels on my form. Where SpecializedLabel inherits from Label, and has properties, methods & event handlers specific to each label.
Within the SpecializedLabel class I would override the OnPaint method to handle painting that specific label. I would have properties on SpecializedLabel to handle the rotation & what to display.
I would use the ControlStyles.UserPaint to handle all the painting myself, then simply use the Text property for what to paint (rather then the select case myPLabel.Name). Alternatively I would have a property for LabelName to display...
I will try to post a start of a sample later today.
Hope this helps Jay
"jcrouse" <me> wrote in message news:Oz**************@TK2MSFTNGP09.phx.gbl... > I have a program that is an external viewer. It is launched with a hotkey > from within another application. The program has 30 labels that can be > displayed. Since the labels may not always be oriented the same as the > Operating System I needed to use the Label_Paint event to PAINT the labels > text if it not oriented the same as the operating system. I am trying > to > figure out a way to make this event happen faster or optimize it if > possible. I am new to this code. I think the idea was Herfreid's, can't even > remember. Here is the code: > > Public Sub myPaint1(ByVal e As System.Windows.Forms.PaintEventArgs) > Dim strLabelName As String > > Select Case myPLabel.Name > > Case "lblP1JoyUp" > > strLabelName = Label1.Text > > Case "lblP1JoyLeft" > > strLabelName = Label2.Text > > Case "lblP1JoyDown" > > strLabelName = Label3.Text > > Case "lblP1JoyRight" > > strLabelName = Label4.Text > > Case "lblP1B1" > > strLabelName = Label5.Text > > Case "lblP1B2" > > strLabelName = Label6.Text > > Case "lblP1B3" > > strLabelName = Label7.Text > > Case "lblP1B4" > > strLabelName = Label8.Text > > Case "lblP1B5" > > strLabelName = Label9.Text > > Case "lblP1B6" > > strLabelName = Label10.Text > > Case "lblP1B7" > > strLabelName = Label11.Text > > Case "lblP1B8" > > strLabelName = Label12.Text > > Case "lblP1JoyType" > > strLabelName = Label25.Text > > Case "lblGameName" > > strLabelName = Label27.Text > > Case "lblNumPlayers" > > strLabelName = Label28.Text > > Case "lblHistory" > > strLabelName = Label29.Text > > Case "lblUndocumented" > > strLabelName = Label30.Text > > End Select > > If myPLabel.Visible = True Then > > Dim myFontBrush As New SolidBrush(myPLabel.ForeColor) > > If frm1.intPriHP1 = frm1.intPriVP1 Then > > If strLay1RotAngle = "0" Then > > myPLabel.Text = strLabelName > > ElseIf strLay1RotAngle = "90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > myPLabel.Text = "" > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width > + x) \ 2, (myPLabel.ClientSize.Height + y) \ 2) > > e.Graphics.RotateTransform(90) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > ElseIf strLay1RotAngle = "180" Then > > Dim sf As New StringFormat > > sf.Alignment = StringAlignment.Center > > sf.LineAlignment = StringAlignment.Center > > myPLabel.Text = "" > > e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, > myPLabel.ClientSize.Height) > > e.Graphics.RotateTransform(180) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf) > > ElseIf strLay1RotAngle = "-90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > lblP1JoyUp.Text = "" > > > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - > x) \ 2, (myPLabel.ClientSize.Height - y) \ 2) > > e.Graphics.RotateTransform(270) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > End If > > ElseIf frm1.intPriHP1 <> frm1.intPriVP1 Then > > If strGameO = "h" Then > > If strLay1RotAngle = "0" Then > > myPLabel.Text = strLabelName > > ElseIf strLay1RotAngle = "90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, > (myPLabel.ClientSize.Height + y) \ 2) > > e.Graphics.RotateTransform(90) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > ElseIf strLay1RotAngle = "180" Then > > Dim sf As New StringFormat > > sf.Alignment = StringAlignment.Center > > sf.LineAlignment = StringAlignment.Center > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, > myPLabel.ClientSize.Height) > > e.Graphics.RotateTransform(180) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf) > > ElseIf strLay1RotAngle = "-90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, > (myPLabel.ClientSize.Height - y) \ 2) > > e.Graphics.RotateTransform(270) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > End If > > ElseIf strGameO = "v" Then > > If strLay2RotAngle = "0" Then > > myPLabel.Text = strLabelName > > ElseIf strLay2RotAngle = "90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width + x) \ 2, > (myPLabel.ClientSize.Height + y) \ 2) > > e.Graphics.RotateTransform(90) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > ElseIf strLay2RotAngle = "180" Then > > Dim sf As New StringFormat > > sf.Alignment = StringAlignment.Center > > sf.LineAlignment = StringAlignment.Center > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform(myPLabel.ClientSize. Width, > myPLabel.ClientSize.Height) > > e.Graphics.RotateTransform(180) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, RectangleF.op_Implicit(myPLabel.ClientRectangle), sf) > > ElseIf strLay2RotAngle = "-90" Then > > Dim y As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Width) > > Dim x As Integer = > CInt(e.Graphics.MeasureString(strLabelName, myPLabel.Font).Height) > > myPLabel.Text = "" > > > e.Graphics.TranslateTransform((myPLabel.ClientSize .Width - x) \ 2, > (myPLabel.ClientSize.Height - y) \ 2) > > e.Graphics.RotateTransform(270) > > e.Graphics.DrawString(strLabelName, myPLabel.Font, > myFontBrush, -y, 0) > > End If > > End If > > End If > > myFontBrush.Dispose() > > End If > > End Sub > > > > Any ides how I might speed this up? Currently, if then user is displaying > all 30 labels I need am calling this code 30 times. > > Thnaks, > John > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Snyke |
last post by:
Hi.
I have a command line script which works really fine, the only problem
is that it take *really* long for the first output to be printed on
screen.
Since I also get some HTTP headers I'm...
|
by: Apollo |
last post by:
I have about 20K records that result from the following query. Front end for the
database is ACCESS97 and pulling up 20K records makes a huge performance hit.
For the form in question I am using...
|
by: dvumani |
last post by:
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the...
|
by: mfyahya |
last post by:
Hi,
I'm new to databases :) I need help speeding up select queries on my
data which are currently taking 4-5 seconds. I set up a single large
table of coordinates data with an index on the fields...
|
by: Robert Wilkens |
last post by:
Ok... This may be the wrong forum, but it's the first place I'm trying.
I'm new to C# and just implemented the 3-tier Distributed application from
Chapter 1 (the first walkthrough) in the...
|
by: OHM |
last post by:
I was wondering about this topic and although I accept that different
situations call for different solutions, but wondered are there any other
solutions and whether has anyone carried out a...
|
by: jcrouse |
last post by:
I am having problems with a Label_Paint event causing a continuous loop.
Here is an explanation of the code.
I right click on a label and a context menu pops up.
I then select a menu...
|
by: RobinAG |
last post by:
Hello,
I just split my database into front and back end. My front end users are experiencing really slow opening of forms. I've searched online for help speeding up forms, but I'm not sure what...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |