Solved

Find a control's name and put it into control.name property

Posted on 2014-01-15
9
274 Views
Last Modified: 2014-01-15
Please consider this code
Public class Form1
       DIM txt1 as new textbox
       createControl(txt1)

Private sub createControl(Byref control as control)

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

       Me.Controls.Add(control)

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")
0
Comment
Question by:IverErling
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 
LVL 62

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"
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 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
txt1.Name="txt1"

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.
0
 

Author Comment

by:IverErling
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...

Iver
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 44

Expert Comment

by:AndyAinscow
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.
0
 
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.
0
 

Author Comment

by:IverErling
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
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39783890
ps.
>>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.
0
 

Author Comment

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

IVer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now