Controls.Add Requires MsgBox

E

eBob.com

Sorry about the cryptic Subject, it was the best I could do. But it does
describe my situation. I have a Controls.Add which works only if a MsgBox
is executed. Here's the code:

Sub HoverPopUp_SampleText(ByVal sender As SampleText)
MsgBox("popup entered") ' !! this is REQUIRED for the Controls.Add
below to work !!
Dim poploc As New Point(sender.LastMouseLoc.X + _
sender.rtbText.Location.X + sender.Location.X, _
sender.LastMouseLoc.Y + sender.rtbText.Location.Y + _
sender.Location.Y + 300)
sender.tbxHover.Location = poploc
'sender.tbxHover.Location = New Point(0, 0)
Me.Controls.Add(sender.tbxHover) ' !! doesn't work without MsgBox
above
sender.tbxHover.BringToFront()
End Sub

I have a SampleText class which consists of a RichTextBox and 6 buttons.
The class contains a MouseHover event handler for the RichTextBox. I'd like
the MouseHover event to popup a TextBox containing some information, but if
I did that in the SampleText class's MouseHover event handler it would have
to place the TextBox on the usercontrol which is not where I want it placed.
So, instead, the MouseHover event handler puts the information together in a
TextBox (tbxHover) and Raises the event which the code above handles. So
all the code above has to do is determine the location for the TextBox, do
the Controls.Add, and, for good measure, do a BringToFront.

With the MsgBox the code works as I'd expect. Without the MsgBox I never
see the TextBox (tbxHover)! Does anyone have any idea what could be going
on here?!?!?!?!

Thanks, Blob
 
J

Jack Jackson

Sorry about the cryptic Subject, it was the best I could do. But it does
describe my situation. I have a Controls.Add which works only if a MsgBox
is executed. Here's the code:

Sub HoverPopUp_SampleText(ByVal sender As SampleText)
MsgBox("popup entered") ' !! this is REQUIRED for the Controls.Add
below to work !!
Dim poploc As New Point(sender.LastMouseLoc.X + _
sender.rtbText.Location.X + sender.Location.X, _
sender.LastMouseLoc.Y + sender.rtbText.Location.Y + _
sender.Location.Y + 300)
sender.tbxHover.Location = poploc
'sender.tbxHover.Location = New Point(0, 0)
Me.Controls.Add(sender.tbxHover) ' !! doesn't work without MsgBox
above
sender.tbxHover.BringToFront()
End Sub

I have a SampleText class which consists of a RichTextBox and 6 buttons.
The class contains a MouseHover event handler for the RichTextBox. I'd like
the MouseHover event to popup a TextBox containing some information, but if
I did that in the SampleText class's MouseHover event handler it would have
to place the TextBox on the usercontrol which is not where I want it placed.
So, instead, the MouseHover event handler puts the information together in a
TextBox (tbxHover) and Raises the event which the code above handles. So
all the code above has to do is determine the location for the TextBox, do
the Controls.Add, and, for good measure, do a BringToFront.

With the MsgBox the code works as I'd expect. Without the MsgBox I never
see the TextBox (tbxHover)! Does anyone have any idea what could be going
on here?!?!?!?!

Thanks, Blob

I think you are going to have to break down and debug this. My guess
is that rather than the controls.Add "not working", the added
textbox's X or Y value puts it somewhere that makes it not visible. I
would look at poploc to see if it makes sense, and also check to see
if txbHover's Location gets changed by the Add.

Is it possible that when you display the MessageBox, the interaction
with it modifies sender.LastMouseLoc?
 
E

eBob.com

Hi Jack,

Thank you for your interest and response. I had used the debugger and had
determined that, even without the MsgBox, the control was getting added and
at a location where it would be visible. But your response got me more
focused on the MsgBox and I did some more experimenting. None of which
helped, BUT it eventually occurred to me that MsgBox brings my application
to a screaching halt. So I commented out the code which removes my popup,
and, lo and behold, there it was. I was using the MouseMove event to
trigger the removal, and apparently that event fires so quickly after
MouseHover that the popup was getting removed before it ever became visible.

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