Run method in another windows form?

  • Thread starter Thread starter ritchiebuckley
  • Start date Start date
R

ritchiebuckley

I've been doing asp.net for so long now that I have forgotten about
windows form programming so excuse this (seemingly) basic question.

I have a form A which then opens up form B. I do stuff on form B after
which I hit a close button. I want to run a method on form A as I
close form B. I don't need to pass data back (though I like to know
how this is done), just run a refresh type method back on form B.

I've got bogged down in delegates and code samples, this must be easy
right?

thanks
 
I have a form A which then opens up form B. I do stuff on form B after
which I hit a close button. I want to run a method on form A as I
close form B. I don't need to pass data back (though I like to know
how this is done), just run a refresh type method back on form B.

I would imagine you really want access to Form B. If you prevent
the form from actually closing, and simply force it to Hide, the resources
will still be available and you can get your information back.

Further, if you show the Form B using ShowDialog, this step is handled
for you automatically. The form is not disposed fully, and in turn can
be reshown.

Then you can just access you information from properties.
 
Thanks for the reply, but in this case I don't need access to data from
the other form. Form B updates a datastore, then closes, then I just
want Form A to "Obtain the latest data". I have a method in form A do
to this, I just can't figure out a way to generate an event to run the
method.
 
Well, let's fix that up then:

public class FormA : Form {
private void DoIt() {
FormB b = new FormB();
b.DataUpdated += new EventHandler(DataStore_Updated);
b.Show();
}

// Have a handler for the event
}

public class FormB: Form {
public event EventHandler DataUpdated;

private void OnDataUpdated(EventArgs e) {
if ( DataUpdated != null )
DataUpdated(this, e);
}
}

// Just call OnDataUpdated when you are closing
}

Another option is an eventing business layer... If you are performing
multiple actions though, this can result in more than a single call. The
alternative is to add a transactional model so that you get a token when
you start updating, and you use that to end your update, then the event
that the data store is updated is fired.
 
Should probably add some invocation logic just in case:

private void DataStore_Updated(object sender, EventArgs e) {
this.BeginInvoke(...); // This ensures that if the event is being invoked on
another thread, very unlikely, but hell.
// Another item is, you may want the event to be invoked after Form B has
completely closed, in which
// case you want the async behavior of BeginInvoke anyway.
}
 
Justin,
I thought monday morning may help me but alas not. This is what I have
so far along the lines you suggested so I hope you can have a quick
check for me. It won't compile - it tells me that getCurrentPrinters()
does not match delegate - void System.EventHandler(object,
System.EventArgs)

public class FormA : Form {
private void lnkAdd_LinkClicked(object sender,
System.Windows.Forms.LinkLabelLinkClickedEventArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpdated += new EventHandler(getCurrentPrinters);
addform.Show();
}

private void GetInstalledPrinters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(EventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(this, e);
}
}

public void btnAdd_Click(object sender, System.EventArgs e)
{
OnDataUpdated(e);
this.Close();
}
}
 
Justin,
I thought monday morning may help me but alas not. This is what I have
so far along the lines you suggested so I hope you can have a quick
check for me. It won't compile - it tells me that getCurrentPrinters()
does not match delegate - void System.EventHandler(object,
System.EventArgs)

public class FormA : Form {
private void lnkAdd_LinkClicked(object sender,
System.Windows.Forms.LinkLabelLinkClickedEventArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpdated += new EventHandler(getCurrentPrinters);
addform.Show();
}

private void GetInstalledPrinters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(EventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(this, e);
}
}

public void btnAdd_Click(object sender, System.EventArgs e)
{
OnDataUpdated(e);
this.Close();
}
}

The error message is correct. The getInstalledPrinters requires 2 parameters
(object sender, EventArgs e). When using delegates the VS IDE will create
the function for you to avoid these mistakes. After typing
"addform.DataUpdated +=" you press Tab once to create the "new EventHandler"
and the second tab will create the function with the correct parameters.

SP
 
GetInstalledPrinters do not requires 2 parameters.

SP,

Basically make sure that your event handler method getCurrentPrinters has
signature of (object, EventArgs e)

Thanks,
Ashish
 
Simply create a public static variable of the Form class in a class
called Variables

public class Variables
{
public static Form f;
}

In Form A, before moving to Form B, create a reference for FormA as
follows

public class Form1
{
public Form1(){
f=this;
}
}

and in Form B, in the Form_Closing event call the method in Form A as
follows

public void Form2_Closing(....){
f.MethInFormA();
}


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
But somewhere there is probably a method getCurrentPrinters that is being
invoked by the EventHandler delegate and does not have the correct method
signature.

I confused this with the GetInstalledPrinters method.

SP
 
Please modify my earlier code as follows

public Form f;

In FormA before calling on FormB's Show method,

f=this;

In FormB, in the Form_Closing event procedure, use this code

f.MethodInFormA();


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top