Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing controls on previously opened forms 3

Status
Not open for further replies.

Coxylaad

Programmer
Jan 27, 2004
155
0
0
GB
Hi (again) - sorry i accidentally hijacked a thread there,

I am struggling trying to reference a text box on another form.

in MS access it was simply formname.controlname.text

if I try that in vb.net I get a error stating [formname]is not defined.

its doing me Heeed in! can someone help?
 
In .NET, forms are classes, and obey all the usual data scoping rules.

So, in order to access anything on another form, you'll have to first have a variable which contains a reference to that form (you typically save it from when you called new on it).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
dont suppose you could supply a quick example for me to work off could you ?

:)

thanks

Ian
 
It's pretty basic O-O stuff:
Code:
  Dim A As New Form1
  Dim B As New Form2

  B.TheReferenceToA = A
Form "B" now has a reference to A (via the property you wrote called TheReferenceToA that accepts a Form1), and is able to access all the public members of A.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
but if and instance of form A is already open then how do I reference that from form B?

I understand the principles what you are saying but I dont actually know the syntax to reference a textbox on another form.

Do you see what I am saying?

Ian
 
The textbox will be scoped as Private. You would need to scope it as public but this is not sensible. You really should put a public method on the second form to retrieve the value from.

Craig
 
but if and instance of form A is already open then how do I reference that from form B?

You can't do anything without a reference to Form A, and the way to get that is when the instance is created.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
ah I think I get it.
You set a property of form A to be the actual form itself. Then you can call the property and inherit all it controls.

Ian
 
Not "inherit" --- "gain access to" via their public properties/methods etc.

Which brings up the point that Craig0201 said -- by default, Textboxes, etc, are declared as Private. Meaning that code outside the form (aka class) aren't able to access them.

What you'll want to do is in the form create a public property called "TextBoxValue" that wrappers your private textbox. From a design standpoint, you don't want to make the textbox public (that would allow the caller to do unwanted stuff like reposition it on the form, disable it, change it's color, etc). You just want the caller to be able to read/change the value of the textbox. So you create a public property that allows them to do that, and nothing else.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
but how can the user reposition the textbox on the form when it is run as an executable?
 
It's about isolating your code.

You're the form designer, i'm the business object coder.

You declare a textbox as Public then I can access all properties of that textbox on your form, including colour, position, etc. I (not the user) can write code in my business object to do that. You keep the textbox as Private and create a Public ReadOnly Property so that I can read the value then that's all I can do.

Now, it sounds like you're just playing so you're both the form designer and business object coder. So you COULD declare the textbox as Public because you know you're not going to play with it. But it's poor practice.

In fact, even better would be to do it the other way round. Your textbox should push the value into the second object. That way, it's not dependant on knowing the name of your textbox, etc.

Craig
 
As a separate point....

Chip, you are one of the very few VB.Net coders on here that I respect. Most of the time, it's like listening to an Access forum. Why can't VB.Net users respect the power and use it properly???

Craig
 
I see what you are getting at craig.

Thanks for the info

Ian
 
Chiph & Craig0201, great insight! I was wondering too how I would achieve the same thing Coxylaad was trying to.

 
Craig,

Chip, you are one of the very few VB.Net coders on here that I respect. Most of the time, it's like listening to an Access forum. Why can't VB.Net users respect the power and use it properly???

I am new to .NET, so I always try to follow as much links as possible. So now I got one to follow always, do you know of any other guys here who always gives good/valid points in there threads.
 
netKID,

See thread796-825056

You'll see what I mean there. Lots of bad code followed by a little gem from sjnSomethingOrOther.

The best I can recommend aren't posters on here but a few books and a website.

1) A lot of stuff in Java and other OO languages but with a bit of perseverance can be easily translated to .Net
2) Programming VB.Net - Balena
3) UML Distilled - Fowler
4) Design Patterns - Gamma et al

And remember to learn things the right way round.

OO first. Then .Net. Then implementing .Net in VB.Net.

I started off learning some Access stuff in the same sort of way that is often posted on here. It worked to an extent but it wasn't neat, it wasn't easy, it wasn't updatable. But then I got educated and learnt there are often much better ways of dealing with things and that I was trying to build on rocky foundations. So I shored up the foundations then started to rebuild. I'm not there yet by any means but I now learn faster, code faster, think faster, build faster.
 
Thanks Craig for the suggestions, I will try to collect all the gems :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top