views:

47

answers:

1

In my C# app I get an xml from a server that contains some replies like in a forum thread (with elements like author, time, body, title, whatever).

When I get this xml, I create a new form in which i want to display these replies, and a little text box with an "add reply" button. I'd also like some edit buttons on perhaps my own replies in the reply list displayed in the form.

The simplest way that came to my mind to display the replies is to put a web browser control in the form, generate a full html page in a string from the xml, and throw it in that web browser control. And under it i can put the text box with the add reply button. Everything is ok, except that i have no idea of how i could implement the edit function on my own replies (i mean i could add a link in there... but link to what)

I would like to know if there is a way to get that edit event from the web browser control (my guess is i can't) or another (maybe simple/easy) idea of displaying the replies in a winform using other controls

+2  A: 

Yes, that's possible, you want to turn "design mode" on for the document. Add a reference to Microsoft.mshtml. Start a new Windows Forms project and drop a WB and a button on the form. Make the code look similar to this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        webBrowser1.DocumentText = "<html><body><textarea rows='15' cols='92' name='post-text' id='wmd-input'></textarea></body></html>";
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        button1.Click += button1_Click;
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        mshtml.IHTMLDocument2 doc = webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2;
        doc.designMode = "On";
    }

    private void button1_Click(object sender, EventArgs e) {
        var html = webBrowser1.Document.Body.All["post-text"].InnerHtml;
        // do something with that
        //...
    }
}
Hans Passant
Not sure webBrowser1.ActiveXInstance as mshtml.IHTMLDocument2 would yield anything but null. ActiveXInstance should be the ActiveX Instance of the browser, not the document. webBrowser1.Document.DomDocument should be better.
Sheng Jiang 蒋晟
This code was tested.
Hans Passant