Find a control's name and put it into control.name 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 control.name 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:Iver Erling Arva
  • 3
  • 3
  • 2
  • +1
LVL 45

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.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Author Comment

by:Iver Erling Arva
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 45

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

by:Iver Erling Arva
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 45

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

by:Iver Erling Arva
ID: 39784560
Thanks! I will certainly look into that ;-)


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Screencast - Getting to Know the Pipeline

600 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