• 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.
  • Posts

    • Jony Ive is reportedly working on multiple AI devices for OpenAI, including a mobile gadget by Hamid Ganji Last month, OpenAI announced acquiring Jony Ive's startup, io, in a $6.5 billion deal. Ive, who served as Apple's design chief for over two decades, moved to OpenAI to work on the company's AI hardware projects. While the details of these projects have remained relatively untold, Mark Gurman's Power On newsletter revealed some exciting insights about Jony Ive's work at OpenAI. According to Gurman, Apple's former design chief is now spearheading a series of groundbreaking AI hardware products for OpenAI. One of these under-development devices is described as a "mobile gadget" that takes the form of a "pendant" and can be worn around the neck. This device could allegedly allow users to access ChatGPT with voice commands. Another of Ive's works at OpenAI is a home device that functions similarly to a smart speaker. Users can put the device on a table and perform various tasks using OpenAI's ChatGPT. Interestingly, OpenAI also has a robot device under development. As Gurman says, this robot "will likely be a machine that develops a relationship with a human using AI." However, OpenAI's robot is still far from being made available on the market. After acquiring Jony Ive's startup, OpenAI CEO Sam Altman described Ive as the best designer in the world, adding that they can now work on the next generation of AI-powered computers. More details about OpenAI-Ive hardware products could emerge over the next few months. OpenAI was already rumored to be working on a mysterious AI gadget, presumably a screenless AI phone, which might be the same mobile gadget that Gurman revealed. While details of this alleged AI mobile are yet unknown, such a device could pose an alternative to conventional smartphones, raising the alarm for smartphone makers such as Apple, Samsung, and Google. Meanwhile, Apple might already be expecting such a device, as its services chief Eddy Cue allegedly said during his testimony at Google's antitrust case that iPhones might no longer exist 10 years from now.
    • Seems Nintendo have already thought of that after the Joy-Con drift in the Switch 2 EULA: Clause 16 of the EULA reads: "This arbitration provision precludes you and Nintendo from suing in court, having a trial by jury, or participating in a class action. You and Nintendo agree that arbitration will be solely on an individual basis and not as a class arbitration, class action, or any other kind of representative proceeding. You and Nintendo are each waiving the right to trial by a jury."
    • Cool glass phone you have in that silicon case bro
    • I can't say I ever noticed an OEM putting effort into user-friendly descriptions and icons who didn't also put effort into making good drivers. Usually, missing icons and descriptions was also a sign of bad drivers.
  • Recent Achievements

    • First Post
      brynmot earned a badge
      First Post
    • Reacting Well
      brynmot earned a badge
      Reacting Well
    • Week One Done
      Al_ earned a badge
      Week One Done
    • Week One Done
      MadMung0 earned a badge
      Week One Done
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      478
    2. 2
      +FloatingFatMan
      274
    3. 3
      ATLien_0
      243
    4. 4
      snowy owl
      209
    5. 5
      Edouard
      182
  • Tell a friend

    Love Neowin? Tell a friend!