Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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.
Debug me...
  • *Experts*
Posted

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

"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
Posted (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 by eric6304

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...