Adding pages to DocumentWindow

3 posts, 1 answers
  1. Szabolcs
    Szabolcs avatar
    2 posts
    Member since:
    Jun 2013

    Posted 25 Jun 2013 Link to this post

    Hi, I have a problem and can't find a solution for it:
    I have this code that should add a new page to the ...UI.Docking.DocumentWindow on Form1. If I use it on a button on Form1 works fine, but if I try to call it from Form2 the code runs through and show's the messageBox on the end but it doesn't add the page, here's the code:

    private void mNextP1_Click(object sender, EventArgs e)
    {
        string txt = File.ReadAllText("c:\\1.html");
        string style = File.ReadAllText("c:\\styles.css");
     
        Form f = new Form();
        f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        f.Dock = DockStyle.Fill;
        WebBrowser wb = new WebBrowser();
        wb.DocumentText = "";
        wb.Dock = DockStyle.Fill;
        doc = wb.Document.DomDocument as IHTMLDocument2;
        doc.designMode = "On";
        doc.write(txt);
        IHTMLStyleSheet styleSheet = doc.createStyleSheet("", 0);
        styleSheet.cssText = style;
     
        f.Controls.Add(wb);
     
        DocumentWindow dw = new DocumentWindow();
        f.TopLevel = false;
     
        dw.Controls.Add(f);
        f.Show();
        dw.Text = "Contents";
        radDock1.AddDocument(dw);
        radDock1.ActiveWindow = dw;
     
        MessageBox.Show("Done");
    }
    This one works just fine, but here comes the problem:
    I have this on my form2:
    Form1 form1 = new Form1();
    form1.newBlankProject();

    and the code for "form1.newBlankProject();" is the same as above, but for some reason it only show's the messagebox with the text "Done2" and nothing else happens. Here's the code for that one too:

    public void newBlankProject()
    {
        string txt = File.ReadAllText("c:\\1.html");
        string style = File.ReadAllText("c:\\styles.css");
     
        Form f = new Form();
        f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        f.Dock = DockStyle.Fill;
        WebBrowser wb = new WebBrowser();
        wb.DocumentText = "";
        wb.Dock = DockStyle.Fill;
        doc = wb.Document.DomDocument as IHTMLDocument2;
        doc.designMode = "On";
        doc.write(txt);
        IHTMLStyleSheet styleSheet = doc.createStyleSheet("", 0);
        styleSheet.cssText = style;
     
        f.Controls.Add(wb);
     
        DocumentWindow dw = new DocumentWindow();
        f.TopLevel = false;
     
        dw.Controls.Add(f);
        f.Show();
        dw.Text = "Contents";
        radDock1.AddDocument(dw);
        radDock1.ActiveWindow = dw;
     
        MessageBox.Show("Done2");
    }

    Probably is just a stupid mistake on my part, but I can't figure out what the problem may be, any help or ideas are appreciated,

    Thanks,
    Z


  2. Answer
    George
    Admin
    George avatar
    500 posts

    Posted 25 Jun 2013 Link to this post

    Hi Szabolsc,

    Thank you for writing.

    I have looked into your issue and successfully identified your problem. When you use this code into your Form2
    Form1 form1 = new Form1();
    form1.newBlankProject();
    you create a new instance of type Form1, which is different from your original Form1. The solution is to pass the original Form1 to Form2's constructor when creating it, like this:
    //Form2 Constructor
    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
    }
     
    //Form1 Constructor and Form2 Creation
    public Form1()
    {
        InitializeComponent();
        this.form2 = new Form2(this);
        this.form2.Show();
    }

    This way you can always work with the original form and easily call its public methods. Also I am attaching a C# project which shows the complete logic.

    if you need any additional help I would be glad to help.

    Regards,
    George
    Telerik
    RadChart for WinForms is obsolete. Now what?
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Szabolcs
    Szabolcs avatar
    2 posts
    Member since:
    Jun 2013

    Posted 25 Jun 2013 Link to this post

    Thanks George, you've solved my problem.
Back to Top