473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find a control on a form of a specific name

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 ***
Nov 8 '06 #1
3 1671
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.

"Darin" <darin_nospam@nospameverwrote in message
news:e6**************@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 ***

Nov 8 '06 #2
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

"Darin" <darin_nospam@nospameverwrote in message
news:e6**************@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 ***

Nov 8 '06 #3
"Darin" <darin_nospam@nospameverschrieb:
>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/>

Nov 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1583
by: Yasutaka Ito | last post by:
Hi, Is there a way to find to which control the current control is docked against? For example, let's say I have panel1 and panel2 docked to left within a form. The panel1 is the first one on...
3
4158
by: Not Me | last post by:
Hi, Just trying to get my head around tab controls, is there anywhere which would describe the structure of how they work? I seem to be ok so far, being able to check which page I'm on etc......
6
11268
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
10
5286
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
4
2493
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
5
2792
by: Richard Brown | last post by:
Ok, I've been looking through the .NET SDK docs and stuff. I'm wondering if you can provide a control extender that does generic validation or functionality just by dropping it on the form. For...
5
1509
by: Paul Bromley | last post by:
Can someone give me a very simple example on how to do this? As an example I have a commaned button in a user control. Once this user control is placed on a form I want to be able to respond in the...
11
2600
by: -D- | last post by:
How can I turn the visibility of the xml control on or off? <%@ Control Language="c#" AutoEventWireup="false" Codebehind="TopNavBar.ascx.cs" Inherits="compass.user_controls.TopNavBar"...
5
4517
by: YaoBao | last post by:
Is any ColdFusion script I can put on my webpage that will create a search bar so people can type keywords to match it on the current page in my website? It will be exactly like the finder search bar...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.