0

I have a Start and a Done button on a windows form, and I have vehicles stored in a list.

 private void btnStart_Click(object sender, EventArgs e) { Start(); } private void Start() { for (int i = 0; i < vehicles.Count; i++) { txtLicensePlate.Text = vehicles[i].LicensePlate; } 

So by clicking on the start button makes the txtLicensePlate textbox show the car's license plate number one by one. But before moving on from a license plate number, I would like the for loop to wait for a button click.

 private void btnDone_Click(object sender, EventArgs e) { Add(); } 

I tried to invoke the event handler like this:

 private void Start() { for (int i = 0; i < vehicles.Count; i++) { txtLicensePlate.Text = vehicles[i].LicensePlate; btnDone_Click(sender, e); } 

But I got an error message saying, that "sender and e does not exist in the current context". And on the other hand it seems I want the loop to trigger the button click on it's own (which is not my intention), and this leads to my question:

How can make the for loop to wait for a button click by the user?

Edit: Seems like I caused a little confusion, so I'd like to clarify: The Start button would start the loop showing the first license plate number. Then the user can "tell" if the current vehicle will go for a run, and if it will how many kilometres it will run. When the user clicks the Done button, the amount of kilometres will added to the vehicle, and the next license plate number should be shown.

4
  • I am not sure I understand your goal - you want the start button to begin showing the license plate numbers from your list. It will show the first one, and wait until the user clicks done, and then when they click done it will show the next one. Is that correct? Commented Apr 22, 2017 at 15:56
  • Okay - reading it further, is it that you will show the first plate, the user clicks done, this calls the Add method and then you display the next plate number? Does the Add method involve anything in the UI (I am making the assumption that it is going to read from the text box)? Commented Apr 22, 2017 at 16:15
  • Yes, you guessed it right. The Start button would start the loop showing the first license plate number. Then the user can "tell" if the current vehicle will go for a run, and if it will how many kilometres it will run (via textbox). When the user clicks the Done button, the amount of kilometres will added to the vehicle, and the next license plate number should be shown. Commented Apr 24, 2017 at 7:33
  • I have updated my answer based on your clarification. I also added in code that I had left out which validated that there was a next vehicle. Commented Apr 24, 2017 at 9:09

2 Answers 2

2

You should call the PerformClick method of the Button.

That said, why not just call the Add method at the end of the Start method? That's what you really want to achieve and clicking the btnDone is simply a means to that end so why not just go straight to the end instead of trying to artificially invokes the means?

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I forgot to mention something. In short, the application asks if the selected vehicle will go for a run and if it will, how many km-s it will be. So after the user sees the license plate number, he/she can check in a checkbox to tell if the car will be used, then specify he amount of kilometres. That's why I need the loop to wait, because the values in Add() are given by the user for each vehicle.
1

I am going to take a guess at answering what I think you are trying to do.

Edit Based on your update to the question, this is probably how I would solve the issue. One thing to note, you probably want to disable the start button once you begin looping through the vehicles. You could do that within the btnStart_Click event handler. Depending on your goals, you may want to have a way to re-enable it with either a reset button or when you have reached the end of your list. I don't know what you want to happen when you reach that point, so I updated the done handler code to check for the end of the list, but you would need to determine what to do when the end of the list is reached.

Instead of having a loop in your Start button code, have the Start method just place the initial string into the TextBox. You will then just need to store the current position in the list and you can update the next license plate from your Done button.

private int _currentVehicleIndex; private void btnStart_Click(object sender, EventArgs e) { txtLicensePlate.Text = vehicles[0].LicensePlate; _currentVehicleIndex = 1; } private void btnDone_Click(object sender, EventArgs e) { Add(); if (_currentVehicleIndex < vehicles.Count) { txtLicensePlate.Text = vehicles[_currentVehicleIndex].LicensePlate; _currentVehicleIndex++; } else { // Handle the case where we have reached the end of the list // clear the license plate text, reset form, etc... } } 

1 Comment

Based on your pre-edited code I was always thinking about something like this. After the list ends the user will have the chance to start over (send the cars on "new routes"), or just simply finish the work. In the latter case the UI will display some basic information about the vehicles and their routes. Your code turned out to be a different workaround, but exactly what I needed, thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.