eric6304 Posted June 25, 2004 Posted June 25, 2004 Someone created this function to return a TextBox Control ID, does anyone know how to modify this function, so it can return the value stored inside this TextBox at VB.Net windows form application? '================================================ Private Function findObjectByName(ByRef formID As Form, ByVal objectName As String) As Control Dim ctl As Control For Each ctl In formID.Controls If ctl.Name = objectName Then Return ctl End If Next End Function Quote
Malfunction Posted June 25, 2004 Posted June 25, 2004 You're definately looking for the .Text property of the textBox. However in c# you'd have to cast the ctrl to textBox in order to get the textBox's properties still don't know about VB. Quote Debug me...
*Experts* Nerseus Posted June 25, 2004 *Experts* Posted June 25, 2004 Here's the simple answer (I might not be using DirectCast right): Private Function findObjectByName(ByRef formID As Form, ByVal objectName As String) As String Dim ctl As Control For Each ctl In formID.Controls If ctl.Name = objectName Then Dim t As TextBox = DirectCast(TextBox, ctl) Return t.Text End If Next Return String.Empty End Function The "better" approach might be to use Reflection to find the control by the string name, rather than the looping approach. A search of these forums should return some threads where this has been discussed. If you're going to use this looping code a lot you may want to store the control references in a hashtable so you can easily look up a control by it's name. You would fill in the hashtable with every control on the form using the name as the key. Later, you could get the control from the hashtable by name. By the way, the parameters to the function seem to have their ByVal/ByRef's backwards. There's no need to pass the form ByRef (it would allow changing what the variable pointed to - meaning, you could null out the pointer by accident). The string should probably be passed ByRef so that it doesn't have to be copied (which is what ByVal will do). Minor, minor issues - but worth noting since you're using someone else's code. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
eric6304 Posted June 28, 2004 Author Posted June 28, 2004 (edited) Nerseus, Thanks a lot for your help. What you suggested works great!!! Here's the simple answer (I might not be using DirectCast right): Private Function findObjectByName(ByRef formID As Form, ByVal objectName As String) As String Dim ctl As Control For Each ctl In formID.Controls If ctl.Name = objectName Then Dim t As TextBox = DirectCast(TextBox, ctl) Return t.Text End If Next Return String.Empty End Function The "better" approach might be to use Reflection to find the control by the string name, rather than the looping approach. A search of these forums should return some threads where this has been discussed. If you're going to use this looping code a lot you may want to store the control references in a hashtable so you can easily look up a control by it's name. You would fill in the hashtable with every control on the form using the name as the key. Later, you could get the control from the hashtable by name. By the way, the parameters to the function seem to have their ByVal/ByRef's backwards. There's no need to pass the form ByRef (it would allow changing what the variable pointed to - meaning, you could null out the pointer by accident). The string should probably be passed ByRef so that it doesn't have to be copied (which is what ByVal will do). Minor, minor issues - but worth noting since you're using someone else's code. -nerseus Edited June 28, 2004 by eric6304 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.