Link to home
Start Free TrialLog in
Avatar of Iver Erling Arva
Iver Erling ArvaFlag for Norway

asked on

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

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")
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
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"
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iver Erling Arva

ASKER

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
>>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.
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.
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
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.
Thanks! I will certainly look into that ;-)

IVer