Solved

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

Posted on 2014-01-15
9
277 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 63

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

820 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