blah blah blah is here! blah blah » Close

up1down
link

Dear all,

I am new in CS, and what I know is basically an event is triggered when we click a button or do something in the form.

I have a button, called number that will generate random number. Lets call it number_btn. I want to add one textbox with one button. This button, call it several_btn will run the event inside number_btn until the value inside the textbox. For example if it is 9 inside the textbox, it will run it 9 times.

My question is on how to run an event inside a button several times. What come in my mind is by putting it inside a function and run a loop. But that way, my variable must be made global then. My variable is local. Is any other way available?

Kindest Regards,




Eryatronika

last answered one year ago

1 answers

link

If I've understood it correctly, you can call the PerformClick() method in a loop:

private void several_btn_Click(object sender, EventArgs e)
{
int number;
int.TryParse(textBox1.Text, out number);
if (number <= 0) return;
for (int i = 1; i <= number; i++)
{
number_btn.PerformClick();
}
}

As you'll see, nothing happens if the number in the textbox is less than or equal to zero or can't be parsed to an integer ('number' is then zero).

yes vulpes, thank you the PerformClick() works =)

Feedback