473,413 Members | 1,829 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,413 software developers and data experts.

A Quick Delegate Handler Question Win [C#]

Hi Everyone,

I'm lookig for a way around retrieving my information, with out having to
take it to another method.

I have two buttons "YES" "NO" on a custum control and I want to retrieve the
returned info of yes or no in the same method I called up the control
instead of sending it to another method for extraction. My code is as
follows:

MSG_YesNo MSGYesNo = new MSG_YesNo();

MSGYesNo.Location = new System.Drawing.Point(280, 30);

MSGYesNo.Show();

this.Controls.Add(MSGYesNo);

MSGYesNo.BringToFront();

-----Origional way-----

MSGYesNo.Click += new System.EventHandler(MSGYesNo.On_Click);

-----Prefered way, but doesn't work-----

string Response = MSGYesNo.Click += new
System.EventHandler(MSGYesNo.On_Click);

MessageBox.Show(Response);

Thank you all in advance,

MikeY
Jun 7 '07 #1
8 1584
On Jun 7, 3:21 pm, "MikeY" <mikesi...@yaho.comwrote:
I'm lookig for a way around retrieving my information, with out having to
take it to another method.

I have two buttons "YES" "NO" on a custum control and I want to retrieve
the returned info of yes or no in the same method I called up the control
instead of sending it to another method for extraction. My code is as
follows:
<snip>

It's not at all clear to me how you want this to work. Adding an event
handler doesn't call the method - On_Click isn't actually *called*
when you add the handler.

If you actually want to get a yes/no response at a particular time,
you need to launch a dialog then and there - as your original code
does (although I'm not sure why you're adding it to the current form
after showing it separately).

Jon

Jun 7 '07 #2
Hi Jon,

What I've done is to create a control that gets called from this form so
that when a user does an action, a pop-up "YES"/"NO" box appears ontop of
the form. For them to proceed with their action or not.

The previous code calls up and places this contol on my form. And when the
user selects their option the control gets disposed of and the "YES" or "NO"
gets passed back to this form. And all is good up to this point. But I was
looking for a way of keeping the passed information within this method
instead of having to pass the infor to another method instead of ie:
displaying the info in the MessageBox.Show(Response);

Thanks
MikeY
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@m36g2000hse.googlegr oups.com...
On Jun 7, 3:21 pm, "MikeY" <mikesi...@yaho.comwrote:
>I'm lookig for a way around retrieving my information, with out having to
take it to another method.

I have two buttons "YES" "NO" on a custum control and I want to retrieve
the returned info of yes or no in the same method I called up the control
instead of sending it to another method for extraction. My code is as
follows:

<snip>

It's not at all clear to me how you want this to work. Adding an event
handler doesn't call the method - On_Click isn't actually *called*
when you add the handler.

If you actually want to get a yes/no response at a particular time,
you need to launch a dialog then and there - as your original code
does (although I'm not sure why you're adding it to the current form
after showing it separately).

Jon

Jun 7 '07 #3
On Jun 7, 3:46 pm, "MikeY" <mikesi...@yaho.comwrote:
Hi Jon,

What I've done is to create a control that gets called from this form so
that when a user does an action, a pop-up "YES"/"NO" box appears ontop of
the form. For them to proceed with their action or not.
So, you reinvented:

DialogResult answer = MessageBox.Show("Is this a YES/NO question?",
"", MessageBoxButtons.YesNo)

For.. ummm.. what reason?
Jun 7 '07 #4
On Jun 7, 3:46 pm, "MikeY" <mikesi...@yaho.comwrote:
What I've done is to create a control that gets called from this form so
that when a user does an action, a pop-up "YES"/"NO" box appears
ontop of the form. For them to proceed with their action or not.

The previous code calls up and places this contol on my form.
And when the user selects their option the control gets disposed of
and the "YES" or "NO" gets passed back to this form. And all is good
up to this point. But I was looking for a way of keeping the passed
information within this method instead of having to pass the infor to
another method instead of ie: displaying the info in the
MessageBox.Show(Response);
Well, the previous code shows the control (is it a dialog by any
chance?) and *then* adds it to your form, which is somewhat
counterintuitive.

I strongly suspect it's a dialog, as otherwise you wouldn't have any
information when you ask for it - you've got to block for the user to
say yes/no.

As cjard said, it's better to reuse MessageBox in general. However, if
you really want to go your current route, could you provide a short
but complete program which demonstrates the problem?
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jun 7 '07 #5
Hi cjard,

The reason is that if the user proceeds "YES" then they will leave the form.
If they select "NO" then they will not leave the form. Pretty simple, or at
least I thought. The MessageBox was just my way of testing the response from
the user. I wanted to keep all the codes within the same method instead of
having to take the response to another method to finish the procedure.

MSG_YesNo MSGYesNo = new MSG_YesNo();

MSGYesNo.Location = new System.Drawing.Point(280, 30);
MSGYesNo.Show();
this.Controls.Add(MSGYesNo);
MSGYesNo.BringToFront();

-----Origional way-----
MSGYesNo.Click += new System.EventHandler(MSGYesNo_Return);
-----Prefered way, but doesn't work-----
string Response = MSGYesNo.Click += new
System.EventHandler(MSGYesNo.On_Click);

MessageBox.Show(Response);

//I did not want to go here

private void MSGYesNo_Return(string Item_Info)

{

MessageBox.Show(Response);

}

Thanks

MikeY
Jun 7 '07 #6
Thanks Jon for helping out. Yes it is a dialog box. But there are things
going on in the background and the size of the control that I need instead
of the regular message box. Here is the cut down version. From the sounds of
it. I will just have to stick to the regular way.

namespace HOST

{

public class Order_Zone : System.Windows.Forms.Form

{

//Returns User Response From The Pad_Password

private void PadPassword_Return(string Item_Info)

{

//If User Is Changing Table Ownership

if(Temp_Item == "EMPLOYEE TRANSFER")

{

MSG_YesNo MSGYesNo = new MSG_YesNo();

MSGYesNo.Location = new System.Drawing.Point(280, 30);

MSGYesNo.Show();

this.Controls.Add(MSGYesNo);

MSGYesNo.BringToFront();

**//I prefer not to take this route. But it does work prefectly

MSGYesNo.Item_Pass += new MSG_YesNo_Item_Pass(MSGYesNo_Return);

**//I would prefer to do something like this

string response = new MSG_YesNo_Item_Pass(MSGYesNo_Return);

if(response == "YES")

{

....Do Something

}

}

}

//Returns User Click From MSG_YesNo Control

private void MSGYesNo_Return(string Item_Info)

{

...Do Something

}

}

}

...Custom Control

namespace HOST

{

public delegate void MSG_YesNo_Item_Pass(string Item_Info);

public class MSG_YesNo : System.Windows.Forms.UserControl

{

public event MSG_YesNo_Item_Pass Item_Pass

***Both Yes No buttons events Handler

//Get User Response For Button Selection & PassBack info

private void On_Click(object sender, System.EventArgs e)

{

Button Button_Item = (Button)sender;

ItemPass(Button_Item.Text);

//Dispose of This Control

this.Dispose();

}

//Item Pass Back To Calling Function

protected virtual void ItemPass(string Item_Info)

{

Item_Pass(Item_Info);

}

}

}

Hopefully this is not too long coded and hopefully straight forward. Any and
all help is appreciated.

MikeY

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On Jun 7, 3:46 pm, "MikeY" <mikesi...@yaho.comwrote:
>What I've done is to create a control that gets called from this form so
that when a user does an action, a pop-up "YES"/"NO" box appears
ontop of the form. For them to proceed with their action or not.

The previous code calls up and places this contol on my form.
And when the user selects their option the control gets disposed of
and the "YES" or "NO" gets passed back to this form. And all is good
up to this point. But I was looking for a way of keeping the passed
information within this method instead of having to pass the infor to
another method instead of ie: displaying the info in the
MessageBox.Show(Response);

Well, the previous code shows the control (is it a dialog by any
chance?) and *then* adds it to your form, which is somewhat
counterintuitive.

I strongly suspect it's a dialog, as otherwise you wouldn't have any
information when you ask for it - you've got to block for the user to
say yes/no.

As cjard said, it's better to reuse MessageBox in general. However, if
you really want to go your current route, could you provide a short
but complete program which demonstrates the problem?
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Jun 7 '07 #7
On Jun 7, 4:49 pm, "MikeY" <mikesi...@yaho.comwrote:
Thanks Jon for helping out. Yes it is a dialog box. But there are things
going on in the background and the size of the control that I need instead
of the regular message box. Here is the cut down version. From the sounds
of it. I will just have to stick to the regular way.
Well, a few things:

1) I still don't thikn you need to add the control to your form. The
dialog is separate from the form, not part of it.
2) The easiest way to encapsulate it would be to have a method on your
dialog which shows it, captures the result, and returns it.
3) It's not clear what the Item_Pass part is for - why not just have a
property which returns the user's selection?

Jon

Jun 7 '07 #8
Thank you Jon, I will give 3) a go. As for Item_Pass, it is to pass back the
"YES" or "NO" Response from the user. Once again, than you for all your
help, much appreciated.

MikeY
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@m36g2000hse.googlegr oups.com...
On Jun 7, 4:49 pm, "MikeY" <mikesi...@yaho.comwrote:
>Thanks Jon for helping out. Yes it is a dialog box. But there are things
going on in the background and the size of the control that I need
instead
of the regular message box. Here is the cut down version. From the sounds
of it. I will just have to stick to the regular way.

Well, a few things:

1) I still don't thikn you need to add the control to your form. The
dialog is separate from the form, not part of it.
2) The easiest way to encapsulate it would be to have a method on your
dialog which shows it, captures the result, and returns it.
3) It's not clear what the Item_Pass part is for - why not just have a
property which returns the user's selection?

Jon

Jun 7 '07 #9

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

Similar topics

1
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); ...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
4
by: Jon Davis | last post by:
If two delegates are created that point to the exact same method, and an event is assigned both delegates, ... myObj.MyEvent += new EventHandler(MyHandler); myObj.MyEvent += new...
4
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
5
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
3
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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
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...

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.