473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Run method in another windows form?

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

Nov 16 '05 #1
12 13106
> 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.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
Nov 16 '05 #2
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.

Nov 16 '05 #3
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.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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.

Nov 16 '05 #4
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 Rogers" <Ju****@games4dotnet.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
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.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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.


Nov 16 '05 #5
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();
}
}

Nov 16 '05 #6
SP
<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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
Nov 16 '05 #7
GetInstalledPrinters do not requires 2 parameters.

SP,

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

Thanks,
Ashish

"SP" <egatsecneserp(reverse)@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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

Nov 16 '05 #8
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9
SP
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

"Ashish" <no*****@thisaddress.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
GetInstalledPrinters do not requires 2 parameters.

SP,

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

Thanks,
Ashish

"SP" <egatsecneserp(reverse)@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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


Nov 16 '05 #10
SP
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

"Ashish" <no*****@thisaddress.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
GetInstalledPrinters do not requires 2 parameters.

SP,

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

Thanks,
Ashish

"SP" <egatsecneserp(reverse)@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
<ri************@orcon.net.nz> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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


Nov 16 '05 #11
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ashish | last post by:
GetInstalledPrinters do not requires 2 parameters. SP, Basically make sure that your event handler method getCurrentPrinters has signature of (object, EventArgs e) Thanks, Ashish
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
1
by: Richard | last post by:
I think I'm going brain dead on this one. Can someone give me an idea on how to do this? Some sample code would be helpful.
4
by: hufaunder | last post by:
I would like to run a windows form application (app2) within another windows form application (app1). app2 should be displayed within a certain area of the windows form of app1 (could be inside a...
1
by: SadikZ | last post by:
Hi everyone, I want to transfer my data from windows form of Visual basic .Net 2000 to another windows form of Visual basic .Net 2000, but i am not getting the code which i have to write...
0
by: oz | last post by:
Hi All, I want to make a dictionary with windows application. My data is html format. therefore, i use webBrowser control on my windows form. If the user click any word in webBrowser control, word...
1
by: Pumpkin Carver | last post by:
I have a form that has a listview on it and a serious of strings in the listiew. When i doubel click on the listview item it opens a new form and displays the text that i pass to the constructor....
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.