473,889 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3566
* "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=uh8 7DfymDHA.3504%4 0TK2MSFTNGP11.p hx.gbl>

--
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=uh8 7DfymDHA.3504%4 0TK2MSFTNGP11.p hx.gbl>

--
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.c om> wrote in message
news:%2******** ********@TK2MSF TNGP11.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.The Form.SomeMethod
and similarly the TextBox
sGotcha = frmVB6Style.The Form.txtImporta nt.Text
or using the Shared reference
sGotcha = frmVB6Style.oTe xtBox.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
3001
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 component class, I drop an ImageList.
6
2588
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 user control folder so that they can be selected and loaded at run time also. These new controls will need to have access to many of the classes with there properties and methods within the complied code. Is this possible? This seems to be the...
1
2425
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 EditStuff.ascx which reads from an xml file and loads controls to its self based on string value in xml nodes collection of the xml. There are several controls that can be loaded and for each one there exists a public method called 'IntiControl'...
0
12093
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
4
3645
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 possible.. for example..i hav a menu in the parent form named "Administrator" whic has an item "mnuLogIn"..now when i click on login..another child form named "frmLogIn" is displayed..what i want to happen is this: when login form(frmLogIn) is...
2
2665
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
2656
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 to another location because it populates a Textbox in the user control page. Thanks Andrew
8
4200
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 access one of those properties I've shown an error message saying that the Object doesn't support this property or method. Here's the code I'm using (edited to protect the third party NDA) 1: <% 2: dim siteApi, userApi
0
1437
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 configured, rather than take the easy way out and put a fixed amount of controls on the form at design. Makes it more flexible. At the moment the way I am trying to do it is adding instances of the control into a collection with their individual...
0
9962
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9810
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10791
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10442
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9609
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5829
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.