Home All Groups Group Topic Archive Search About

Finding a control on a form with a text reference

Author
27 Sep 2006 3:08 PM
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

Author
27 Sep 2006 3:25 PM
Herfried K. Wagner [MVP]
<l***@starways.net> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
27 Sep 2006 3:55 PM
lisa
Herfried K. Wagner [MVP] wrote:
> <l***@starways.net> schrieb:
> > 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.

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
Author
27 Sep 2006 4:11 PM
Kerry Moorman
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


Show quoteHide quote
"l***@starways.net" wrote:

>
> Herfried K. Wagner [MVP] wrote:
> > <l***@starways.net> schrieb:
> > > 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.
>
> 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
>
>
Author
28 Sep 2006 5:41 AM
GhostInAK
Hello Kerry,

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

-Boo

Show quoteHide quote
> 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
>
> "l***@starways.net" wrote:
>
>> Herfried K. Wagner [MVP] wrote:
>>
>>> <l***@starways.net> schrieb:
>>>
>>>> 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.
>>>
>> 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
>>
Author
28 Sep 2006 1:21 PM
Phill W.
l***@starways.net wrote:

> 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.