Calling sub from another form

bmackkc

New member
Joined
Jan 12, 2007
Messages
4
Programming Experience
1-3
I have two forms; I'll call them frmReturn and frmReturnItem. FrmReturn is bound to data in a SQL table; data entered in frmReturnItem is stored as a child table in SQL. In the database, there is a one-->many between Return and ReturnItem. frmReturnItem can be opened only from frmReturn.

frmReturn has a datagrid which displays the summary of the associated ReturnItems.

Problem: I would like the frmReturn's grid to refresh every time frmReturnItem is saved. I can post my existing code if that will help.

Thanks,

Mike
 
Use interface to refresh parent form

Hi bmackkc,
You have to use Interface to refresh the parent form(frmReturn) from the child form(frmReturnItem)

1. Declare an interface with a subroutine to refresh datagrid

Public Interface IRefreshParent
Sub RefreshDataGrid()
End Interface
2.In frmReturnItem,declare a new variable and make a new constructor

Public Class frmReturnItem
.
.
.
Public Irefresh as IrefreshParent
Public sub New (ByVal _irefresh as IRefreshParent)
' assign _irefresh to Irefresh
Irefresh = _irefresh
end sub

Public Sub DataSaved()
' if data is saved successfully call the interface's method
IRefresh.RefreshDataGrid()

end Sub

.
.
.
End Class
3. Implement RefreshDataGrid in frmReturn

Public Class frmReturn
implements IRefreshParent

.
.
.

Public Sub Refresh() implements IRefreshparent.RefreshDataGrid()
' call the databinding code for the datagrid here
BindDataGrid
end sub


private sub BindDataGrid()
DataGrid1.DataSource = mydataset.tables(0).defaultview
DataGrid1.DataMember = "TableName"
end sub
.
.
'Always call the frmReturnItem by the new constructor newly created if you want to show frmReturnItem
'You are passing frmReturn as type IRefreshParent

Public sub ShowfrmReturnItem
Dim myform as New frmReturnItem(Me)
myform.Show()
end sub
.
.
.
end class
Hope this helps

Sincerely,
Vikrant.
 
You have to use Interface
You don't have to, it is an option. Unless you need to interface different types of forms through a common interface I would say you should not use Interface.

The alternatives are same as with all interaction between class instances, use reference to the other class instance and:
1) use a property
2) use a method
3) use an event

In this case you can DirectCast the child forms Parent property into the only form type it is, namely FrmReturn, on which you call its type specific method to make the refresh happen (like abhiramvikrants RefreshDataGrid method).

Or, you can declare an event in child form, for example SaveHappened, and raise this event in child form when needed. The parent form would subscribe to this event for each child it creates with the AddHandler keyword, a common event handler can handle all childs.

I would go for the event model here.
 
Thanks to you Vikrant and John for the replies. John, could you post some sample code (hopefully 1.1)? I have not used Events before.

Thanks

Mike
 
I may have misunderstood your description about one-to-many in first post, if you open frmReturnItem as a dialog you should either use the dialog return result to refresh or a property of the dialog form to determine whether to refresh parent.

I will use some simple code and give examples of each scenario (4 numbered). The actual refresh code I leave up to you, here it is simply written 'me.datagrid1.refresh'.

1. Dialogs, parent open and use dialogresult:
VB.NET:
Dim f As New frmReturnItem
If f.ShowDialog = DialogResult.OK Then me.datagrid1.refresh
f.Dispose()
2. Dialogs, parent open and use child form property:
VB.NET:
Dim f As New frmReturnItem
If f.ShowDialog = DialogResult.OK Then 
  If f.WasSaved = True Then me.datagrid1.refresh
End If
f.Dispose()
frmReturnItem would here when a save happened internally set a private boolean flag, through public property this flag is returned to those asking for it:
VB.NET:
Private Sub somethingCausedSave()
  Me.SavedFlag = True
End Sub
 
Private SavedFlag As Boolean
 
Public ReadOnly Property WasSaved() As Boolean
Get
  Return SavedFlag
End Get
End Property
3. Multiple forms (not dialog), using a parent form method. Parent code:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim f As New frmReturnItem
  f.Owner = Me
  f.Show()
End Sub
 
Public Sub RefreshMe()
  me.datagrid1.refresh()
End Sub
child code:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim p As frmReturn = DirectCast(Me.Owner, frmReturn)
  p.RefreshMe()
End Sub
(if the owner form here could be different type forms you would do as abhiramvikrant and let each caller form implement the interface, Owner could then be cast to interface type IRefreshParent and no matter what actual owner form it was you could call the RefreshMe method.)

An added benefit of setting the Owner is that child forms minimize/restore and close when the parent does, but may get in the way, if that is a problem use another property for this purpose.

4. Multiple forms (not dialog), using event. Child code:
VB.NET:
Public Event WasSaved()
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  RaiseEvent WasSaved()
End Sub
parent code:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim f As New frmReturnItem
  AddHandler f.WasSaved, AddressOf RefreshMe
  f.Show()
End Sub
 
Public Sub RefreshMe()
  me.datagrid1.refresh
End Sub
 
Hi bmackkc and JohnH,
thanx for your reply bmackkc,Thanx johnHfor ur code, I went thro' it and it is cool,but , I got a doubt here with the line
Dim p As frmReturn = DirectCast(Me.Owner, frmReturn)
p.RefreshMe()
Above code gets tightly coupled with frmReturn and I would like to raise the following doubts
1. Won't casting cause resources?
2. If interfaces are not used here, why should we have the concept interfaces?(Apart from multiple interface, No-Implementation BLAH BLAH!)
3. I think casting the forms will cause more memory than using interface.



Nothing offensive johnH , just trying to learn all the perspectives and I'm open to all suggestions
 
Neither casting or using interface take any resources, both are simply used to give the compiler a different perspective of the current object.

Why do we have interfaces? To enable handling instances of different types as one common type.
 
Got your point JohnH , I couldn't get these from books.........Nice discusiing with u..........
Btw, is there anyway we can see memory used dynamically while debugging
Like we see threads, Command , Autos and local at debug mode
 
Not accurately as I know of, memory usage is also a thing of its own with .Net, where the memory is managed and not controllable from the client app. (of course don't waste resources :)) You can see with Windows Task Manager, the Process tab lists the processes including the debugging app, but you will see aeons of memory usage reported that is actually loose allocations, minimize the application and the "memory usage" gets down to probably somewhere near actual current usage.
 
Back
Top