Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to update one form from another form

Status
Not open for further replies.

mkell1

Programmer
Mar 20, 2003
15
US
I am trying to update a grid control (refresh with a new data source) on one form after saving some values to the database via another form. The main form that contains the grid control remains open while the second form is opened, data is entered and saved to the database. Once the data is saved, I would like the grid on the first form to be requeried and refreshed. It was simple to access a control on another form within VB6, but I am having some difficulty determining how to accomplish this in .NET. Any guidance would be greatly appreciated.
 
But that creates a new instance of the form - it doesn't give me access to the controls that are currently open on the form that I am trying to access. The code runs, but does not update the actual grid.

I tried to create a shared method within the first form (where the grid resides) to call from the second form to update the grid control, but I get the "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class." error on the control and variable names that exist on the form. Maybe I am overcomplicating things, but I am not quite sure how to create an explicit instance of the class so that I can properly refer to these controls that are currently open.

In VB6 it was so easy - just [FormName].[ControlName] from any other form in the project.

Anyone have any ideas?
 
Mike

I presume you know that a property or public scoped variable on form2 can be accessed by form1 even after form2 has closed.

Are you opening form2 as dialog?. If so the solution is fairly simple. However if you are doing a form2.show, then you will probably need to add an openchildform event to form1, which will subsequently fire a childformclosed event on form1, when form2 closes.

Let me know what you are trying to do, and I'll post some simple code.




Sweep
...if it works dont mess with it
 
To add Event Handlers for Child Form Closing, you can add the following code to form1. This is similar to the code I have in my baseclass form.

Code:
Public Sub zOpenChildForm(ByVal f As form)

Me.oChildForm = f
AddHandler f.Closed, AddressOf oChildForm_Closed
RaiseEvent OpenChildForm(f)

End Sub

Private Sub oChildForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs)

Dim f As form = sender
RaiseEvent ChildFormClosed(f)

End Sub


....so to call form2 from form1
dim f as new form2
Me.zopenchildform(f)

....then when form2 is closed the ChildFormClosed event fires in form1. So you can enter code in here to requery/rebuild grid.




Sweep
...if it works dont mess with it
 
I'm trying to do the same thing as mkell1
and i'm trying to understand your code Sweep...
but i don't quite understand... how are the
events (OpenChildForm) and (ChildFormClosed)
being declared in form1?

can you be a little more specific in your code?
thanks
-d
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top