• 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

    • So they admit that Windows 11 is slow and bloated. Good to know!
    • Real handheld Xbox hardware was canceled, this is just a PC with a screen and controller built-in.
    • Amazon Deal: JBL BAR 1000 7.1.4, BAR 700 5.1 Dolby Atmos wireless subwoofer soundbars by Sayan Sen If you are in the market for an audio system and are after smaller bookshelf speakers delivering highly accurate sound, then take a look at KEF and Polk Audio's Q Concerto Meta and Reserve R200 speakers, respectively, as both of them are up for sale at their lowest ever prices. However, if you are more into shaking your house, which is not possible without a subwoofer, then Samsung has its Q900F, Q800F, and Q600F soundbar systems with wireless subwoofers at the lowest prices. These are the latest 2025 models, and you can take a look at them in this article here. JBL BAR 1000 For those looking for additional options, JBL's BAR 1000 and Bar 700 are also available. The former has hit its lowest ever price too, while the latter is back to its cheapest (purchase links down below). JBL claims that its BAR 1000 model goes as low as 33Hz which is crucial for movie-watching or even some genres of music. The 10-inch subwoofer is rated at 300 watts of RMS power. The total power output of the system is 880 watts at THD (total harmonic distortion) of 1%. JBL BAR 1000 rear view Unlike the 7.1.4 JBL BAR 1000, the BAR 700 is a 5.1 system which means it lacks true Dolby Atmos, but it should still provide an Atmos-like experience. DTS:X is also not supported. The BAR 700 is rated at 620 watts. It is good to see some power ratings, as companies like Samsung, Sonos, Bose, and more tend not to mention them all too often nowadays. Interestingly, both the BAR systems have similarly-specced subwoofers so if bass is what you are looking for and do not care about the Atmos experience so much, you can opt for the BAR 700 too. Get them at the links below: JBL Bar 1000: 7.1.4-Channel soundbar with Detachable Surround Speakers, MultiBeam™, True Dolby Atmos®, and DTS:X®, Black: $799.95 (Shipped and Sold by Amazon US) JBL Bar 700: 5.1-Channel soundbar with Detachable Surround Speakers and Dolby Atmos®, Black: $549.95 (Amazon US) + you also get free 90-day Amazon Music This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Funny how just a few days ago we hear a lot of rumors that this device was cancelled.
    • Many order mixed mango yogurt.  
  • Recent Achievements

    • Dedicated
      Epaminombas earned a badge
      Dedicated
    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      ATLien_0
      235
    4. 4
      snowy owl
      233
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!