Find Parent Control of Control.

G

Guest

Some one help me out.

I have a windows form.I added Panel to a windows form.
I put a textbox in a Panel.

When ever I try to get Parent of textbox. its always returns me the handle of Form not the panel.

Is there any way to get the Parent Control of a control on the Form?
Am I missing anything?

Thanks

dinoo
 
T

Tom Dacon

So I started up a new VB Windows Forms project, put a panel on the form and
a textbox inside it, put the following code in the textbox's click handler,
and it worked just fine. Printed out

"TextBox1's container: Panel1"

just as expected.

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Click

Dim ctl As TextBox = CType(sender, TextBox)
Debug.WriteLine(ctl.Name + "'s container: " + ctl.Parent.Name)

End Sub

I can't say what's happening in your case, but this test worked just as it
should.

Good luck,
Tom Dacon
Dacon Software Consulting
 
C

Cor Ligthert

Dinoo,

In addition to Tom

MessageBox.Show(TextBox1.Parent.Parent.Name)
Gives for me "Form1"
MessageBox.Show(TextBox1.Parent.Name)
Gives for me "Panel1"

I hope this helps?

Cor
 

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