events and delegates: EventArgs question

  • Thread starter Thread starter Abdessamad Belangour
  • Start date Start date
A

Abdessamad Belangour

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.
 
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?
 
Abdessamad Belangour said:
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.
 
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 said:
Abdessamad Belangour said:
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.
 
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 said:
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 said:
Abdessamad Belangour said:
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.
 
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.
 
Back
Top