简体   繁体   中英

Set background color of a button in web browser control in windows application

I have a webBrowser control in my win form, I am populating it with some HTML, and the HTML contains a button too. I want to change the color of the button when it is clicked. Tried all option of the code mentioned below but none is working, could someone please help with the right code.

I have tried following 3 options to change the color of the button onClick but none worked

Option 1:
button.SetAttribute("backgroundColor", "red");

Option 2:
button.SetAttribute("style", "background-color:#D84315");

Option 3:
webbrowser_control.Document.GetElementById(buttonID).Style.Replace("background:#0F9D58", "background:#D84315");

When the button is clicked, below code is fired.

string buttonID = webbrowser_control.Document.ActiveElement.Id;
HtmlElement button = webbrowser_control.Document.GetElementById(buttonID);
button.SetAttribute("value", "Reseting...");
button.SetAttribute("backgroundColor", "red");
---some background work using a thread which takes few seconds
button.SetAttribute("value", "Reset");
button.SetAttribute("backgroundColor", "green");

I want the color of the button to be 'red' while the background work is running and then change back to 'green' after it is completed. Strangely below line is working as expected, the text on the button is changing as expected during the background work, its just the background color that's not working.

button.SetAttribute("value", "Reseting...");

below is how i am binding HTML to web browser

string data = "<table border='1'>" +
              "<thead style='background:black;color:white'>" +
                  "<tr style='font: bold'>" +
                        "<td>Id</td>" + 
                        "<td>Client</td>" +
                        "<td>Server</td>" +
                        "<td>Reset</td>" +
                  "</tr>" +
               "</thead> <tbody>";
data = data + "<tr><td>1</td><td>some client</td><td>some server</td>";
data = data + "<td><button id='reset' type='button' style='padding-right:5px; padding-left:5px;margin-right:5px; margin-left:5px;background:#0F9D58;color:white'>  Reset  </button></td></tr>";
data = data + "</tbody></table>";
webbrowser_control.DocumentText = data;

binding button click event on document completed event of the WebBrowser control

private void webbrowser_control_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webbrowser_control.Document.Click += new HtmlElementEventHandler(Document_Click);
}

Capturing the button click event as below

private void Document_Click(object sender, HtmlElementEventArgs ea)
{
    string buttonID = webbrowser_control.Document.ActiveElement.Id;
    HtmlElement button = webbrowser_control.Document.GetElementById(buttonID);
    button.SetAttribute("value", "Reseting...");
    button.SetAttribute("style", "background-color: red;");
    ---some background work using a thread which takes few seconds
    button.SetAttribute("value", "Reset");
    button.SetAttribute("style", "background-color: green;");
}

Wpf Net Core 3.1. You can change style use this example:

        MSHTML.IHTMLDocument3 doc = webBrowser1.Document as MSHTML.IHTMLDocument3;
        var byTagName = doc.getElementsByTagName("div");

        foreach (MSHTML.IHTMLElement element in byTagName)
        {
            string attribute = (string)element.getAttribute("className");
            if (attribute == "res-row")
            {
                element.style.backgroundColor = "#ccc";
            }
        }

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