Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Find a control's name and put it into property

Posted on 2014-01-15
Medium Priority
Last Modified: 2014-01-15
Please consider this code
Public class Form1
       DIM txt1 as new textbox

Private sub createControl(Byref control as control)

       control.Name = control.something <=== Here I would like to put in "txt1"


End Sub

End class

Open in new window

I would have thought the property would be "txt1", but it is an empty string so I need to set it. I need this to refer to the control later on (me.controls("txt1")
Question by:IverErling
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
LVL 44

Expert Comment

ID: 39782757
control.Name = "txt1"

by the way - your class looks very odd.  The sub is called createControl BUT it accepts an existing control as the input parameter.
LVL 64

Expert Comment

by:Fernando Soto
ID: 39782833
Hi IverErling;

In order to find a control you need to know its name. The trick is to give the dynamically created control a name to start with. For example from your code.

Public class Form1
       ' Create a TextBox control
       Dim txt1 As New TextBox
       ' Give that new TextBox a name
       txt1.Name = "txt1"
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39782845
The control name and the variable name are not the same thing. When you call Dim on a control, you create a variable. The Name property is another thing that can be used as an alternative to the variable name in some situations.

When you create a control in the designer, the designer sets the control Name property to the same name you gave the variable, by generating code similar to the following in the .designer.vb hidden file:

Dim txt1 As New TextBox

If you really need the Name property set for controls that you create dynamically, you need to do the same. There is no way to retrieve something like "control.something", because it's your job to define the name.

This being said, in most situations you do not need the name in order to work, so you can leave it to an empty string.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39783125
First to AidanAinscow: The strange looking code isn't the real code. It was just to illustrate the problem.

Then to Fernando Soto: I don't want to do control.Name="txt1". That is what I want to avoid.

To JamesBurger: You seem to understand what I am looking for. Since I want to use a separat class to create all my controls so the code in my form is kept to a minimum, I don't want to hard-code anything to do with names etc.  I want to pass info to the class and keep it as simple as possible. I do currently have it working just fine like this:

createClass(Form, Control, Top, Left, Width, Heigth, Text, TabOrder) etc.

My class creates the controls in my form, but I then realised that I need a different way to access those controls in the forms dynamicly created events, since the controls aren't there at design time. So by using the controls(name) method I can do that. But then the name property needs to be set which currently it isn't. And so I thought that since I am passing the txt1 control to the class after defining it I should be able to pick up the text "txt1" also. But haven't found any way to do this except like a separate parameter as a text when calling the class method like this:

createClass(Form, Control, ControlName, Top, Left, Width, Heigth, Text, TabOrder)

createClass(Me, txt1, "txt1",100,150, 200,20,"Enter my name here",1) etc.

Open in new window

(I am of course replacing the top, left etc. parameters with variables in the actual code)

And that seems a bit silly. Is there really not a way to pick ut the variable name from the control parameter so I could make the control name the same?

Oh, well, I guess I can live with that allthough it looks a bit silly ;-) Unless you can suggest something else...

LVL 44

Expert Comment

ID: 39783146
>>The strange looking code isn't the real code. It was just to illustrate the problem.

The problem with doing that is the pseudo code may end up not being at all useful to demonstrate your problem / requirement.

All three experts have pointed out that in a dynamically created control YOU MUST supply the name yourself.  There is nothing to pick up to use your terminology.
LVL 40
ID: 39783254
A variable name does not exists in the compiled code, it is replaced by a pointer, a numeric value. So there is no way to retrieve it. That is why you need to provide the Name property yourself if you need it.

It's not silly, it's the way things are done Under the Hood. Look at the code in the .designer.vb file that is created along with any of your Form. You will see that for each Control, the designer has generated a line to set the Name, because this cannot be done dynamically.

You need to do the same.

Author Comment

ID: 39783755
Ok. I have to get used to this. I've been working with a semi compiled language that would allow all sorts of things.

Regarding silly, I just thought it looked a bit silly to supply the same info, or at least what looked the same twice next to each other.

But your explanation makes sense, and I am grateful for it. Thanks!

All the best
IVer in Oslo
LVL 44

Expert Comment

ID: 39783890
>>Since I want to use a separat class to create all my controls so the code in my form is kept to a minimum

Have a look at partial class in the help files.  If you look very carefully at your form you will actually find a second file on disc that is used for the design elements.
Maybe that is what you really wanted to know about so your class appears 'small' in terms of code.

Author Comment

ID: 39784560
Thanks! I will certainly look into that ;-)


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question