|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Finding a control on a form with a text referenceI 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 <l***@starways.net> schrieb:
> I have a VB6 app at work that we're upgrading to .NET. In the VB6 ..NET 2.0: 'Me.Controls("ButtonCancel")'. Note that you can only access > 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. 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/> Herfried K. Wagner [MVP] wrote:
> <l***@starways.net> schrieb: So they decided to give it back. I don't suppose there's any way to do> > 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. this in .NET 1.1, is there? Lisa 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 > > 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 >> l***@starways.net wrote:
> I have a VB6 app at work that we're upgrading to .NET. In the VB6 It's tedious, but you could set up a Hashtable to each Control, with the > 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. 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.
XML to save some settings
overflow detection without try-catch How can I create a Shortcut (with parameters) on a user-desktop with vb.net? Redirecting standard output VB Program - CHM help integration Posting login data with HttpWebRequest Permanent Rectangle on Form. Are you a C# Developer in Buckinghamshire? Getting nodes from an XML-document Q: select (case insensitive) |
|||||||||||||||||||||||