开发者

How to perform automatic button click on Windows Form based on a Parameter? C#

开发者 https://www.devze.com 2023-03-23 03:23 出处:网络
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.

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.

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

Below is the Code for Sensex_Prediction_Form method.

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


    }

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

private void Sensex_Prediction_Form_Load(object sender, EventArgs e)
    {
      if(Inovker == "x")
        {
          predict_butn.performclick();
        }
开发者_JAVA技巧    }

But click is not happening automatically even though argument passed is X.

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.

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

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

    }

And i initialized the data variable of the class with Scheduler as below..

public string Invoker = "Scheduler";

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

Suprisingly..(for me :-))

if (Invoker == "Scheduler")
        {
            MessageBox.Show("Testing");
            prediction();
            MessageBox.Show("OK");
        }

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

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

ActiveForm.Text = "Sensex Prediction System ";

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

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

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


Promote the code within button1_click to a method. Then once the form is loaded and if the parameter match your filter, call the method.

private void Button1_Click(object sender, EventArgs e)
{
    DoSomething();
}

private void Sensex_Prediction_Form_Load(object sender, EventArgs e)
{
    if(Inovker == "X")
    {
        DoSomething();
    }
}

private void DoSomething()
{
    ...
}


Extract the code out of the button event into it's own method that is called in the button click. Now you just need to call that new extracted method instead of trying to invoke a ui button click.


First you can use Environment.GetCommandLineArgs() instead of passing the args to the constructor of the form.
Second you may assign the wrong event handler to the click. reassign the predict_butn.Click to its correct event handler instead of the current one which is button1_click


Double click button to generate event handler(for example button1_Click(object sender, System.EventArgs e)), and then you want to trigger that method just call button1_Click(null, null)

Of course better solution is to create one function with your logic and then call that function in button click event and when Invoker == "X".


First of all, you're not checking whether Invoker == 'X', but Invoker == 'x'. This may be part of the problem. Comparing strings with == is not good practice, however. You should use

if (Invoker.Equals('X', StringComparison.InvariantCulture))

Is the action performed if you click the button with your mouse? If so, the problem is not the event handler.

If it is not performed, the event handler and the button's event are not connected. You can check in the button's properties whether the event handler is attached to the button's click event.

What happens if you double-click the button in the designer? Is a new event handler created? If so, move the code from button1_click to the new event handler and delete the button1_click event handler.

Event handlers and control events are not automatically associated. You need to do this in the control's properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号