【问题标题】:Auto click button in winform c#winform c#中的自动单击按钮
【发布时间】:2014-03-14 03:41:20
【问题描述】:

我有一个按钮代码,只要用户点击它就可以连接到网络。

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

但是现在我希望此代码首先自动运行,而无需任何人点击它。我想这样做是因为我希望任务调度程序每天运行此代码以进行更新。该程序将在后面运行,并且与用户没有交互。因此我希望这个程序自行自动连接。这可能吗?请指教。

【问题讨论】:

标签: c# winforms


【解决方案1】:

您可以将此代码放在一个方法中,而不是按钮单击中,并从按钮单击事件以及在 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
}

【讨论】:

  • 但是按钮还需要点击仪式吗?我不希望任何按钮被点击。因为这个程序在后面运行,没有与用户交互。
  • 如果您的表单正在加载,则无需单击按钮。每当创建表单对象时,您的上述代码就会运行。试着测试一下。
  • 他说创建一个包含网络代码代码的方法,并从构造函数和按钮处理程序中调用该方法。虽然我认为问题是关于如何执行/自动单击按钮自动调用的方法。
  • 好的。但对不起,我是这个 c# winform 的新手。你能指导我多一点吗?真的很感激。
  • 看,每当窗口窗体显示在屏幕上时,它的构造函数就会被调用,并且里面有一个称为 InitializeComponent() 的方法。此方法初始化表单中的所有文本框和其他内容。在此方法调用之后,您可以按照 Alex 在他的回答中的建议添加方法。
【解决方案2】:

在您的构造函数中调用cmdConnect_Click(null, null),或者在您希望它首先运行的任何位置。

【讨论】:

    【解决方案3】:

    您可以通过在 FormLoad() 事件上调用 PerformClick() 来引发按钮控件的单击事件。所以它会首先自动运行,而无需任何人点击它,或者当Task Scheduler 运行您的应用程序进行更新时。

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

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      相关资源
      最近更新 更多