Using a variable to select a control?

N

Nozza

I want to be able to use a variable in a program to select one of many
controls - labels - on a form, and make the control visible or not.

But I can't seem to get this to work.

I seem to be going around in circles, and there is something I am not
getting.

If I have a form named Form 1, and with a label, and a button on it, I
can enter the following code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim MyString As String = "Form1.Label1"

Call MakeInVisible(MyString)

End Sub

Sub MakeInVisible(ByVal PassedString As String)
Dim MyControl As New Control

MyControl.Name = PassedString
MyControl.Visible = False

End Sub
End Class


I can run the program, but the control named Label1 does not have its
visible property changed

The point of the program above is for me to work out how to pass a
variable to a parameter of a procedure, and then in the procedure use
the parameter to identify a control and then adjust the proerties of
the control. I know in the example above I could just enter a command
Form1.Label1.Visible = False without the need to call a procedure :)


Any help gratefully received.

Noz
 
A

Armin Zingler

Nozza said:
I want to be able to use a variable in a program to select one of
many controls - labels - on a form, and make the control visible or
not.

But I can't seem to get this to work.

I seem to be going around in circles, and there is something I am
not getting.

If I have a form named Form 1, and with a label, and a button on it,
I can enter the following code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim MyString As String = "Form1.Label1"

Call MakeInVisible(MyString)

End Sub

Sub MakeInVisible(ByVal PassedString As String)
Dim MyControl As New Control

MyControl.Name = PassedString
MyControl.Visible = False

End Sub
End Class


I can run the program, but the control named Label1 does not have
its visible property changed

The point of the program above is for me to work out how to pass a
variable to a parameter of a procedure, and then in the procedure
use the parameter to identify a control and then adjust the
proerties of the control. I know in the example above I could just
enter a command Form1.Label1.Visible = False without the need to
call a procedure :)


Any help gratefully received.


Form1 is a class name. Class names are (usually) out of interest at
runtime. They are names used in a language during development and by the
compiler, but not later. Just like variable names that are easy to
remember memory addresses. At runtime, the names are gone. (Reflection
offers additional possibilities, but this is IMO a misuse of this
technique as long as there are straight ways to accomplish the same)

To address an object you need a reference. There can be 0 or many
instances of class Form1. Therefore, "Form1" is nothing that helps.
"Label1" is a variable name, but it's also a property of the Label
object. The Controls collection of a Form offers an overloaded version
of it's 'Item' property where you can pass the control's name as the
key:

dim c as control
c = TheForm.Controls("Label1")

As written above, TheForm is the reference to the Form that you still
need.

However, I wonder why you have the control's name in a String. In every
occuring case I've seen, it is because the variable name is also stored
outside the project's source code. Is this true?


Armin
 
C

Cor Ligthert[MVP]

Nozza,

I have learned an easy method from Armin for that, just create an array from
the labels you want to hide.

\\\
dim myControls as Control() = {TextBox1, Combobox1, etc}

And then a simple for each loop in that array

For each ctr as Control in myControls.
ctr.Visible = false
Next
///
Cor
 
J

Jack Jackson

I want to be able to use a variable in a program to select one of many
controls - labels - on a form, and make the control visible or not.

But I can't seem to get this to work.

I seem to be going around in circles, and there is something I am not
getting.

If I have a form named Form 1, and with a label, and a button on it, I
can enter the following code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim MyString As String = "Form1.Label1"

Call MakeInVisible(MyString)

End Sub

Sub MakeInVisible(ByVal PassedString As String)
Dim MyControl As New Control

MyControl.Name = PassedString
MyControl.Visible = False

End Sub
End Class


I can run the program, but the control named Label1 does not have its
visible property changed

The point of the program above is for me to work out how to pass a
variable to a parameter of a procedure, and then in the procedure use
the parameter to identify a control and then adjust the proerties of
the control. I know in the example above I could just enter a command
Form1.Label1.Visible = False without the need to call a procedure :)

No, you can't say Form1.Label1.Visible = False. I assume Form1 is the
name of the form. You need a reference to the form. If it is the
current form you would use Me.Label1.Visible or just Label1.Visible.
Note that Label1 is a form level variable that just happens to have
the same name as the name of the control.

I don't recommend using the text names of controls. You would be much
better off just passing a reference to the control:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Call MakeInVisible(Label1)

End Sub

Sub MakeInVisible(ctl As Control)

ctl.Visible = False
End Sub


If you insist on using text names:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim MyString As String = "Label1"

Call MakeInVisible(MyString)

End Sub

Sub MakeInVisible(ByVal PassedString As String)

For Each MyControl as Control In Me.Controls
If MyControl.Name = PassedString Then
MyControl.Visible = False
Exit For
End If
Next
End Sub

This code assumes that the controls you are interested in are direct
cildren of the form. If not, you will need to recurse into the
children of container controls.
 

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