Label_Paint event causing a continuous loop. Help needed.

  • Thread starter Thread starter jcrouse
  • Start date Start date
J

jcrouse

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 item named "cmLSolidColor". Here is its code:

Private Sub cmLSolidColor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmLSolidColor.Click

LabelBackColorSolid()

End Sub



It calls a sub named "LabelBackColorSolid". Here is its code:

Private Sub LabelBackColorSolid()

If ColorDialog1.ShowDialog() = DialogResult.OK Then

If bCMTrigger = True Then

If strCMTrigger = "lblP1JoyUp" Then

lblP1JoyUp.BackColor = (ColorDialog1.Color)

ElseIf strCMTrigger = "lblP1JoyRight" Then

lblP1JoyRight.BackColor = (ColorDialog1.Color)

End if

End if

End if

End Sub



The problem is that the label I'm right clicking on has a Label_Paint event.
Execution goes to the line "LabelBackColorSolid" in the cmlSolidColor_Click
event properly. Then It enters the Private Sub, "LabelBackColorSolid". When
it attempts to read the first line, "If ColorDialog1..." and launch the
dialog box, It immediately triggers the Label_Paint event and enter a
continuous loop. If I pause it in the debugger and then continue the loop
ends, the dialog box pops up, I pick my color, it applies it and all is
well. What can I do to stop this behavior. Is there a way to force it now to
do the Label_Paint event at this point, say, until the end of the
"LabelBackColorSolid" sub is complete?



Thank you,

John
 
John,

What is the code in your label paint event? I cannot see how the continuous
loop is happening from what you posted.

This will temporarily turn off the paint event, but something seems amiss to
have to do this...

Private Sub LabelBackColorSolid()
RemoveHandler Label1.Paint, AddressOf Label1_Paint ' Stop handling
events.

If ColorDialog1.ShowDialog() = DialogResult.OK Then
' do stuff
End If

AddHandler Label1.Paint, AddressOf Label1_Paint
End Sub

HTH,
Greg
 
Add a module level Boolean to state that you are in the
LabelBackColorSolid function. Check this variable in the Label_Paint
event. If set exit sub.

HTH

David
 
Here is the OnPaint 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 mnuHLabel.Checked = True Then

If bFlipText = False Then

lblP1JoyUp.Text = ""

ElseIf bFlipText = True Then

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center

sf.LineAlignment = StringAlignment.Center

lblP1JoyUp.Text = ""


e.Graphics.TranslateTransform(lblP1JoyUp.ClientSize.Width,
lblP1JoyUp.ClientSize.Height)

e.Graphics.RotateTransform(180)

e.Graphics.DrawString("P1JoyUp", lblP1JoyUp.Font,
myFontBrush, RectangleF.op_Implicit(lblP1JoyUp.ClientRectangle), sf)

End If

ElseIf mnuVLabel.Checked = True Then

If bFlipText = False Then

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

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

lblP1JoyUp.Text = ""


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

e.Graphics.RotateTransform(270)

e.Graphics.DrawString("P1JoyUp", lblP1JoyUp.Font,
myFontBrush, -y, 0)

ElseIf bFlipText = True Then

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

Dim x As Integer =
CInt(e.Graphics.MeasureString("lblP1JoyUp", 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("P1JoyUp", lblP1JoyUp.Font,
myFontBrush, -y, 0)

End If

End If

myFontBrush.Dispose()

End If

End Sub
 
Back
Top