A Quick Delegate Handler Question Win [C#]

M

MikeY

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
 
J

Jon Skeet [C# MVP]

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
 
M

MikeY

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
 
C

cjard

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?
 
J

Jon Skeet [C# MVP]

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
 
M

MikeY

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
 
M

MikeY

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
 
J

Jon Skeet [C# MVP]

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
 
M

MikeY

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top