Accessing public property on multiple instances of open forms

N

nottarealaddress

I'm trying to get my feet wet in VB2005 (our new standard at work
after officially stopping new development in VB6 about a month ago).
I'm working with a simple sql 2005 table of 50 entries, one for each
state. Each entry contains Name, postal abbreviation, etc. Just simple
stuff to understand the mechanisms, syntax, etc.

I'm now to the point where I've got a Master MDI form that opens one
or more types of forms, I'm able to edit/save the data and even return
a "Fail" if another user has altered the record by checking the
rowversion/timestamp field.

My current task is trying to poll all the open forms to check a
property unique to that type of form so I can exclude it from the new
open list.

My frmState2 has a public property of .Key which consists of the two-
letter USPS state code; AL, AK, etc. When a new instance of frmState2
is created, I set frmState2.Key to the two letter code and when the
new instance of the form loads, it gets the Me.Key property and is
able to take it from there.

Now, in the frmOpen's routine to fill the Listbox of state names, I'd
like to tack on a WHERE clause to the SQL statement to exlude the
states already open. (The first time through all 50 states show up,
the next time frmOpen is invoked and fills its listbox, it should only
present 49 states, etc.)

To do this I should (be able to) ripple through the open forms and
compile a list of state codes. My problem is accessing the .Key
property.

I'm currently trying:
For Each f As Form In My.Application.OpenForms
If f.Name = "frmState2" Then
MsgBox(f.Key) 'Just to check what I'm referencing
for right now.
End If
Next
I put in the qualifier checking the f.Name since not every type of
form has a .Key property. However, the compiler won't even let me run
it because "'Key' is not a member of 'System.Windows.Forms.Form'".

Multiple instances of frmState2 are not the only form(s) open, I'll
have the mastermdi and frmOpen as well, so not all the forms will have
a .Key property.

I could do this in VB6 because of the "If f.Name" qualifier. I'm going
to need this "type of thing" later to execute each instance's save
routine in case the user closes the application without saving to save
all the data.

There's got to be a way to reference all the instances of this
property!

-Scott
 
A

Armin Zingler

I'm currently trying:
For Each f As Form In My.Application.OpenForms
If f.Name = "frmState2" Then
MsgBox(f.Key) 'Just to check what I'm
referencing for right now.
End If
Next

For Each f As Form In Me.MdiChildren
if typeof f is frmState2 Then
MsgBox(directcast(f, frmState2).Key)
End If
Next


Armin
 
N

nottarealaddress

For Each f As Form In Me.MdiChildren
    if typeof f is frmState2 Then
        MsgBox(directcast(f, frmState2).Key)
    End If
Next

Armin

Thank you!
 
R

rowe_newsgroups

I'm trying to get my feet wet in VB2005 (our new standard at work
after officially stopping new development in VB6 about a month ago).
I'm working with a simple sql 2005 table of 50 entries, one for each
state. Each entry contains Name, postal abbreviation, etc. Just simple
stuff to understand the mechanisms, syntax, etc.

I'm now to the point where I've got a Master MDI form that opens one
or more types of forms, I'm able to edit/save the data and even return
a "Fail" if another user has altered the record by checking the
rowversion/timestamp field.

My current task is trying to poll all the open forms to check a
property unique to that type of form so I can exclude it from the new
open list.

My frmState2 has a public property of .Key which consists of the two-
letter USPS state code; AL, AK, etc. When a new instance of frmState2
is created, I set frmState2.Key to the two letter code and when the
new instance of the form loads, it gets the Me.Key property and is
able to take it from there.

Now, in the frmOpen's routine to fill the Listbox of state names, I'd
like to tack on a WHERE clause to the SQL statement to exlude the
states already open. (The first time through all 50 states show up,
the next time frmOpen is invoked and fills its listbox, it should only
present 49 states, etc.)

To do this I should (be able to) ripple through the open forms and
compile a list of state codes. My problem is accessing the .Key
property.

I'm currently trying:
For Each f As Form In My.Application.OpenForms
If f.Name = "frmState2" Then
MsgBox(f.Key) 'Just to check what I'm referencing
for right now.
End If
Next
I put in the qualifier checking the f.Name since not every type of
form has a .Key property. However, the compiler won't even let me run
it because "'Key' is not a member of 'System.Windows.Forms.Form'".

Multiple instances of frmState2 are not the only form(s) open, I'll
have the mastermdi and frmOpen as well, so not all the forms will have
a .Key property.

I could do this in VB6 because of the "If f.Name" qualifier. I'm going
to need this "type of thing" later to execute each instance's save
routine in case the user closes the application without saving to save
all the data.

There's got to be a way to reference all the instances of this
property!

-Scott

While Armin has shown you how to loop through the mdichildren and
inspect their types, I would prefer to not do this in the application.
Another option is to fire off an event in the mdi parent that the
various mdi children would subscribe too. Basically, the parent would
send out a "save now" message and any children who hear the message
would execute their save routines.

The main reason I like this pattern is that it enables the parent to
not care in the least what, if any, children it has. The saving is
handled by the children and the parent couldn't care less. To me it is
a much cleaner solution as it has much less coupling between the
different forms. It generally requires more work to initially set up,
but can save time down the road, say if you go from having one type of
form that needs saved to 5 different forms that need saved. Each would
just have to subscribe to the parent's "save now" event and handle
their save routines however they please.

Thanks,

Seth Rowe [MVP]
 
N

nottarealaddress

Seth,

Thanks for this information. I'll definitely look into it. I'm just
making the transition from VB6 to VB2005 and my first priority is "it
works". My second would be "it works better".

Having the children subscribe to the parent's event is going to be a
worthwhile to know because I can see it coming in handy in so many
ways--it's always good to know multiple ways of doing things.

Thanks,
-Scott
 

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