473,831 Members | 2,320 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.EventArg s 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 1656
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.i l

"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message
news:eX******** *****@TK2MSFTNG P09.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.EventArg s 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_Clicke d(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog( );
string strValueFromFor m2 = 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 ExtendedEventAr gs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventAr gs()
{
OkIsClicked=fal se;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler( object sender,Extended EventArgs e);
//--------------------------------------------------------------------
this.button1.Cl ick += new myEventHandler( this.button1_Cl ick);

//----------------------------------------------
private void button1_Click(o bject sender, ExtendedEventAr gs 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.EventHa ndler'>>
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.btinte rnet.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_Clicke d(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog( );
string strValueFromFor m2 = 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, extendedeventar gs e)function might
yield better results.

~~~~~~~~~~~~~
Tommie Carter
--
"Abdessamad Belangour" <be*******@irin .univ-nantes.fr> wrote in message news:<eI******* ******@TK2MSFTN GP12.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 ExtendedEventAr gs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventAr gs()
{
OkIsClicked=fal se;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler( object sender,Extended EventArgs e);
//--------------------------------------------------------------------
this.button1.Cl ick += new myEventHandler( this.button1_Cl ick);

//----------------------------------------------
private void button1_Click(o bject sender, ExtendedEventAr gs 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.EventHa ndler'>>
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.btinte rnet.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_Clicke d(object sender, EventArgs e)
{
MyForm1 frm = new MyForm1();
frm.ShowDialog( );
string strValueFromFor m2 = 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 ExtendedEventAr gs:EventArgs
{
bool OkIsClicked;
string data;
public ExtendedEventAr gs()
{
OkIsClicked=fal se;
data="";
}
}
//-------------------------------------------------------------------
public delegate void myEventHandler( object sender,Extended EventArgs e);
//--------------------------------------------------------------------
this.button1.Cl ick += new myEventHandler( this.button1_Cl ick);

//----------------------------------------------
private void button1_Click(o bject sender, ExtendedEventAr gs 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
ExtendedEventAr gs 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,Extended EventArgs e);
public event myEventHandler ExtendedEvent;

this.button1.Cl ick += new myEventHandler( this.button1_Cl ick); //----------------------------------------------
private void button1_Click(o bject sender, EventArgs e)
{
if(ExtendedEven t != null)
{
ExtendedEvent(t his, new ExtendedEventAr gs);
}
}
----------------------------------------------
When i try to compile this example the compiler gives me the following error
message.
<< Cannot implicitly convert type 'testEventargs. myEventHandler' to
'System.EventHa ndler'>>
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 ExtendedEventAr gs 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
1351
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 TestEventHandler(object sender, EventArgs arg); public event TestEventHandler Test; And fire the event with
15
2102
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 reference to some of the ClassA objects created in ClassB. In ClassC I hook into the ClassA events with a foreach loop so that I hook each object. The code is something like this: class ClassC { void SomeMethod() { foreach (ClassA item in...
1
2843
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 remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
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 the code I'm trying to get working but with no luck. To prove it works, I'm trying to force an event from elsewhere by calling
3
2308
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 WebApplication1 { using System;
9
1839
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using CSharpLib;
2
2350
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 fires events when the data changes, is often the central part. Connected to these states are various observers that act on changes in data, by altering the information presented to the user, executing code and so on, each observer with its own...
7
3425
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 uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
3
9503
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 state change requires other objects to change as well. The object that sends an event is known as the event sender. The object that receives (or listens for) the event is known as the event receiver. Events implement the "Observer Design...
0
9793
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
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
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...
0
10494
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10534
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
10207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9317
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
7748
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...
2
3963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.