Finding a control with a particular name...

  • Thread starter Thread starter BradC
  • Start date Start date
B

BradC

(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!!!
 
BradC said:
(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.
 
* "BradC said:
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.
 
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
 
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

">
 
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...? :)
 
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
 
Thanks, Cor. I used a loop very much like that on form load to populate a
HashTable, as suggested by some other replies.
 
Back
Top