473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing form properties from another form at runtime

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
8 3532
* "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
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
* "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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J F | last post by:
Hi all I'm totally new to Visual Studio, coming from Delphi. My question, I'm sure is pretty trivial. I'm using C# I have a component class (Project/Add Class/Component Class). In this...
6
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
1
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
8
by: Kevin Blount | last post by:
I'm tyring to access an object created by using a method from a third party API. The documentation tells me what object should be return, and the properties of that object, but when I try and...
0
by: porksmash | last post by:
I'm developing an app here that uses ActiveX controls to connect to industrial cameras over Ethernet. I want to be able to dynamically create those controls at runtime based on how many cameras are...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.