简体   繁体   中英

How to link buttons in visual studio with c#?

I just want it so that whenever I click on like button one, like button two should be clicked as well.

So how can I do it?

Actually I have a windows application form and have inserted a web browser in the form. The form URL is some facebook page link. So I want to like that page with my own created button (button 1). Please guide me how can I do it using C#

This is an image which can explain my problem

在此处输入图片说明

Consider the following Form with only a webbrowser in the background and a simple blue colored button. 在此处输入图片说明

In the OnLoad() function, we navigate to the facebook page. When the "Like" button is pressed, all HTML buttons are searched for the one whose class -attribute contains "PageLikeButton". When that like button is found, the click() function is executed upon it.

using System;
using System.Windows.Forms;

namespace FacebookLiker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Go to that link
            webBrowser1.Url = new Uri("https://www.facebook.com/haadischool/?fref=ts");

        }

        private void btnLike_Click(object sender, EventArgs e)
        {
            //Scan the facebook page for a like button

            foreach(HtmlElement button in webBrowser1.Document.GetElementsByTagName("button"))
            {
                //Is this the like button?
                if (button.GetAttribute("className").Contains("PageLikeButton"))
                {
                    //yes, simulate the click!
                    button.InvokeMember("click");
                }
            }
        }
    }
}

As you can see, when I visit the page without having liked it: 在此处输入图片说明

And after I click the Form's "Like" button:

在此处输入图片说明

Ofcourse, this simple demo should be enhanced to handle the cases where there is no "Like"-button at all, you can do that by hooking the DocumentCompleted event of the WebBrowser , then searching for a "Like"-Button there. You can also detect wether the page was already liked. In that case, the class attribute you'll be searching for will be "PageLikedButton" (past-tense of "like").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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