简体   繁体   English

如何通过编码点击按钮?

[英]How to click a button through coding?

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

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. 我认为你所描述的是你想要在单击按钮B时调用方法,但是当单击按钮A时也调用该方法。

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. 根据您在事件处理程序中的实现,您也可以从第一个按钮中调用第二个按钮的事件处理程序,但上面的方法是“正确”的方法。

You could just call the method. 你可以调用这个方法。

    private void btnA_Click(object sender, EventArgs e)
    {
        doA();
    }
    private void doA()
    {
        //A stuff
    }

    private void btnB_Click(object sender, EventArgs e)
    {
        doA();
        doB();
    }
    private void doB()
    {
        //B stuff
    }

Or call the _Click method directly; 或者直接调用_Click方法;

    private void btnB_Click(object sender, EventArgs e)
    {
        btnA_Click(sender, e);
        doB();
    }

I guess, you don't really care whether the button is clicked or not, you just care that the code of the second button is executed. 我想,你真的不关心是否点击了按钮,你只关心第二个按钮的代码是否被执行。 So... just call it: 所以...只需称呼它:

void button1_Click(...)
{
    button2_Click(...);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM