How to perform automatic button click on Windows Form based on a Parameter? C#
<p>I created a Windows Application with a form and few buttons on it. I need to fire the button click event automatically some times based on the parameter value passed to the application.</p>

<pre><code>static class SensexPrediction
{
    static void Main(string[] Args)  &lt;---- Modified this so accepting arguments.
    {
       Application.Run(new Sensex_Prediction_Form(Args)); &lt;--- Passing Args to Form.
    }
}
</code></pre>

<p>Below is the Code for Sensex_Prediction_Form method.</p>

<pre><code>public Sensex_Prediction_Form(String[] Args)
    {
        InitializeComponent();
        if (Args.Length != 0) //There is atleast one argument.
        {
            this.Invoker = Args[0];  &lt;-----Invoker is the name of the data member of the class.
        }


    }
</code></pre>

<p>Now on form load if Invoker == "X" i need to perform button1_Click event code. For that i wrote the following...</p>

<pre><code>private void Sensex_Prediction_Form_Load(object sender, EventArgs e)
    {
      if(Inovker == "x")
        {
          predict_butn.performclick();
        }
    }
</code></pre>

<p>But click is not happening automatically even though argument passed is X. </p>

<p>What i couldn't understand is the button name in the solution explorer is predict_butn but when i click on the button the event code is in a function named button1_click. Is this the reason?
Please help. Thanks.</p>

<p>After the suggestions i separated the event code and actual logic in a separate method named prediction.</p>

<pre><code>private void Sensex_Prediction_Form_Load(object sender, EventArgs e)
    {
        if (Invoker == "Scheduler")
        {
            prediction();
        }

    }
</code></pre>

<p>And i initialized the data variable of the class with Scheduler as below..</p>

<pre><code>public string Invoker = "Scheduler";
</code></pre>

<p>Even then when i load the form the method is not being invoked.
As suggested i corrected the connection between button name and even method name etc. 
thank q</p>

<p>Suprisingly..(for me :-))</p>

<pre><code>if (Invoker == "Scheduler")
        {
            MessageBox.Show("Testing");
            prediction();
            MessageBox.Show("OK");
        }
</code></pre>

<p>Testing message is getting executed but after that it just displays the form. so what could be the reason?</p>

<p>Understood the issue..I am getting an exception object reference not set..because i have a line that says </p>

<pre><code>ActiveForm.Text = "Sensex Prediction System ";
</code></pre>

<p>At this point form has not been loaded so it can't set the text.</p>

<p>------> Now the issue is how to call a method automatically after loading the form? because the method will have code that will modify the form while executing.</p>

<p>Got it...using the "shown" event for the form able to do what i wanted. Thanks.</p>
