Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click a button through coding?

Tags:

c#

winforms

I have two buttons in my program and i want that when i press first button the second button is clicked automatically ( in the event handler of first button , i want to press the second button through coding).

private void button1_Click(object sender, EventArgs e)
    {

        passWord = pwd.Text;
        user = uName.Text;


        loginbackend obj = new loginbackend();
        bool isValid = obj.IsValidateCredentials(user, passWord, domain);
        if (isValid)
        {
            loginbackend login = new loginbackend();
            passWord = pwd.Text;

            login.SaveUserPass(passWord);
            HtmlDocument webDoc = this.webBrowser1.Document;
            HtmlElement username = webDoc.GetElementById("__login_name");
            HtmlElement password = webDoc.GetElementById("__login_password");

            username.SetAttribute("value", user);
            password.SetAttribute("value", passWord);

            HtmlElementCollection inputTags = webDoc.GetElementsByTagName("input");

            foreach (HtmlElement hElement in inputTags)
            {
                string typeTag = hElement.GetAttribute("type");
                string typeAttri = hElement.GetAttribute("value");

                if (typeTag.Equals("submit") && typeAttri.Equals("Login"))
                {
                    hElement.InvokeMember("click");

                    break;
                }
            }
            button3_Click(sender, e);
            label1.Visible = false ;
            label3.Visible = false;
            uName.Visible = false;
            pwd.Visible = false;
            button1.Visible = false;
            button2.Visible = true;
    }
         else 
        {
            MessageBox.Show("Invalid Username or Password");
        }

    }
private void button3_Click(object sender, EventArgs e)
    {
        HtmlDocument webDoc1 = this.webBrowser1.Document;
        HtmlElementCollection aTags = webDoc1.GetElementsByTagName("a");

        foreach (HtmlElement link in aTags)
        {
            if (link.InnerText.Equals("Show Assigned"))
            {
                link.InvokeMember("click");
                break;
            }
        }
    }
like image 963
Prachur Avatar asked Feb 25 '23 14:02

Prachur


1 Answers

I think what you're describing is that you want to call a method when button B is clicked, but then also call that method when button A is clicked.

protected void ButtonA_Click(...)
{
    DoWork();
}

protected void ButtonB_Click(...)
{
    // do some extra work here
    DoWork();
}

private void DoWork()
{
    // do the common work here
}

Depending on your implementation in the event handlers, you can also just call the event handler of the second button from that of the first, but the above way is the 'right' way to do it.

like image 119
Town Avatar answered Mar 08 '23 04:03

Town