473,835 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13152
> 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(Da taStore_Updated );
b.Show();
}

// Have a handler for the event
}

public class FormB: Form {
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e) {
if ( DataUpdated != null )
DataUpdated(thi s, 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.goo glegroups.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_Updat ed(object sender, EventArgs e) {
this.BeginInvok e(...); // 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****@games4d otnet.com> wrote in message
news:eG******** ******@TK2MSFTN GP12.phx.gbl...
Well, let's fix that up then:

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

// Have a handler for the event
}

public class FormB: Form {
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e) {
if ( DataUpdated != null )
DataUpdated(thi s, 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.goo glegroups.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 getCurrentPrint ers()
does not match delegate - void System.EventHan dler(object,
System.EventArg s)

public class FormA : Form {
private void lnkAdd_LinkClic ked(object sender,
System.Windows. Forms.LinkLabel LinkClickedEven tArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpd ated += new EventHandler(ge tCurrentPrinter s);
addform.Show();
}

private void GetInstalledPri nters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(thi s, e);
}
}

public void btnAdd_Click(ob ject sender, System.EventArg s e)
{
OnDataUpdated(e );
this.Close();
}
}

Nov 16 '05 #6
SP
<ri************ @orcon.net.nz> wrote in message
news:10******** **************@ c13g2000cwb.goo glegroups.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 getCurrentPrint ers()
does not match delegate - void System.EventHan dler(object,
System.EventArg s)

public class FormA : Form {
private void lnkAdd_LinkClic ked(object sender,
System.Windows. Forms.LinkLabel LinkClickedEven tArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpd ated += new EventHandler(ge tCurrentPrinter s);
addform.Show();
}

private void GetInstalledPri nters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(thi s, e);
}
}

public void btnAdd_Click(ob ject sender, System.EventArg s e)
{
OnDataUpdated(e );
this.Close();
}
}


The error message is correct. The getInstalledPri nters 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.DataUp dated +=" 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
GetInstalledPri nters do not requires 2 parameters.

SP,

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

Thanks,
Ashish

"SP" <egatsecneserp( reverse)@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
<ri************ @orcon.net.nz> wrote in message
news:10******** **************@ c13g2000cwb.goo glegroups.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 getCurrentPrint ers()
does not match delegate - void System.EventHan dler(object,
System.EventArg s)

public class FormA : Form {
private void lnkAdd_LinkClic ked(object sender,
System.Windows. Forms.LinkLabel LinkClickedEven tArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpd ated += new EventHandler(ge tCurrentPrinter s);
addform.Show();
}

private void GetInstalledPri nters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(thi s, e);
}
}

public void btnAdd_Click(ob ject sender, System.EventArg s e)
{
OnDataUpdated(e );
this.Close();
}
}


The error message is correct. The getInstalledPri nters 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.DataUp dated +=" 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.Ravichandra n
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandra n+J.V.&cob=aspn etpro
- 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.Ravichandr an"
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 getCurrentPrint ers that is being
invoked by the EventHandler delegate and does not have the correct method
signature.

I confused this with the GetInstalledPri nters method.

SP

"Ashish" <no*****@thisad dress.com> wrote in message
news:uH******** ******@TK2MSFTN GP12.phx.gbl...
GetInstalledPri nters do not requires 2 parameters.

SP,

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

Thanks,
Ashish

"SP" <egatsecneserp( reverse)@hotmai l.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
<ri************ @orcon.net.nz> wrote in message
news:10******** **************@ c13g2000cwb.goo glegroups.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 getCurrentPrint ers()
does not match delegate - void System.EventHan dler(object,
System.EventArg s)

public class FormA : Form {
private void lnkAdd_LinkClic ked(object sender,
System.Windows. Forms.LinkLabel LinkClickedEven tArgs e)()
{
AddPrinter addform = new AddPrinter();
addform.DataUpd ated += new EventHandler(ge tCurrentPrinter s);
addform.Show();
}

private void GetInstalledPri nters()
{
// do stuff
}
}

and in formB

public class AddPrinter : Form
{
public event EventHandler DataUpdated;

private void OnDataUpdated(E ventArgs e)
{
if ( DataUpdated != null )
{
DataUpdated(thi s, e);
}
}

public void btnAdd_Click(ob ject sender, System.EventArg s e)
{
OnDataUpdated(e );
this.Close();
}
}


The error message is correct. The getInstalledPri nters 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.DataUp dated +=" 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

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

Similar topics

0
1179
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
2827
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 facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
1
1906
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
2418
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 panel or similar). app1 does not need any control over app2 except for starting, closing, hiding and showing it. Basically, app1 should look like being a part of app2 despite the fact that they have no knowledge of each other (except the app name,...
1
1248
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 for extracting data, so please friends help me for that ........
0
958
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 mean must browse to user (like babylon). I have a windows form. My form has webBrowser control. I fill a html content with javascript (Dictionary.js) to webBrowser. Javascript (dictionary.js) can obtain the word if user double click on
1
1608
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. What i am trying to do now is have a previous/next button on the form i just opened and have it go through the list of items depending on whats previous or next to that item. I pretty much have the logic for that, the only issue i cant seem to...
3
4701
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 form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog under the LinkClicked method. ...
0
9802
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10559
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9343
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7765
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6961
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5632
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5802
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.