Ren
Ren

Reputation: 775

Auto click button in winform c#

I have a button code that enable the connection to the network whenever the user click it.

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

However now I want this code to auto run first without anyone click on it. I want to do this because I want the task scheduler to run this code everyday to do update. This program will run behind, and theres no interaction with users. Therefore I want this program to auto connect by itself. Is this possible? Please advise.

Upvotes: 3

Views: 4630

Answers (3)

Mayur Agarwal
Mayur Agarwal

Reputation: 124

You can put this code in a method instead of button click and call that method from button click event and also from the Form's constructor after 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
}

Upvotes: 4

Aung Kaung Hein
Aung Kaung Hein

Reputation: 1576

You can raise click event of button control by calling PerformClick() on FormLoad() event. So it will auto run first without anyone clicking on it or when Task Scheduler runs your the application to do the update.

cmdConnect.PerformClick();

But I don't think sending click events is the best way.

If you just want to run the code which is in the try block from another place in the form, put the code into a separate method like DoWork() and call the method from anywhere you need to use it. That way you always have access to that function.

Example:

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

In Button Click event, just call NetworkConnection() method:

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

Upvotes: 2

Xela
Xela

Reputation: 2382

Call cmdConnect_Click(null, null) in your constructor or where ever you want it to run first.

Upvotes: 2

Related Questions