470,595 Members | 1,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,595 developers. It's quick & easy.

Finding a control with a particular name...

(VB.NET 2002, Windows app).
I'm going to be provided a two-letter string like "BV" or "TP" that
represents a location.

I then need to perform some actions on several form controls that have names
derived from that location.
For example: lblBVimp, txtBVsignoff, txtBVsignoffdate, chkBV

Now I know i can do a "For Each XXX in myForm.Controls", If XXX.name =
blahblahblah,
but I've got several HUNDRED controls on the form.
Is there any better way?

Call me crazy, but I could have sworn that in the past I've been able to do
something like
myForm.controls("txt" & strLocation & "imp")
and have it find the control, but one of my coworkers says that has never
been possible.

What about GetIndexOf ? Can it accept a string as a parameter?
Thanks!!!
Nov 20 '05 #1
7 1635
* "BradC" <br********@yahoo.com> scripsit:
Call me crazy, but I could have sworn that in the past I've been able to do
something like
myForm.controls("txt" & strLocation & "imp")
and have it find the control, but one of my coworkers says that has never
been possible.


Add all controls to a 'Hashtable' (use their names as keys). Then you
can easlily get a reference to a control by its name.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
"BradC" <br********@yahoo.com> schrieb
(VB.NET 2002, Windows app).
I'm going to be provided a two-letter string like "BV" or "TP"
that represents a location.

I then need to perform some actions on several form controls that
have names derived from that location.
For example: lblBVimp, txtBVsignoff, txtBVsignoffdate, chkBV
Now I know i can do a "For Each XXX in myForm.Controls", If XXX.name
= blahblahblah,
but I've got several HUNDRED controls on the form.
Is there any better way?

Run "for each..next" only once in form_load (or in the constructor) to
add all controls to a hashtable (System.Collections.Hashtable). Use the
name property as the key. Note: Use recursion to get the nested controls
also because Form.Controls only contains the controls directly placed on
the form.
Call me crazy, but I could have sworn that in the past I've been able
to do something like
myForm.controls("txt" & strLocation & "imp")
and have it find the control, but one of my coworkers says that has
never been possible.


It has been possible.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
BradC,
In addition to the others comments.
I'm going to be provided a two-letter string like "BV" or "TP" that
represents a location.
I then need to perform some actions on several form controls that have names derived from that location. This smells like a bad design! (Code Smell is a term used in Refactoring
http://www.refactoring.com expressing when a section of code should be
refactored. Refactoring is a technique to restructure code in a disciplined
way, with the intent to improve the design of the code.).
Call me crazy, but I could have sworn that in the past I've been able to do something like
myForm.controls("txt" & strLocation & "imp")
and have it find the control, but one of my coworkers says that has never
been possible. I believe VB6 supported that however VB.NET does not.
I then need to perform some actions on several form controls that have names derived from that location.
For example: lblBVimp, txtBVsignoff, txtBVsignoffdate, chkBV I would recommend creating a User Control that contains 5 controls
(encapsulation), then your routine is a member of this user control. Your
form would have multiple instances of the User Control. This allows you to
simply call a method on the user control and be done with it, rather then
"waste time" looking for each control & performing some action. Seeing as
you would have multiple instances of the user control, each would have its
own instance of the 5 controls.

If due to layout of the controls creating an actual User Control is not
possible, I would recommend creating a Component that has 5 properties
representing the 5 controls. The routine would still be a member of this
Component. Because its a component you can add instances of it to your form,
and set the 5 control properties at run time.

The User Control would be the "cleaner" design, however the Component will
be the least impact to the existing design!

Note creating a User Control should also reduce the amount of code in your
form, as a lot of the logic will be encapsulated in the user control. Also
the number of lines of code may be reduced as the user control itself would
have the event handler for a control, rather then the form having it
multiple times (once for BV, and once for TP) Granted the new Handles clause
helps reduce this anyway...

Hope this helps
Jay
"BradC" <br********@yahoo.com> wrote in message
news:eg**************@tk2msftngp13.phx.gbl... (VB.NET 2002, Windows app).
I'm going to be provided a two-letter string like "BV" or "TP" that
represents a location.

I then need to perform some actions on several form controls that have names derived from that location.
For example: lblBVimp, txtBVsignoff, txtBVsignoffdate, chkBV

Now I know i can do a "For Each XXX in myForm.Controls", If XXX.name =
blahblahblah,
but I've got several HUNDRED controls on the form.
Is there any better way?

Call me crazy, but I could have sworn that in the past I've been able to do something like
myForm.controls("txt" & strLocation & "imp")
and have it find the control, but one of my coworkers says that has never
been possible.

What about GetIndexOf ? Can it accept a string as a parameter?
Thanks!!!

Nov 20 '05 #4
Thanks, guys. Hashtable works great!!!

This is a "I Love .net" moment.
Now if I just had more of those than my "I Hate .net" moments.
(Yes, I'm converting from VB6).

Brad

">
Add all controls to a 'Hashtable' (use their names as keys). Then you
can easlily get a reference to a control by its name.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #5
> This smells like a bad design! (Code Smell is a term used in Refactoring
http://www.refactoring.com expressing when a section of code should be
refactored. Refactoring is a technique to restructure code in a disciplined way, with the intent to improve the design of the code.).
I don't disagree with your assessment of bad control design!

We've been asked to make a SQL-VB.Net app that looks and behaved EXACTLY
like the Access database they are used to (bad design and all). You should
have seen the database structure before we normalized it. (At least they
didn't insist we use the same horrible data structure).
I would recommend creating a User Control that contains 5 controls
(encapsulation), then your routine is a member of this user control. Your
form would have multiple instances of the User Control. This allows you to
simply call a method on the user control and be done with it, rather then
"waste time" looking for each control & performing some action. Seeing as
you would have multiple instances of the user control, each would have its
own instance of the 5 controls.
The controls are nowhere near each other on the actual form, so I don't
think the above would work.
If due to layout of the controls creating an actual User Control is not
possible, I would recommend creating a Component that has 5 properties
representing the 5 controls. The routine would still be a member of this
Component. Because its a component you can add instances of it to your form, and set the 5 control properties at run time.
So I could have a single "BVStuff" object that had a .checkbox, a .label,
and a .textbox property?
Could these then still have their own individual .top and .left property for
positioning?

That sounds pretty cool. I could just do
with BVStuff
.checkbox.checked = true
.label.forecolor = color.black
.textbox.text = "Hello there".
End With.

Am I on the right track?
Soo...... How do I actually DO that...? :)
The User Control would be the "cleaner" design, however the Component will
be the least impact to the existing design!

Note creating a User Control should also reduce the amount of code in your
form, as a lot of the logic will be encapsulated in the user control. Also
the number of lines of code may be reduced as the user control itself would have the event handler for a control, rather then the form having it
multiple times (once for BV, and once for TP) Granted the new Handles clause helps reduce this anyway...

Hope this helps
Jay

Nov 20 '05 #6
Cor
Hi Brad,

This examples finds "all" controls on your form

In this example all texts are changed, but you can of course use it as you
wish by testing the name.

\\\
Private Sub Load_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim nada As New doCtr(Me)
End Sub
End Class
Public Class doCtr
Public Sub New(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Text = "CTR"
Dim newdoCtr As _
New doCtr(ctr)
Next
End Sub
End Class
///
I hope this helps a little bit?

Cor
Nov 20 '05 #7
Thanks, Cor. I used a loop very much like that on form load to populate a
HashTable, as suggested by some other replies.

"Cor" <no*@non.com> wrote in message
news:u2**************@TK2MSFTNGP09.phx.gbl...
Private Sub Load_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim nada As New doCtr(Me)
End Sub
End Class
Public Class doCtr
Public Sub New(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Text = "CTR"
Dim newdoCtr As _
New doCtr(ctr)
Next
End Sub
End Class
///
I hope this helps a little bit?

Cor

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Bilal | last post: by
21 posts views Thread by André | last post: by
6 posts views Thread by Jay Bienvenu | last post: by
4 posts views Thread by Chris | last post: by
3 posts views Thread by Tor Inge Rislaa | last post: by
2 posts views Thread by Fred Wilson | last post: by
1 post views Thread by avik1612 | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.