Your code doesn't "click" buttons - it reacts to what the user does, and the user can't click a button while a
for
or
foreach
loop is running - or at least not with any precision.
If you mean that you want to execute the code that would be executed when the user clicks a button, then that's simple: move the code into a separate method, and call that from both places:
private void MyButton_Click(object sender, EventArgs e)
{
DoMyFunction();
}
private void DoMyFunction()
{
}
...
foreach (FileInfo info in dir.GetFiles("*.jpg"))
{
...
DoMyFunction();
...
}
If you mean that you want the user to confirm each action by clicking a button, then that's simple as well: just use MessageBox:
foreach (FileInfo info in dir.GetFiles("*.jpg"))
{
...
if (MessageBox.Show("Are you sure?", "Please confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
...
}
}
If you are trying to do something else entirely, then you need to explain in a lot more detail!