在winformc#中自动点击按钮

本文关键字:按钮 winformc# | 更新日期: 2023-09-27 17:50:22

我有一个按钮代码,每当用户单击它时,它就能连接到网络。

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();
    }  

然而,现在我想这段代码自动运行第一没有任何人点击它。我想这样做是因为我想让任务调度器每天运行这段代码来进行更新。这个程序将在后面运行,并且没有与用户交互。因此,我希望这个程序自动连接自己。这可能吗?请建议。

在winformc#中自动点击按钮

你可以把这段代码放在一个方法中,而不是button click,并从button click事件调用该方法,也可以在InitializeComponent()之后从Form的构造函数调用该方法。

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
}

在你的构造函数中调用cmdConnect_Click(null, null),或者在你想要它首先运行的地方。

你可以通过调用PerformClick()的FormLoad()事件引发按钮控件的点击事件。所以它会自动运行,没有任何人点击它或当Task Scheduler运行你的应用程序来做更新。

cmdConnect.PerformClick();

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

如果您只想从表单的另一个地方运行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();
}

在Button Click事件中,只需调用NetworkConnection()方法:

private void cmdConnect_Click(object sender, System.EventArgs e)
{
    NetworkConnection();
}