473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

events and delegates: EventArgs question

Hi all,
I'm working on a Windows application composed of two forms : form1 and
form2.
form1 calls form2 which has two buttons : OK and Cancel.
The form1 needs to know which of form2 buttons were clicked and to get some
form2 processed values .

private void OK_Click(object sender, System.EventArgs e)
{
// do proccessing and return a value
}

Do i have to declare my own EventArgs parameter? If yes do i have also to
change the delegate(to declare my own delegate)? Please give me a small
example if possible.
Thanks in advance.
Abdessamad.

Nov 15 '05 #1
5 1644
Hello Abdessamad,

A button in .NET has a property called DialogResult. This will say what is
the result of the click on a certain button. So if you have an OK and Cancel
button you can use the dialog result to know if OK or Cancel were clicked
using the corresponding DialogResult values.

When you call Form2 use the ShowDialog method which will return the
DialogResult corresponding to the button clicked and when you will know what
button was clicked you can than access Form2 to process information from it.

Is this what you were looking for?
--
Eran Sandler esandler at netvision.net.il

"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message
news:eX*************@TK2MSFTNGP09.phx.gbl...
Hi all,
I'm working on a Windows application composed of two forms : form1 and
form2.
form1 calls form2 which has two buttons : OK and Cancel.
The form1 needs to know which of form2 buttons were clicked and to get some form2 processed values .

private void OK_Click(object sender, System.EventArgs e)
{
// do proccessing and return a value
}

Do i have to declare my own EventArgs parameter? If yes do i have also to
change the delegate(to declare my own delegate)? Please give me a small
example if possible.
Thanks in advance.
Abdessamad.

Nov 15 '05 #2
"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote:
form1 calls form2 [...]
The form1 needs to know which of form2
buttons were clicked and to get some form2
processed values . [...]


One way to do this is to add public properties to the second form and read
their values from the first form. They are readable even after the form has
been closed.

// code in MyForm1 (with a button MyButton)

private void MyButton_Clicked(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog();
string strValueFromForm2 = frm.TheValue;
frm.Dispose();
}

// code in MyForm2 (with a textbox MyTextBox)

public string TheValue
{
get { return m_strValue; }
}

private string m_strValue = "";

private void MyForm2_Closing(object sender, CancelEventArgs e)
{
// Remember what was in the textbox
m_strValue = MyTextBox.Text;
}

P.

--
www.CL4.org
Nov 15 '05 #3
Paul and Eran, thank you for answering me.
your answers helped me but i would like to explore also the possibility to
extends EventsArgs Class and to use events and delegates.
The important pieces of my small test program are :
//---------------------------------------------------------------
public class ExtendedEventArgs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventArgs()
{
OkIsClicked=false;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler(object sender,ExtendedEventArgs e);
//--------------------------------------------------------------------
this.button1.Click += new myEventHandler(this.button1_Click);

//----------------------------------------------
private void button1_Click(object sender, ExtendedEventArgs e)
{
MessageBox.Show("Hi everybody");
}
----------------------------------------------
When i try to compile this example the compiler gives me the following error
message.
<< Cannot implicitly convert type 'testEventargs.myEventHandler' to
'System.EventHandler'>>
What's wrong with that. Should i do any casting?
Thanks again.
Abdessamad.


"Paul E Collins" <fi******************@CL4.org> a écrit dans le message de
news:bt**********@titan.btinternet.com...
"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote:
form1 calls form2 [...]
The form1 needs to know which of form2
buttons were clicked and to get some form2
processed values . [...]
One way to do this is to add public properties to the second form and read
their values from the first form. They are readable even after the form

has been closed.

// code in MyForm1 (with a button MyButton)

private void MyButton_Clicked(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog();
string strValueFromForm2 = frm.TheValue;
frm.Dispose();
}

// code in MyForm2 (with a textbox MyTextBox)

public string TheValue
{
get { return m_strValue; }
}

private string m_strValue = "";

private void MyForm2_Closing(object sender, CancelEventArgs e)
{
// Remember what was in the textbox
m_strValue = MyTextBox.Text;
}

P.

--
www.CL4.org

Nov 15 '05 #4
Hi Abdessamad,

I didn't see your public event myEventHandler .. declaration after the
delegate declaration...

Additionally, I'm not sure if adding your custom event handler removes
the default Click event which is associated with the object. I think
it just adds to the existing built-in event.. Perhaps, a test of using
totally new names for the events or simply deriving and passing the
extended argument data and passing the new argument data to the
standard button1.Click (sender o, extendedeventargs e)function might
yield better results.

~~~~~~~~~~~~~
Tommie Carter
--
"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote in message news:<eI*************@TK2MSFTNGP12.phx.gbl>...
Paul and Eran, thank you for answering me.
your answers helped me but i would like to explore also the possibility to
extends EventsArgs Class and to use events and delegates.
The important pieces of my small test program are :
//---------------------------------------------------------------
public class ExtendedEventArgs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventArgs()
{
OkIsClicked=false;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler(object sender,ExtendedEventArgs e);
//--------------------------------------------------------------------
this.button1.Click += new myEventHandler(this.button1_Click);

//----------------------------------------------
private void button1_Click(object sender, ExtendedEventArgs e)
{
MessageBox.Show("Hi everybody");
}
----------------------------------------------
When i try to compile this example the compiler gives me the following error
message.
<< Cannot implicitly convert type 'testEventargs.myEventHandler' to
'System.EventHandler'>>
What's wrong with that. Should i do any casting?
Thanks again.
Abdessamad.


"Paul E Collins" <fi******************@CL4.org> a écrit dans le message de
news:bt**********@titan.btinternet.com...
"Abdessamad Belangour" <be*******@irin.univ-nantes.fr> wrote:
form1 calls form2 [...]
The form1 needs to know which of form2
buttons were clicked and to get some form2
processed values . [...]


One way to do this is to add public properties to the second form and read
their values from the first form. They are readable even after the form

has
been closed.

// code in MyForm1 (with a button MyButton)

private void MyButton_Clicked(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog();
string strValueFromForm2 = frm.TheValue;
frm.Dispose();
}

// code in MyForm2 (with a textbox MyTextBox)

public string TheValue
{
get { return m_strValue; }
}

private string m_strValue = "";

private void MyForm2_Closing(object sender, CancelEventArgs e)
{
// Remember what was in the textbox
m_strValue = MyTextBox.Text;
}

P.

--
www.CL4.org

Nov 15 '05 #5
On 2004-01-02, Abdessamad Belangour <be*******@irin.univ-nantes.fr> wrote:
Paul and Eran, thank you for answering me.
your answers helped me but i would like to explore also the possibility to
extends EventsArgs Class and to use events and delegates.
The important pieces of my small test program are :
//---------------------------------------------------------------
public class ExtendedEventArgs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventArgs()
{
OkIsClicked=false;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler(object sender,ExtendedEventArgs e);
//--------------------------------------------------------------------
this.button1.Click += new myEventHandler(this.button1_Click);

//----------------------------------------------
private void button1_Click(object sender, ExtendedEventArgs e)
{
MessageBox.Show("Hi everybody");
}

The problem is that the button1.Click event is already defined to have a
parameter EventArgs, there's no way you can make the button create an
ExtendedEventArgs structure for the event because you don't control the
code in the button.

Since this is for learning purposes, the way to do this is to catch the
button event and resend a MyEventHandler event yourself, such as...

public delegate void myEventHandler(object sender,ExtendedEventArgs e);
public event myEventHandler ExtendedEvent;

this.button1.Click += new myEventHandler(this.button1_Click); //----------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
if(ExtendedEvent != null)
{
ExtendedEvent(this, new ExtendedEventArgs);
}
}
----------------------------------------------
When i try to compile this example the compiler gives me the following error
message.
<< Cannot implicitly convert type 'testEventargs.myEventHandler' to
'System.EventHandler'>>
What's wrong with that. Should i do any casting?


Casting won't help, because the EventArgs structure the button
sends you is not an ExtendedEventArgs structure.

--
David
dfoster at
hotpop dot com
Nov 15 '05 #6

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

Similar topics

6
1337
by: Flare | last post by:
Hi i have a qusstion about events and delegates. Especially the precis role of the Event. Eg. We have a class wich want to fire events so we declare: public delegate void...
15
2071
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
1
2829
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
5
404
by: aaj | last post by:
Hi I'm an OOP newbie and working my way through some of the concepts of .NET and c#. I'm looking at messaging between objects using events, but having a few problems generating them. This is...
3
2279
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
9
1818
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
2
2331
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
7
3406
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
3
9451
Frinavale
by: Frinavale | last post by:
Background An Event is a message sent out by an object to notify other objects that an action/trigger/state-change (ie. an event) has taken place. Therefore, you should use an event when an object's...
0
7226
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
7125
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...
1
7049
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
5631
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
4709
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...
0
3199
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...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
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...

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.