Click to See Complete Forum and Search --> : Simulated button click


Glaing
November 12th, 2008, 05:41 PM
Hi
I have a window that has a grid, initial requirements were for the grid to be empty till a calculate button was clicked. I now need to fill this grid when the window is instantiated. Instead of repeating the code in the button I need to either call that method or simulate a click on the button so it will fill the gridinitially. Can anyone help me with either a simulated click on the button or calling that method programatically. The name of the method is

public void executeButton_Click(object sender, EventArgs e)

Thanks

BigEd781
November 12th, 2008, 05:46 PM
If there is no button, there is no need to hide it and simulate a button click. Why not just subclass the grid and add the code in the constructor?

Arjay
November 12th, 2008, 08:25 PM
Pull the code out of the button and put it into another method:

public void executeButton_Click(object sender, EventArgs e)
{
FillGrid( );
}

private void FillGrid( )
{
// grid filln' code
}

MMH
November 13th, 2008, 12:00 AM
One should never code directly under the control event handler. Program logic should be kept separate in methods for the reuse.

As Arjay stated, that is the correct way.

But since you would be interested in knowing how to simulate a click, below is the code for it.

executeButton_Click(null, null)


Beware, there are lots of potential problems with this, so try to avoid it where ever possible.

At the same time, i could think of some scenarios where you can do this.
Suppose, you have an edit form having cascaded dropdown lists. Here you would like to call the "SelectedIndexChanged" inorder to select the value in the lists.

I hope this helps.