How to fire sub on parent form?

M

Mark Lam

Hello,

I have a form (cmDeptList) that contains a list of entries from a database,
and a separate form I use to add entries to the database. The separate form
is launched when the user clicks a button as follows:
Dim fAddDept as New cmDeptEdit(True)
fAddDept.MdiParent = Me.MdiParent
fAddDept.Activate()
fAddDept.Show()

So far so good. However, once the user has successfully added the entry and
closes fAddDept, I want to run the Sub that updates the list on cmDeptList
so that the new entry is shown. I *think* have I have to pass a reference
to cmDeptList to fAddDept to do this, but I'm not sure how to go about doing
that. Any ideas?

Thanks!

Mark
 
J

Jeremy Cowles

Mark Lam said:
Hello,

I have a form (cmDeptList) that contains a list of entries from a
database,

cmDeptList ?! Why the cm prefix? A good naming convention I came across (in
Practical Standards for Visual Basic.NET) is to name the object with Form as
a suffix, then name the instance with frm as a prefix hence :

Object: DepartmentListForm
Instance: frmDeptList
Example: Dim frmDeptList as New DepartmentListForm( )

and a separate form I use to add entries to the database. The separate form
is launched when the user clicks a button as follows:

So far so good. However, once the user has successfully added the entry and
closes fAddDept, I want to run the Sub that updates the list on cmDeptList
so that the new entry is shown. I *think* have I have to pass a reference
to cmDeptList to fAddDept to do this, but I'm not sure how to go about doing
that. Any ideas?

Why not just do this:

Dim fAddDept as New cmDeptEdit(True)
fAddDept.MdiParent = Me.MdiParent
fAddDept.Activate()

'// SHOWDIALOG( ) INSTEAD OF SHOW( )
fAddDept.ShowDialog( )

'// Now just refresh the list
me.RefreshList( )

You could even add a DialogResult that indicates weather or not an update
was made (add this in the AddDept form, then read it from ShowDialog's
return value)

HTH,
Jeremy
 
M

Mark Lam

Jeremy,

"cm" is just a shorthand I use for the actual .vb file name. I'll admit
that I have my own goofy naming conventions - where can one find the
Practical Standards? Is that at MSDN?

As for ShowDialog - thanks for the tip! That's what I was looking for.

Mark
 
P

Paul Hetherington

ShowDialog works for this case.
In some of my work I didn't want a modal dialog so an alternative solution
is to use an event.
Raise the event in your child form and handle it in your Parent form.
-paul
 
J

Jeremy Cowles

Paul Hetherington said:
ShowDialog works for this case.
In some of my work I didn't want a modal dialog so an alternative solution
is to use an event.
Raise the event in your child form and handle it in your Parent form.

Good point (and good solution).
 
M

Mark Lam

Paul and Jeremy,

I can see the use of raising and event and handling it - I'll keep that one
in mind for the future. However, the behavior of the modal dialong is
ALMOST exactly what I'm looking for, with one major exception.

The form that calls the ShowDialog method (cmDeptList), is itself an
MdiChild. In the following code:

as is doesn't work - I get an error stating that fAddDept can't be shown as
modal unless it's the top-most form, and that I have to make it the top most
by getting rid of the Parent relationship. Doing that is fine, except now
my fAddDept form is showing up as a whole new program on the Task Bar. I
know I can (and have, actually) set the form to not display in the Task Bar,
but isn't it now running in its own thread, while at the same time tieing up
the original application until it (fAddDept) is closed out? That's not
really the behavior I want. Any way around this?

Thanks,

Mark
 

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