Resolved Waiting for any of a set of buttons to be clicked

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
188
Programming Experience
10+
I have a WinForms program that loops thru a list of images. For each image, a form pops up with buttons for several choices what to do with the image.
Originally that form was a separate dialog, which worked fine. But now I had the idea to move all that functionality (including said buttons) to the main form which runs the loop. Having done that, I find that I have no clue how to pause my loop until any of the buttons is clicked.... Not sure if this is even possible short of creating a busy loop ? I have a feeling I've painted myself in a corner here ...
 
You're probably just not thinking about it from the right perspective.

Have all of the buttons do the same last thing when you click it; load the next image

There is no loop, just a list of images and a counter of which one you're up to (or remove the first image from the list).

C#:
//images
List<string> _images = ...

void LoadButton_Click(...){
  _images.AddRange(Directory.GetFiles("path", "*.jpg"));
  ShowImage(false);
}

void ShowImage(bool removeFirst){

  if(removeFirst) _images.RemoveAt(0);

  _picturbox.Image = Image.FromFile(_images.First());
}

void KeepButton_Click(...){
  ShowImage(true);
}
void DeleteButton_Click(..){
  File.Delete(_images.First());
  ShowImage(true);
}

There is no loop here, advancement is made by repeatedly clicking a button, some code happens, then stops waiting for the next user input


Note this code is absolutely just for illustration of a concept and not recommending e.g. how to load a file etc. the important bit is how the list is treated, how advancement occurs etc. there is no error handling, it's purely about ditching a loop and generating looping behavior based on repeptitive user actions with time between where the Ui thread may go and draw the UI
 
Last edited:
Ha ! Brilliant ! Ditching the sacred loop is something that had never occurred to me 💡 That looks absolutely the way to go here. Many thanks for the idea !!!
 

Latest posts

Back
Top Bottom