Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
| Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 9th, 2004, 01:58 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Directly referencing a conrol based on a string

I have a simple function which loops through all the controls on a form searching for a specific one:

    Public Function FindControl(ByVal ctrls As Control.ControlCollection, ByVal ctrlName As String) As Control

        For Each ctrl As Control In ctrls
            If ctrl.Name = ctrlName.Trim Then
                Return ctrl
            End If
            If ctrl.HasChildren Then
                FindControl(ctrl.Controls, ctrlName)
            End If
        Next
        Return Nothing
    End Function

Anyone know of a more efficient way of directly referencing the control since I know it's name? Once I find it I need to set various properties of the control.

Thanks,
Tamarack

 
Old April 9th, 2004, 02:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

For example, if it is a textbox you could do this:

------------------------------------------------------------
CType(ctrl, TextBox).ReadOnly = True
------------------------------------------------------------

Replace whatever "ReadOnly" with the specific property you want to get or set.

J
 
Old April 9th, 2004, 02:14 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Thanks for the quick reply.

You are assuming I already know which control I need to manipulate. But I don't - that's why I have the looping construct. I would like to replace the entire looping process with a direct reference to the control based on it's Name property.

Tamarack

 
Old April 9th, 2004, 06:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

OK, then see if this helps you out more:

-------------------------------------------------
Dim ctrl as Control

For Each ctrl In GroupBox.Controls
      If (ctrl.GetType.ToString = "System.Windows.Forms.TextBox") Then
          CType(ctrl, TextBox).Text = ""
      ElseIf (ctrl.GetType.ToString = "System.Windows.Forms.ComboBox") Then
          CType(ctrl, ComboBox).SelectedIndex = 2
      ElseIf (ctrl.GetType.ToString = "System.Windows.Forms.DateTimePicker") Then
          CType(ctrl, DateTimePicker).Value = Now()
      End If
Next
----------------------------------------------------

Here are just a few examples of looping through the controls in a groupbox, but you can do this with other containers(forms, panels). Simply replace the portions of the code with the control that you want to check and modify. Hope this helps.

J
 
Old April 9th, 2004, 11:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

After re-reading your last post (and I after I posted my reply), I have a feeling that I didn't really answer your question. I guess I still don't see what you are trying to do. If you know the control's name as you state in your first post, why don't you just set it's properties by that?

J
 
Old April 11th, 2004, 11:12 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default


You are correct, I do know the controls name. But how do I reference that control's properties simply by knowing it's name? I was hoping you would show an example in your last post because that is exactly what I'm looking for. The looping function I showed allows me to work with a strongly typed object so I can directly reference the properties because it finds the object given the name. But it is extremely inefficient. What I'm trying to do is exactly what you can do with an ADO recordset where you refer to a specific field by it's name:

myRecordset("txtFirstName") = "Robert"

I would like to do the same thing with the Controls collection of a given form:

myForm.Controls("txtFirstName").Text = "Robert"

Of course that is not allowed in .Net but I'm trying to find the equivalent. Basically, I need to refer to a control directly by using its name instead of looping through all the controls to find one that matches the name I already know.

I know it's a little confusing and I'm probably not explaining it very well, but thanks for any assistance.

Tamarack

 
Old April 11th, 2004, 07:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

OK, maybe:

For Each ctrl In GroupBox.Controls
      If (ctrl.GetType.ToString = "System.Windows.Forms.TextBox") Then
          If CType(ctrl, TextBox).Name = "txtFirstName" Then
              CType(ctrl, TextBox).Text = "Robert"
          End If
      End If
Next

Is this what you want?

J




Similar Threads
Thread Thread Starter Forum Replies Last Post
just directly close the window kanoorani Javascript How-To 0 May 5th, 2007 12:13 AM
Printing directly leo_vinay ASP.NET 1.0 and 1.1 Basics 0 October 26th, 2005 11:17 PM
Directly editing Binary HiddenFire VB.NET 4 July 6th, 2005 12:14 PM
the record after x directly akhamis SQL Language 5 November 1st, 2004 03:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.