Finding a control on a form with a text reference

L

lisa

This is incredibly frustrating.

I have a VB6 app at work that we're upgrading to .NET. In the VB6
code, I'm able to get a reference to txtText by using
frm.Controls(txtText). In ASP.NET, we have Page.FindControl(txtText).
But in WindowsForms, there doesn't seem to be any equivalent.

Now, the Form.Controls collection only takes a numeric index. It's a
big form, and I don't want to have to iterate through the entire
control collection for each item I'm trying to reference.

I've been over and over the MSDN documentation, and I can't find
anything that I can use to do this. Has this capability really been
left out of .NET forms? Or is it just hiding under a name I wouldn't
have guessed (like replacing Repaint with Invalidate)?

Even the CallByName method requires an actual reference to the control.
Does anyone know how to get a reference to a control based on the text
of the control's name?

Thanks,
Lisa
 
H

Herfried K. Wagner [MVP]

I have a VB6 app at work that we're upgrading to .NET. In the VB6
code, I'm able to get a reference to txtText by using
frm.Controls(txtText). In ASP.NET, we have Page.FindControl(txtText).
But in WindowsForms, there doesn't seem to be any equivalent.

..NET 2.0: 'Me.Controls("ButtonCancel")'. Note that you can only access
controls directly contained in the container.
 
L

lisa

Herfried said:
.NET 2.0: 'Me.Controls("ButtonCancel")'. Note that you can only access
controls directly contained in the container.

So they decided to give it back. I don't suppose there's any way to do
this in .NET 1.1, is there?

Lisa
 
G

Guest

lisa,

I have never tried this, but I wonder if you could iterate through all the
controls on the form at form load, adding each control to a hashtable, with
the control's name as the key?

Then you could retrieve a reference to the control from the hashtable, using
the control's name as the key.

Kerry Moorman
 
G

GhostInAK

Hello Kerry,

While entirely possible, I'm having a real hard time coming up with a valid
scenario for this.

-Boo
 
P

Phill W.

I have a VB6 app at work that we're upgrading to .NET. In the VB6
code, I'm able to get a reference to txtText by using
frm.Controls(txtText). In ASP.NET, we have Page.FindControl(txtText).
But in WindowsForms, there doesn't seem to be any equivalent.

It's tedious, but you could set up a Hashtable to each Control, with the
Control's name as key, then use that to do the lookup.

Private m_oCtls As New HashTable

Private Sub Form_Load()

m_oCtls.Add( Me.txtText.Name, Me.txtText )
m_oCtls.Add( Me.txtText2.Name, Me.txtText2 )
' or some recursive code that does them all in one go
. . .

End Sub

then, you can access each Control out of the Hashtable by name, as in

' Watch out for other Types of Control
Dim tb As TextBox _
= DirectCast( m_oCtls.Item( "txtText" ), TextBox )

HTH,
Phill W.
 

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