label1.Location coordinates not being reported correcly in dynamic form.

  • Thread starter thomasamillergoogle
  • Start date
T

thomasamillergoogle

I have a MainForm with a Panel1. My project also has a UserControl1.
When a nav button is clicked in the MainForm the Usercontrol1 is loaded
into the MainForm.Panel1. All that is working beautifully.

I went into the user controls and added a label1 and a button1. When
the button1 is clicked the I put a breakpoint on Label1 and the
Label1.Location property is set to {1,1} which is obviously wrong - it
should be {333,500}.

Is there something I can override so I can get the location of the
Label1 after the usercontrol has been added to the MainForm.Panel1 ?
What am I doing wrong?

P.S. I kind of over simplified things, I have a bunch of panels that
are being added dynamically to eacy other, it is hard to explain
neatly.
 
L

Lee Gillie

I have a MainForm with a Panel1. My project also has a UserControl1.
When a nav button is clicked in the MainForm the Usercontrol1 is loaded
into the MainForm.Panel1. All that is working beautifully.

I went into the user controls and added a label1 and a button1. When
the button1 is clicked the I put a breakpoint on Label1 and the
Label1.Location property is set to {1,1} which is obviously wrong - it
should be {333,500}.

Is there something I can override so I can get the location of the
Label1 after the usercontrol has been added to the MainForm.Panel1 ?
What am I doing wrong?

P.S. I kind of over simplified things, I have a bunch of panels that
are being added dynamically to eacy other, it is hard to explain
neatly.

When do you breakpoint? New? Load? somewhere else?

If you want an event after everything is visible (and sometimes it is
necessary, and sometimes it just allows you to delay some lengthy
initialization work to a point after the form is visible) I do something
like this:

Dim blnInitialized as boolean

Sub Form_Load
blnInitialized = False
End Sub

Sub Form_Activated
If blnInitialized Then Return
blnInitialized = True

... do your 1-time stuff here, form is visible now

End Sub

Also, don't you have to add panel location to control location to get
location of control within the form? Maybe you have this part worked
out, it was not clear to me.

- Lee
 
B

Bob Powell [MVP]

The location of a control is relative to it's parent. This user control is
obviously parented to a panel within the form.

You can find the screen location of the control by using
theControl.PointToScreen and teh find it's position relative to the form by
using theForm.PointToClient.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

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