• 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

    • Nice! My $100 refurbished iPad 8 that I bought last year for a small project (and still runs like new cuz it's been mostly in storage the past several months) will get this v26 upgrade. I'm definitely gonna scope out OS26 on this device before upgrading my also-supported refurbished iPhone SE 3rd Gen (only got it for the 5G UW support).
    • Hello, The separate discussion about VLC has been moved to its own thread at  Regards, Aryeh Goretsky  
    • Hello, I am guessing this is the result of using a third-party tweaking tool instead of changing things within Windows settings. Windows 11 has a decent set of configuration options for the display, but they tend to be scattered throughout the operating system due to fragmentation of product ownership/inconsistent standards being applied by Microsoft. There are a few places in Windows 11 where you can modify the various display settings in order to get them to your liking: Under Settings → System → Display you can stroll down to the Scale & layout section and change the Scale and Display resolution settings to whatever you prefer.  For the Scale option, you can click on the text box to choose between the various pre-populated settings.  If none of those are exactly what you are looking for, click on the caret ("") caret to open the Custom scaling setting (also available under Settings → Display → Custom scaling).  I would recommend this as an option of last resort as this can cause display issues in various programs. I strongly recommend using the screen's native Display resolution here; it should show up as the "(Recommended)" resolution.  If it does not, you may need to install your monitor's profile information file (basically, a device driver for your monitor) to get the correct setting. Another place to look at making changes to fonts is under Accessiblity → Text size.  There is a slider bar for changing the text size from 100% to 225% there. You can try making changes to the ClearType settings.  It's a little bit finer-grained than the others, but it may make the text easier to view on the display.  Run the ClearType Text Tuner (filename: CTTUNE.EXE) and go through the wizard. You may want to do this several times under different lighting conditions or times of the day to figure out what works best for you overall. Lastly, you may want to look into using a dedicated assistive screen technology program from a third-party.  Microsoft provides a Screen Magnifier and a Narrator for text-to-speech, but they provide only basic functionality, and you may find that a third-party program works a lot better.  The pros of this is that third-party assistive technology programs tend to work quite well under Windows; Microsoft has historically worked closely with third-party developers of assistive technologies.  The cons of this are that such tools tend to be on the expensive side, since they are often sold to schools, businesses, or paid for by insurance companies, not individuals. Regards, Aryeh Goretsky  
    • Totally agree with you. I think people, especially on tech sites, need to realize that they’re the minority. Microsoft isn’t going to waste time on things that, in the big picture, don’t matter to the majority.
    • I think a lot of people here are too young to even remember Aqua.
  • Recent Achievements

    • Dedicated
      MadMung0 earned a badge
      Dedicated
    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      267
    3. 3
      +FloatingFatMan
      257
    4. 4
      Edouard
      203
    5. 5
      snowy owl
      178
  • Tell a friend

    Love Neowin? Tell a friend!