简体   繁体   中英

c# - Invoke button_click event handler in a method

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.

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?

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...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM