Refering to a control by text name

B

Bob

Warning: newbie question!

Of course this works: combobox1.text = "Hello"

I want to do this in vb.net....
dim cb as combobox
cb.name = "combobox1"
cb.text = "Hello" --- this doesn't work, my combobox1 is unchanged

thanks
Bob
 
H

Herfried K. Wagner [MVP]

Bob said:
Of course this works: combobox1.text = "Hello"

I want to do this in vb.net....
dim cb as combobox
cb.name = "combobox1"
cb.text = "Hello" --- this doesn't work, my combobox1 is unchanged

\\\
DirectCast(Me.Controls("ComboBox1"), ComboBox).Text = ...
///

If the control is not placed directly on the form but on another container,
you'll have to use the 'Controls' collection of the container.
 
J

James Hahn

Your new variable needs to refer to the instance of the object. It is not
sufficient simply to give it the same name.

dim cb as combobox
cb = combobox1
cb.text = "Hello"
 
O

Onur Güzel

Warning: newbie question!

Of course this works:  combobox1.text = "Hello"

I want to do this in vb.net....
dim cb as combobox
cb.name = "combobox1"
cb.text = "Hello"    ---  this doesn't work, my combobox1 is unchanged

thanks
Bob

Assuming you have an existing combobox, in that case setting text of
it is done simply as you stated:
combobox1.Text = "Hello"

And if you're using another variable, that is "cb":
Dim cb As ComboBox = ComboBox1
cb.Name = "combobox1"
cb.Text = "Hello"

...and if you don't have an existing combobox, you need to instantiate
a new one using new keyword with "cb" variable:
Dim cb As New ComboBox
cb.Name = "combobox1"
cb.Text = "Hello"
' Set its location
cb.Location = New Point(100, 100)
' Add it
Me.Controls.Add(cb)

So, it depends on your wish.

Onur Güzel
 
C

Cor Ligthert[MVP]

Herfried,

I was me not aware that did exist in Net 2.0 (and then of course latter
versions).

However, I was curious as well why you used the DirectCast.
I tried it by doing this.

\\\
Controls("combobox1").Text = "Herfried"
///
This was working as well

The combobox derives from Controls so the text is default (although Controls
is here the ControlCollection)
I am suprised that I can use combobox1 in lower case, while I had used
upercases for the C and last B

Cor
 
A

Armin Zingler

Bob said:
Warning: newbie question!

Of course this works: combobox1.text = "Hello"

I want to do this in vb.net....
dim cb as combobox
cb.name = "combobox1"
cb.text = "Hello" --- this doesn't work, my combobox1 is unchanged

You know that Combobox1 is a variable name, and they are resolved at compile
time? Why do you have a variable name in a String? (yes, I do know that
there's also a Name property) This is usually only necessaray of you want to
store references to controls outside the application, e.g. in a database.


Armin
 
J

James Hahn

No. Combobox1 is the name of the existing control. OP states that

combobox1.text = "Hello"

works OK. That is, the control exists and is visible. Therefore, all that
is needed is

cb = combobox1

to set the new variable to the existing control. However OP does not
explain why they need a new reference to the object when the reference
combobox1 is already available, so the problem doesn't make a lot of sense..
 
A

Armin Zingler

James said:
No. Combobox1 is the name of the existing control. OP states that

Right, it is the name of an existing control, but...
combobox1.text = "Hello"

.... he also wrote

cb.name = "combobox1"

There "Combobox1" is a string. Therefore, the way I read his code, he wanted
to refer to the control by a string. Turned out to be true.

works OK. That is, the control exists and is visible. Therefore, all
that is needed is

cb = combobox1

to set the new variable to the existing control. However OP does not
explain why they need a new reference to the object when the reference
combobox1 is already available, so the problem doesn't make a lot of
sense..


Armin
 
H

Herfried K. Wagner [MVP]

Cor --

Cor Ligthert said:
I was me not aware that did exist in Net 2.0 (and then of course latter
versions).

However, I was curious as well why you used the DirectCast.
I tried it by doing this.

\\\
Controls("combobox1").Text = "Herfried"
///
This was working as well

The combobox derives from Controls so the text is default (although
Controls is here the ControlCollection)

Well, you are right. I just wanted to demonstrate how to enable access to
all properties of the control.
 
J

James Hahn

I don't know where you got that from. OP claimed that the original code as
posted didn't work. The actual post was "cb.text = "Hello" --- this
doesn't work, my combobox1 is unchanged"

By making the change that I suggested it did work - combobox1 is changed -
for the reason I mentioned.
 
A

Armin Zingler

James said:
I don't know where you got that from.

That it turned out to be true? (just asking what you're referring to) Well,
he wrote "the contol's name (and a lot of others) are stored in a file."
OP claimed that the original
code as posted didn't work. The actual post was "cb.text = "Hello" ---
this doesn't work, my combobox1 is unchanged"

By making the change that I suggested it did work - combobox1 is
changed - for the reason I mentioned.

I have no doubt that it works. Though I think it's not what the OP was
looking for. By writing

1. cb.name="combobox1"
2. cb.text="hello"
he wanted to
1. specify which control (yes, by a string)
2. change it's text.

But we both know that it doesn't work this way. Herfried already gave the
answer.


Armin
 
O

OmegaSquared

Hello, Bob/Herfried,

Re:
If the control is not placed directly on the form but on another container,
you'll have to use the 'Controls' collection of the container.

In v2.0 and later, you can use the Find method to return named controls that
are nested in container controls. Something like:

Dim cb As ComboBox = DirectCast(Me.Controls.Find("ComboBox1",
True)(0), ComboBox)

If you are not sure that there is one and only one instance of the named
control, then testing the result returned by Find is advisable (before
assuming index zero is appropriate).

Cheers,
Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top