在Winform C#中自动点击按钮

3

我有一个按钮代码,当用户单击它时,它会启用与网络的连接。

private void cmdConnect_Click(object sender, System.EventArgs e)
    {
        try
        {
            EnableCommands(true);
            //Creating instance of Socket
            m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

            // retrieve the remote machines IP address
            IPAddress ip = IPAddress.Parse (txtIPAddr.Text);
            //A printer has open port 9100 which can be used to connect to printer
            int iPortNo = System.Convert.ToInt16 ( txtPort.Text);
            //create the end point 
            IPEndPoint ipEnd = new IPEndPoint (ip.Address,iPortNo);
            //connect to the remote host
            m_socClient.Connect ( ipEnd );
            EnableCommands(false);
            //wait for data to arrive 
            WaitForData();
        }
        catch(SocketException se)
        {
            MessageBox.Show (se.Message );
            EnableCommands(true);
        }

        page_counter();
    }  

然而现在我想让这段代码自动运行,而不需要任何人点击它。我想这样做是因为我希望任务计划程序每天运行此代码以进行更新。该程序将在后台运行,并且没有与用户的交互。因此,我希望该程序可以自动连接自己。这是否可行?请给予建议。


http://stackoverflow.com/questions/17878730/how-to-automatically-click-a-button-on-a-form-application - Nagaraj S
3个回答

4
您可以将此代码放在方法中,而不是按钮单击事件中,并从按钮单击事件和窗体的构造函数中的InitializeComponent()调用该方法。
public partial class Sample : Form
    {
        public Sample()
        {
            InitializeComponent();
            CmdConnect(txtIPAddr.Text, txtPort.Text);
        }

        private void cmdConnect_Click(object sender, System.EventArgs e)
        {
            CmdConnect(txtIPAddr.Text, txtPort.Text);
        }

        private void CmdConnect(string txtIPAddr, string txtPort)
        {
            try
            {
                EnableCommands(true);
                //Creating instance of Socket
                m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // retrieve the remote machines IP address
                IPAddress ip = IPAddress.Parse(txtIPAddr);
                //A printer has open port 9100 which can be used to connect to printer
                int iPortNo = System.Convert.ToInt16(txtPort);
                //create the end point 
                IPEndPoint ipEnd = new IPEndPoint(ip.Address, iPortNo);
                //connect to the remote host
                m_socClient.Connect(ipEnd);
                EnableCommands(false);
                //wait for data to arrive 
                WaitForData();
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
                EnableCommands(true);
            }
            page_counter();
        }

// Other Methods
}

但是仍然需要点击一个按钮,对吗?我不想要任何按钮被点击。因为这个程序在后台运行,不需要与用户交互。 - Ren
如果你的表单正在加载,那么无需点击按钮。每当表单对象被创建时,以上代码都将运行。试着测试一下吧。 - Mayur Agarwal
1
他的意思是创建一个包含网络代码的方法,并从构造函数和按钮处理程序中调用该方法。虽然我认为问题是关于如何执行/自动点击按钮调用的方法。 - Xela
好的,但是很抱歉我对这个C# Winform还不熟悉。您能不能再指导我一下?非常感激。 - Ren
每当窗体显示在屏幕上时,它的构造函数被调用,并且其中有一个名为InitializeComponent()的方法。此方法初始化表单中的所有文本框和其他内容。在此方法调用之后,您可以按照Alex在他的答案中建议的方式添加方法。 - Mayur Agarwal
显示剩余2条评论

2

在FormLoad()事件中调用PerformClick()方法,可以触发按钮控件的点击事件。这样,在没有任何人点击它或者当任务计划程序运行应用程序来进行更新时,它就会自动运行。

cmdConnect.PerformClick();

但我不认为发送点击事件是最好的方法。

如果您只想从表单中的另一个位置运行 try 块中的代码,请将代码放入一个类似于 DoWork() 的独立方法中,并从任何需要使用它的地方调用该方法。这样,您始终可以访问该函数。

示例:

private void NetworkConnection()
{
    try
    {
        EnableCommands(true);
        //Creating instance of Socket
        m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

        // retrieve the remote machines IP address
        IPAddress ip = IPAddress.Parse (txtIPAddr.Text);
        //A printer has open port 9100 which can be used to connect to printer
        int iPortNo = System.Convert.ToInt16 ( txtPort.Text);
        //create the end point 
        IPEndPoint ipEnd = new IPEndPoint (ip.Address,iPortNo);
        //connect to the remote host
        m_socClient.Connect ( ipEnd );
        EnableCommands(false);
        //wait for data to arrive 
        WaitForData();
    }
    catch(SocketException se)
    {
        MessageBox.Show (se.Message );
        EnableCommands(true);
    }

    page_counter();
}

在按钮单击事件中,只需调用NetworkConnection()方法:
private void cmdConnect_Click(object sender, System.EventArgs e)
{
    NetworkConnection();
}

2

在您的构造函数或任何您想要它首先运行的地方调用cmdConnect_Click(null, null)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接