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

April 9th, 2004, 01:58 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 9th, 2004, 02:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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
|
|

April 9th, 2004, 02:14 PM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 9th, 2004, 06:53 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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
|
|

April 9th, 2004, 11:43 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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
|
|

April 11th, 2004, 11:12 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 11th, 2004, 07:38 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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
|
|
 |