By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Accessing form properties from another form at runtime

P: n/a
In VB.NET, need to be able to access certain properties on my main form from
other forms. These are properties that may be changed by the user, so I
have to be able to get to them throughout the application. How can I access
the current instance of a specific Form object? For example:

Class frmOne
.......

Public Readonly Property XYZ() as String
Get
Return Me.txtXYZ.Text
End Get
End Property

End Class

Class frmTwo
.......

Sub GetXYZProperty()
Dim XYZ as String = {frmOne ?}.XYZ
End Sub

End Class
Thanks in advance,
Mike
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* "Mike Caputo" <mi************@radarwire.com> scripsit:
In VB.NET, need to be able to access certain properties on my main form from
other forms. These are properties that may be changed by the user, so I
have to be able to get to them throughout the application. How can I access
the current instance of a specific Form object? For example:


<http://www.google.de/groups?selm=uh87DfymDHA.3504%40TK2MSFTNGP11.phx.gb l>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
Thanks Herfried, but I have to do it a different way. The form I will be
accessing is the Main form, which will already be open. I need to be able
to access properties on that instance of the form from several other forms.
The only other thing I can think of is explicitly declaring a global
variable and updating that whenever the value of the textbox on the Main
form changes, but I was hoping for a more dynamic way of doing it. Thanks
for trying to help, though.

Mike

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn*************@ID-208219.news.uni-berlin.de...
* "Mike Caputo" <mi************@radarwire.com> scripsit:
In VB.NET, need to be able to access certain properties on my main form from other forms. These are properties that may be changed by the user, so I
have to be able to get to them throughout the application. How can I access the current instance of a specific Form object? For example:


<http://www.google.de/groups?selm=uh87DfymDHA.3504%40TK2MSFTNGP11.phx.gb l>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
* "Mike Caputo" <mi************@radarwire.com> scripsit:
Thanks Herfried, but I have to do it a different way. The form I will be
accessing is the Main form, which will already be open.


What's the problem? Just extend the other forms as shown in the sample
and instantiate them in the main form. By modifying the type of the
constructor you can even pass the whole form to the sub form.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
I realize I could do it that way, but I was just hoping to be able to access
the property directly somehow. That way, there's no question about getting
the current value (whereas if I pass it in the constructor, it's then static
and if the user changes the value, it won't be updated), and I don't have to
always pass the value to other forms (since the procedure I'll be doing
opens a third form from the second form which was instantiated by the main
form). I also thought this would just be a nice ability to have, in case I
come across some other application for it. I take it from your replies and
the lack of any others that there's no way to do it the way I was hoping, so
I think I'll just store the values in a global variable that's updated on
the Changed event of the textboxes. Thanks for your help.

Mike

--
Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.
847-272-7691
mi************@radarwire.com

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bn*************@ID-208219.news.uni-berlin.de...
* "Mike Caputo" <mi************@radarwire.com> scripsit:
Thanks Herfried, but I have to do it a different way. The form I will be
accessing is the Main form, which will already be open.


What's the problem? Just extend the other forms as shown in the sample
and instantiate them in the main form. By modifying the type of the
constructor you can even pass the whole form to the sub form.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
Hi Mike,

There is of course a way to do what you want - this is VB.NET - how many
ways to skin a cat?

One way is to pass frmOne to the other Forms when they are constructed.
This way they can access XYZ at will. It will always be the last copy because
they are getting from frmOne each time.

The snag with this approach is in its lack of flexibility. Fine for now,
but later? Suppose you then create a frmPlus (not inherited from frmOne) which
has a similar value PQR. Now, in your other Forms you may want frmOne and its
XYZ <or> frmPlus and its PQR. So do you have a constructor which takes a
frmOne or a frmPlus or both or hat?

The best thing for this is an Interface which frmOne and frmPlus
implement. The other Forms would then accept any Form that conforms to that
interface.

I'm going to stop there and ask if this sounds interesting as a solution.
If so, I'll explain more and give some examples. If it's just leading you into
a confusing area, it's best left until later. But mainly because there are
other cats...

=========================
Another way is very straightforward and will be reminiscent of VB6. In VB6
a Form and it's class were both accessible using the same name. frmOne.XYZ was
legitimate because it <was> the Form.

In VB.NET, you can have Shared properties of a Form class which will be
accessible to all other classes using the class name. frmOne.XYZ will be
legitimate again. But! This method <only> works if there will be only one
frmOne. If you have two or more, they will all be utilising the same XYZ -
because it's Shared, of course.

=========================
Another way. You have a Delegate which contains a reference to a method of
frmOne that provides the value of XYZ. Then you can pass this Delegate to the
other Forms. When they neex XYZ, they call the Delegate and frmOne gives sends
it back.

=========================
Another way (LOL) - Each of the Forms that want XYZ can raise an event
calling out for it. frmOne would subscribe to these events and, on hearing a
call, would pop XYZ into the mouth of the hungry Form (I'm thinking of chicks
in the nest here).

=========================
And another way - This time frmOne has the event and raises it when XYZ
changes. The <other> Forms subscribe to frmOne's event and when XYZ changes,
they are called and given the new version. This one's the newspaper boy in the
morning.
So, of all that lot (and I bet there are some more), tell me is anything
sounds interesting and useful to you. ;-))

Regards,
Fergus
Nov 20 '05 #6

P: n/a
"Fergus Cooney" <fi*****@post.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

<snipped the first part, I thought the same thing>
The best thing for this is an Interface which frmOne and frmPlus
implement. The other Forms would then accept any Form that conforms to that interface.

I'm going to stop there and ask if this sounds interesting as a solution. If so, I'll explain more and give some examples. If it's just leading you into a confusing area, it's best left until later. But mainly because there are
other cats...
This is a good area to explore in the future, but I like your other ideas
better.... -->
=========================
Another way is very straightforward and will be reminiscent of VB6. In VB6 a Form and it's class were both accessible using the same name. frmOne.XYZ was legitimate because it <was> the Form.

In VB.NET, you can have Shared properties of a Form class which will be accessible to all other classes using the class name. frmOne.XYZ will be
legitimate again. But! This method <only> works if there will be only one
frmOne. If you have two or more, they will all be utilising the same XYZ -
because it's Shared, of course.
This is exactly what I want to do... I'm a VB6 programmer at heart, and this
is really my first foray into .NET, so I'm still basically building it
around the idea of using each form once. Can't believe I didn't think of
this before.
=========================
Another way. You have a Delegate which contains a reference to a method of frmOne that provides the value of XYZ. Then you can pass this Delegate to the other Forms. When they neex XYZ, they call the Delegate and frmOne gives sends it back.
This is still kind of static, like the first idea of passing the whole form,
but another good idea to have for the future.
=========================
Another way (LOL) - Each of the Forms that want XYZ can raise an event
calling out for it. frmOne would subscribe to these events and, on hearing a call, would pop XYZ into the mouth of the hungry Form (I'm thinking of chicks in the nest here).
Same.
=========================
And another way - This time frmOne has the event and raises it when XYZ changes. The <other> Forms subscribe to frmOne's event and when XYZ changes, they are called and given the new version. This one's the newspaper boy in the morning.


I was going to do something along these lines, with a global variable that
was modified whenever the value in frmOne changed. This would work too, but
I think the best solution is your idea of a Shared property, so that's what
I'm going to do. Thanks a lot for your help.

Mike
Nov 20 '05 #7

P: n/a
Well, it looks like I can't use a Shared property, since I need to return a
value from a Textbox, which is only available after instantiation. I guess
I'll try one of the other methods you suggested. Thanks a lot.

Mike
Nov 20 '05 #8

P: n/a
Hi Mike,

|| > have Shared properties of a Form class
||
|| This is exactly what I want to do...
|| I'm a VB6 programmer at heart,
|| Can't believe I didn't think of this before.

I kind of went from VB4 straight to VB.NET with a brief dip into VB6 (and
another back to QBasic!) but I've always thought OOPish, even in VB3. This
method of using Shared only occurred to me a couple of weeks ago!! because
'it's just not the done thing'. But with single-instance Form classes it makes
good pragmatic sense.

You can even use it to have your Form available globally. And its
Controls, such as your TextBox. What you need is a Shared reference to the
Form, and optionally, to all the other items that you want to share. Then you
initialise them at the first opportunity.

Public Class frmVB6Style As Form
Public Shared TheForm As VB6Style
Public Shared oTextBox As TextBox

Public Sub New
TheForm = Me
oTextBox = txtImportant

'Auto gen stuff
Friend txtImportant As TextBox
'this, that and the other Controls...

And in FormX or ClassY you can access any available method
frmVB6Style.TheForm.SomeMethod
and similarly the TextBox
sGotcha = frmVB6Style.TheForm.txtImportant.Text
or using the Shared reference
sGotcha = frmVB6Style.oTextBox.Text

Just as long as you know that you're compromising the OOP principles for
pragmatic and ease-of-transition purposes. (Or any other justification that
you care to make up, lol).

Regards,
Fergus
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.