• 0

[C#] Waiting for a Click event


Question

Hi folks, any help is appreciated

I'm in C#/.NET here for the first time and I'm trying to force my program to wait for a button's click (okbutton event Click). I don't want the program going anywhere until the user clicks that OK button.

I can't find any method, and since the event returns a void, I have no way of tracking a bool value...aiee!

Oh, and I don't want to use a MessageBox, please :)

Thanks in advance!

Link to comment
https://www.neowin.net/forum/topic/444311-c-waiting-for-a-click-event/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I have no idea what you're doing...(This is based on the fact that you mentioned a MessageBox...)

But....

If this form is being spawned by something else, and you want it to appear, and then wait to close, you can call it via ShowDialog(), which will show it, and then wait until the form closes.

  • 0

Thanks for the help folks.

This is actually my main form (just to clarify). I've went ahead and set the class level bool to true on my event, one problem I'm still having is that I cannot truly wait for the event to happen (I'm getting into threads here). My CPU clock cycles are going through the roof because I'm just using a while(bool == false) loop to wait.

I know why it's not working now, but I have no idea how to correct this and make the program truly wait on an event...

Anymore suggestions for me? Thanks again!

  • 0

you can use delegation to fire simultanesou events, maybe put the thread to sleep and have the delegate fire it again, here is a quick example of someones implementation of handling events with multiple threads, http://www.codeproject.com/csharp/h5csthreading.asp this may send you in the right direction. if you dont want to post your real code can u post something similar (a test app) the mimicks what you are trying to do so we can analyze it? hope this helps!!

  • 0

This is my code. I'm having a look through that page. It does look like I need to do multi threads here (ack).

Right here, where my MessageBox.Show call is, I'd like to replace it by waiting on the okbutton_Click event (or something of that could replace the MessageBox.Show command). I have a class level boolean called okclicker. It would be set to false until we get to this point in the code. I would then set it to true and allow the user to continue to find the next string.

///////code

while(firststrindex != -1)

{

havefound = false;

firststrindex = finallist.IndexOf(",");

if(firststrindex != -1)

{

firststr = finallist.Substring(0,firststrindex);

MessageBox.Show (firststr, "Finding String", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

allstr += firststr;

allstr += ", ";

//here I have a great deal of function calls and if statements to handle finding my string firststr in a char array

firststr = "";

finallist = finallist.Substring(firststrindex+2);

}

and just for reference, I had this code when my clock cycle's pinned...

///////code

while(firststrindex != -1)

{

okclicker = false;

havefound = false;

firststrindex = finallist.IndexOf(",");

if(firststrindex != -1)

{

firststr = finallist.Substring(0,firststrindex);

//here is my wait

while(!okclicker){}

allstr += firststr;

allstr += ", ";

//here I have a great deal of function calls and if statements to handle finding my string firststr in a char array

firststr = "";

finallist = finallist.Substring(firststrindex+2);

}

I had an okbutton_Click event that set okclicker to true;

Thanks again! I'm having a read through this link you gave me. All the help is greatly appreciated.

  • 0

From a simple test on my end, the MessageBox.Show() call will pause program execution.

Probably what you want is to check the return value of your MessageBox.Show() call (it returns a DialogResult enum -- in your case DialogResult.OK or DialogResult.Cancel). If the user clicks cancel, bust out of your loop (either set a "processing" bool before you enter the loop, and set it == false when the user clicks cancel (and check it in your while condition), or use "break" to break out of the while loop).

If they click ok, then ignore the result (since you're going to fall through to your processing code). This is assuming you want this message box as a check that you want to keep on processing.

Kormac

  • 0

It's the MessageBox that I want to remove, I'd like to integrate it into my main form.

MessageBox does stop the program. I'm aware of that, but I want a button on my main form that acts as this MessageBox does (without the extra popup window, as I'm finding that very annoying to my user).

IE. Button4.Click allows the user to continue. Button4 is found on the main form, not as part of the MessageBox. I will be removing the MessageBox when I can.

Again, thanks...boy, lots of help from lots of smart folks here.

Oh, and Kormac, I am assuming the user will be hitting ok, the cancel button actually will not break out of my loop anyways. I don't want it to.

This is actually a word search puzzle. They have to continue until all of the words are found (these are actually being pulled from the web at www.wonderword.com/puzzle.html, along with the puzzle).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.