How one form can tell another to perform an action [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Okay - this is a fun one...
I have a main form (called frmApp with class cApp) which, when the user presses a certain button button, can open (show) another form (frmHistory with class cHistory).
Now both of these forms communicate with a common database [Access], the thing is the user can open the frmHistory form and make changes to the database and then close that form (and still be in the cApp form) but the Datagrid in cApp isn't Refreshing because it has no way of knowing that data has changed.

I need an efficient way for my cHistory class to "tell" my cApp class that it needs to refresh the datagrid.
Is there a way for my cHistory class to "launch an instantce" of a function in my cApp class to force cApp to refresh it's datagrid?
cApp already has a function in cApp (RefreshGUI()) that does all that I need, I just need to find a way to get cHistory to tell cApp that it needs to launch RefreshGUI() now.
I tried making it PUBLIC but in cHistory doing capp. doesn't give me the function - and even there I don't know if that would work?

Any help/hints would be appreciated, thanks
 
Visual Basic:
Public Class frmHistory
   Protected mAppInstance as cApp = Nothing

   Public Sub New(Byref cAppInstance as cApp)
   
                MyBase.New()

		'This call is required by the Windows Form Designer.
		IsLoading = True
		InitializeComponent()
		IsLoading = False

                mAppInstance = cAppInstance
    End Sub

Private Sub frmHistory_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    If Not mAppInstance is Nothing Then
        mAppInstance.RefreshGUI()
    End If
End Sub


Now, instead of declaring your frmHistory using:

Visual Basic:
    Dim frm as New frmHistory()

you use

Visual Basic:
    Dim frm as New frmHistory(reference-to-your-instance-of-cApp)

HTH!

Barry.
 
Thing is I don't want the event to only fire on CLOSE, I want it to fire whenever the user hits the REFRESH button in the HISTORY form.
Note that both HISTORY and APP can be open/showing at the same time.

So I guess I need to ... somehow ...
a) create an event in cAPP that will launch the RefreshGUI()
b) somehow in cHistory I need to launch that event from cApp

And I see you create a NEW instance of cAPP in cHistory to run your function
Won't this not work? Seeing as cApp is still Open/Running won't you just be creating another one?
 
You're thinking about the events going in a different direction.

You would need to

a. create an event in cHistory and fire it when the user clicks the Refresh button there.
b. attach an eventhandler for that event to the cHistory instance in cApp form that will call RefreshGUI() function.

Does this make more sense?
 
DimkaNewtown: Makes a lot more sense - only problem is I have no clue how to do it ... :) .... Can you provide an example?
 
If I'm understanding your question correctly this is what you are attempting todo. You have 2 forms, one called frmApp and another called frmHistory. The frmApp has a method RefreshGUI which you wish to call from the frmHistory.

The way I would do this is to pass an instance of cApp into the constructor of cHistory, then whenever you create frmHistory pass frmApp to it. Then simply create a local reference so that you can call frmApp.RefreshGUI() from anywhere on frmHistory.

C#:
public class cApp : System.Windows.Forms.Form
{
	private cHistory frmHistory;

	public cApp()
	{
		frmHistory = new cHistory(this);
	}

	public void ShowHistory()
	{
		frmHistory.Show();
	}
}

public class cHistory : System.Windows.Forms.Form
{
	private cApp frmAppRef;

	public cHistory(cApp frmTemp)
	{
		frmAppRef = frmTemp;
	}

	public void ButtonClicked()
	{
		frmAppRef.RefreshGUI();
	}
}
 
Back
Top