Home All Groups Group Topic Archive Search About

Find a control on a form of a specific name

Author
8 Nov 2006 5:36 PM
Darin
I have a form that might (or might not) have a textbox on it named
something particular - how can I find if the form has the control, and
if it does what the text of the control has? This form has textboxes
that are created on the fly, and when the OK is clicked I need to figure
out which ones are on the screen and what the text is in each to do
other stuff.

So, kind of like:

dim xstr as string
xstr=me.findcontrol("tText").tostring

Thanks.

Darin

*** Sent via Developersdex http://www.developersdex.com ***

Author
8 Nov 2006 7:04 PM
RobinS
Try this. It's not terribly elegant, but I think
it will get the job done.
------------------


'call with the name of the control you're looking for
If ControlIsThere("MyBigGiantTextBox") Then...

'You need this to be available in both the
'function and the recursive routine that
'it calls, so you have something to stop
'the recursion. I'd make it a form-level
'variable.
Dim FoundControl as Boolean

Private Function ControlIsThere(controlName as String) as Boolean
    'Set this to false.
    FoundControl = False
    'Call the recursive routine with the form.
    ControlExists(Me)
    'Return the result.
    Return FoundControl
End Function

'This will recurse, so it gets all the controls on the form.
'This way, if you have panels, for example, it will
'  touch the controls inside those containers.
Private Sub ControlExists(ByVal ctrlContainer As Control)
    For Each ctrl As Control In ctrlContainer.Controls
        'Debug.Print(ctrl.Name.ToString)
        If ctrl.Name = "MyBigGiantTextBox" Then
            FoundControl = True
            Exit For
        End If
        'if control has children, call this function recursively
        If ctrl.HasChildren Then
            ControlExists(ctrl)
        End If
    Next
End Sub

Robin S.

Show quoteHide quote
"Darin" <darin_nospam@nospamever> wrote in message
news:e6LV6x1AHHA.3620@TK2MSFTNGP02.phx.gbl...
>I have a form that might (or might not) have a textbox on it named
> something particular - how can I find if the form has the control, and
> if it does what the text of the control has? This form has textboxes
> that are created on the fly, and when the OK is clicked I need to figure
> out which ones are on the screen and what the text is in each to do
> other stuff.
>
> So, kind of like:
>
> dim xstr as string
> xstr=me.findcontrol("tText").tostring
>
> Thanks.
>
> Darin
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
8 Nov 2006 8:34 PM
Steve Long
You know, I'm not sure if this will pick up controls that are contains
within control containers on a form, but what's wrong with this:

Public Function ContainsControl("ControlYourLookingFor" as string) as
Control
for each ctl as control in me.controls
    if string.Compare(ctl.name, "ControlYourLookingFor", True) then
        return ctl
    end if
next
end function

if not ctl is nothing then
    ' get the text of the control and do something with it
end if

HTH
Steve

Show quoteHide quote
"Darin" <darin_nospam@nospamever> wrote in message
news:e6LV6x1AHHA.3620@TK2MSFTNGP02.phx.gbl...
>I have a form that might (or might not) have a textbox on it named
> something particular - how can I find if the form has the control, and
> if it does what the text of the control has? This form has textboxes
> that are created on the fly, and when the OK is clicked I need to figure
> out which ones are on the screen and what the text is in each to do
> other stuff.
>
> So, kind of like:
>
> dim xstr as string
> xstr=me.findcontrol("tText").tostring
>
> Thanks.
>
> Darin
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
8 Nov 2006 11:19 PM
Herfried K. Wagner [MVP]
"Darin" <darin_nospam@nospamever> schrieb:
>I have a form that might (or might not) have a textbox on it named
> something particular - how can I find if the form has the control, and
> if it does what the text of the control has? This form has textboxes
> that are created on the fly, and when the OK is clicked I need to figure
> out which ones are on the screen and what the text is in each to do
> other stuff.

If you are using .NET 2.0 and you know the container control containing the
control you can use '<container control>.Controls("TextBox22")' to obtain a
reference to the control.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>