如何通过编码点击一个按钮? [英] How to click a button through coding?

查看:138
本文介绍了如何通过编码点击一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序两个按钮,我想,当我按下第一个按钮第二个按钮被自动点击(在第一个按钮的事件处理程序,我想通过编码按第二个按钮)。

 私人无效的button1_Click(对象发件人,EventArgs五)
{

密码= pwd.Text;
用户= uName.Text;


loginbackend的obj =新loginbackend();
BOOL的isValid = obj.IsValidateCredentials(用户名,密码,域);
如果(的isValid)
{
loginbackend登录=新loginbackend();
密码= pwd.Text;

login.SaveUserPass(密码);
的HTMLDocument webDoc = this.webBrowser1.Document;
的HtmlElement用户名= webDoc.GetElementById(__ LOGIN_NAME);
的HtmlElement密码= webDoc.GetElementById(__ login_password);

username.SetAttribute(价值,用户);
password.SetAttribute(价值,密码);

HtmlElementCollection inputTags = webDoc.GetElementsByTagName(输入);

的foreach(的HtmlElement hElement在inputTags)
{
串typeTag = hElement.GetAttribute(类型);
串typeAttri = hElement.GetAttribute(值);

如果(typeTag.Equals(提交)及和放大器; typeAttri.Equals(登录))
{
hElement.InvokeMember(点击);

中断;
}
}
button3_Click(发件人,E);
label1.Visible = FALSE;
label3.Visible = FALSE;
uName.Visible = FALSE;
pwd.Visible = FALSE;
button1.Visible = FALSE;
button2.Visible = TRUE;
}
,否则
{
MessageBox.Show(无效的用户名或密码);
}

}
私人无效button3_Click(对象发件人,EventArgs五)
{
的HTMLDocument webDoc1 = this.webBrowser1.Document;
HtmlElementCollection aTags = webDoc1.GetElementsByTagName(一);

的foreach(在的HtmlElement链接aTags)
{
如果(link.InnerText.Equals(显示分配))
{
link.InvokeMember (点击);
中断;
}
}
}


解决方案

我觉得你描述的是你要调用的方法时b按钮被点击,但随后也称点击一个按钮时,该方法。

 保护无效ButtonA_Click(...)
{
的DoWork();
}

保护无效ButtonB_Click(...)
{
//在这里做一些额外的工作
的DoWork();
}

私人无效的DoWork()
{
//在这里做的共同工作
}

根据在事件处理您的实现,也可以只调用第二个按钮的事件处理程序,从第一,但上面的方法是正确的方式做到这一点。


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.

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.

这篇关于如何通过编码点击一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆