Fire event

J

Jerry

Hi,

My app is controled by a treeview.
Each node brings a subform for input and calculations to the front. The
subforms are loaded as controls on the main form.

Dim ctl As Control

For Each ctl In Me.Controls
If TypeOf ctl Is Form Then
If ctl.Tag.ToString = sTabKey.ToString Then

ctl.BringToFront()
ctl.Show()

End If
End If
Next

Which event can I use to fire my update sub on the subform when the subform
is showen? I tryed gotfocus, but it didn't work.


Thanks,

Jerry
 
T

tommaso.gastaldi

Why not to use a form method:

ctl.BringToFront()
ctl.Show()
ctl.MyUpdateSub(Args)

-tom

Jerry ha scritto:
 
J

Jerry

I tryed that Tom,
it didn't work. The main form says MyUpdateSub is not a member of
system.windows.form.control. Can I change that?

Jerry
 
T

tommaso.gastaldi

:)
of course it isn't. You are supposed to create it. Make sure it will be
visible outside the form
(public / friend ...).

Public Sub MyUpdateSub()
'...
'your update code
End Sub

-tom

Jerry ha scritto:
 
J

jeff

And if you want to keep your code generic in your loop - as you have below -
I would create a base class - MySubForm (inherited from
System.Window.Form) - add the MyUpdateSub procedure (or a function or
event - for this example it is a procedure) and inherit all your 'subforms'
from this class. That way you will ensure that all your sub-forms will have
a call to MyUpdateSub ... and you can still utilize your 'generic' control
loop from finding / displaying / updating the select treenode form...

So...

1. Create a new form in your project ... Call it something like
mySubFormBase

2. Add a PUBLIC OVERRIDABLE procedure / event ... MyUpdateSubFrom ... or
whatever ... MySubFormMakeVisible
Public Overridable Sub MySubFormUpdate()

End Sub

3. Save the form...

4. If you use namespaces ... add the mySubFormBase to your base namespace...

5. Now, BACKUP YOUR CURRENT FORMS ... make copies of them ...

6. For each of you sub forms ... open the <SubFormName>.designer.vb file...

7 REPLACE THE LINE:

INHERITS System.Windows.Forms.Form

with

INHERITS <yourProjectName>.MySubFormBase
or
INHERITS <yourProjectName>.<yourNameSpace>.MySubFormBase

where <yourProjectName = the name of your project
and
where yourNameSpace = the name of your namespace

8. Change your code in the your loop to ...

Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is <myProjectName>.MySubFormBase Then
If ctl.Tag.ToString = sTabKey.ToString Then
ctl.BringToFront()
ctl.Show()
ctl.MyUpdateSubForm() ' If Procedue ...
End If
End If
Next

9. In each subform code the following procedure...

Public Overrides Sub sMySubFormUpdate()

MyBase.MySubFormUpdate()

End Sub

This will work ...

now in all your subforms ... simply ...override the mySubFormUpdate
procedure with the appropriate code...

Public Class Form1

Public Overrides Sub MySubFormUpdate()

MyBase.MySubFormUpdate()

' Your code here ... update code

End Sub

End Class

If you take the approach and create a MySubFormUpdate procedure for each of
you sub forms, I am not sure how you would reference / call this from within
your Control Loop where ctl is a system.windows.form object ... Not sure how
you would 'dynamically' reference the procedure ?

JEff
 
J

Jerry

Thanks Tom,
but I had tried that too. Didn't work....

Jeff, this looks nice. I'll try this first thing tomorrow. Thanks for your
effort and yours too Tom.

Jerry
 
J

Jerry

Jeff,

just got through reading the rest. Your right, how am I going to reference
the others. This looks very good.

Thanks,

Jerry
 
J

jeff

The others? Other sub forms ... as I mentioned, you will need to change all
the subform parent class inorder for this work ... you can not selectively
choose to do it for some forms and not others that you want to control with
your treeview...otherwise you will need to change your control's loop ...

the 'ctl's that you loop through must be from the same 'base' class ...
create a base class form, add your 'overridable' procedure, and when you
iterate through the controls on a 'master' window looking for the 'subforms'
check for the following...

If TypeOf ctl Is <myProjectName>.MySubFormBase Then


Instead of ...

If TypeOf ctl Is System.Windows.Form Then

This will ensure...

a. you have a subform that has your procedure
b. generically code against your procedure.

Jeff.
 
J

jeff

For example ...

If you have two or more 'types' of these subforms ... you need to code
accordingly...

Instead of having a If TypeOf ... change it to ...

Select Case TypeOf Ctl

Case System.Windows.Form

Case <myProject>.MySubFormBase

Case ...

End Select

Since VB only knows that the procedure exists for the BaseSubForm's ... you
can only call it from the appropriate case ...

So, if you have 'seven' sub forms but only 3 need to use the update
procedure, than only those three need to inherit from you base class -
MySubFormBase ...

However, I would recommend 'inheriting' all you subforms from the same base
class form ... that way you will always have this functionality available.

IMHO, it is good practice to always inherit ALL your forms from a custom
base class .... even if in the beginning u do not intend on extending it ...
this will allow you to easily extend all your forms to include this type of
functionality ...

Jeff
 

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