Access to a control in a form through a variable...

N

Nicola

Is it possible, in VB.Net, to access to a control (as a
textbox) in a form through a variable ???
Many thanks
nq
 
M

Morten Wennevik

Yes, through a property. It will appear as a variable, but you control
what is sent and how the received value should be treated.

Public Property Prop1() As String
Get
Return TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property

Then you would call Form1.Prop1 and you will receive the contents of
TextBox1

(note: my VB is really horrible, so there may be errors in the code)
 
M

Marc Scheuner [MVP ADSI]

Is it possible, in VB.Net, to access to a control (as a
textbox) in a form through a variable ???

I would prefer to access it through a property, which is basically the
same, but allows you to a) do some error and security checking, if and
when needed, and b) allows you to show only parts of a control (some
of its properties, for instance).

Read up on properties for classes! That should give you an idea.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
G

Guest

I THINK THIS IS NOT THE RIGHT SOLUTION FOR ME...
I WANT TO SET THE TEXT PROPERTY OF A TEXTBOX IN A FORM,
PASSING TO A FUNCTION THE NAME OF THE FORM, THE NAME OF
THE CONTROL AND THE VALUE TO SET INTO IT...
I READ THE NAME OF THE CONTROL FROM A TEXTFILE FOR
EXAMPLE... AND THEN CALL THE FUNCTION...
COULD I DO THIS USING THE PROPERTY SOLUTION YOU SUGGEST
ME ???

SORRY FOR MY BAD ENGLISH... I'M AN ITALIAN NEW VB.NET
USER...

THANKS
nq
 
G

Guest

I THINK THIS IS NOT THE RIGHT SOLUTION FOR ME...
I WANT TO SET THE TEXT PROPERTY OF A TEXTBOX IN A FORM,
PASSING TO A FUNCTION THE NAME OF THE FORM, THE NAME OF
THE CONTROL AND THE VALUE TO SET INTO IT...
I READ THE NAME OF THE CONTROL FROM A TEXTFILE FOR
EXAMPLE... AND THEN CALL THE FUNCTION...
COULD I DO THIS USING THE PROPERTY SOLUTION YOU SUGGEST
ME ???

SORRY FOR MY BAD ENGLISH... I'M AN ITALIAN NEW VB.NET
USER...

THANKS
nq
 
M

Morten Wennevik

Please, turn off caps lock. You could manage it by making the textbox
public

(from inside another Form, and provided you have a reference to Form1
available, and Form1 has a public textbox1)

Form1.TextBox1.Text = "Hello World"
 
G

Guest

I try to explain better my problem...
I have 2 arraylist. One stores the names of 10 controls of
a form and the other stores 10 values, one for every
control of the first arraylist. In this form there are
many controls, and the only way I found to set every
control to the relative value is to loop through all the
controls of the form and to set the value only if its name
is cointained in the first arraylist...
If I have 100 controls in the form, I have to loop through
all the controls...
If I found another way, I could avoid this and access
directly to the controls I'm interested... saving the time
to loop through the others 90 controls i don't need...
For example :

for i=0 to arraylist.count-1
myform.arraylist(i).text=arraylist2(i).tostring
next i

now i do:

for every control in myform.controls

next

I hope You understand what I want to say...I know my
english is very bad...
Many thanks
nq
 
N

Nicola

I send the previous message before end it...
This is the complete version of message...
I try to explain better my problem...
I have 2 arraylist. One stores the names of 10 controls of
a form and the other stores 10 values, one for every
control of the first arraylist. In this form there are
many controls, and the only way I found to set every
control to the relative value is to loop through all the
controls of the form and to set the value only if its name
is cointained in the first arraylist...
If I have 100 controls in the form, I have to loop through
all the controls...
If I found another way, I could avoid this and access
directly to the controls I'm interested... saving the time
to loop through the others 90 controls i don't need...
For example :

for i=0 to arraylist1.count-1
myform.arraylist1(i).text=arraylist2(i).tostring
next i

Now I do the following:

for every control in myform.controls
if arraylist1.contains(control.name) then
I=arraylist1.indexof(control.name)
control.text=arraylist2(i).tostring
end if
next

I hope You understand what I want to say...I know my
english is very bad...
Many thanks
nq
 
H

Herfried K. Wagner [MVP]

* said:
I THINK THIS IS NOT THE RIGHT SOLUTION FOR ME...
I WANT TO SET THE TEXT PROPERTY OF A TEXTBOX IN A FORM,
PASSING TO A FUNCTION THE NAME OF THE FORM, THE NAME OF
THE CONTROL AND THE VALUE TO SET INTO IT...
I READ THE NAME OF THE CONTROL FROM A TEXTFILE FOR
EXAMPLE... AND THEN CALL THE FUNCTION...
COULD I DO THIS USING THE PROPERTY SOLUTION YOU SUGGEST
ME ???

Don't cry!
SORRY FOR MY BAD ENGLISH... I'M AN ITALIAN NEW VB.NET
USER...

;->
 
M

Marc Scheuner [MVP ADSI]

I THINK THIS IS NOT THE RIGHT SOLUTION FOR ME...
I WANT TO SET THE TEXT PROPERTY OF A TEXTBOX IN A FORM,
PASSING TO A FUNCTION THE NAME OF THE FORM, THE NAME OF
THE CONTROL AND THE VALUE TO SET INTO IT...
I READ THE NAME OF THE CONTROL FROM A TEXTFILE FOR
EXAMPLE... AND THEN CALL THE FUNCTION...
COULD I DO THIS USING THE PROPERTY SOLUTION YOU SUGGEST
ME ???

You probably could, if you really wanted to - it might be easier to do
this in a function, though:

public void SetTextboxText(string aFormName, string aControlName,
string aTextValue)

and then have that function use Reflection to go find the form, find
the control on the form, and set that control's value to the value
passed in.

My thinking was more along the lines of

* you have a form class, e.g. MyForm
* you want to expose two or three captions that can be set
==> one property per caption

public class MyForm : System.Windows.Forms.Form
{
// .... lots of stuff snipped
// properties
public string Textbox1Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

public string Textbox2Text
{
get { return textBox2.Text; }
set { textBox2.Text = value; }
}
}

Using it, you could then do something like:

MyForm m_oForm = new MyForm();
m_oForm.Textbox1Text = "Some Text";
m_oForm.Textbox2Text = "Some other Text";

In such a case, properties are the way to go - they hide and
encapsulate the details of the implementation, and give you the choice
and chance to change implementation details later on if need be.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
G

Guest

Great !!!
This is the solution I was looking for...
Thank You very much Herfried K. Wagner
Bye
nq
 

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