2

I just want to create multiple web control using c# in visual studio. I have written the code for that but it create only once, I think it show the control created at the last in the loop.

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDMScreenSharing
{
    public partial class Form2 : Form
    {
        private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;

        public Form2()
        {
            InitializeComponent();
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = true;
            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            int inputNumber =5;
            geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
            for (int i = 1; i <= inputNumber; i++)
            {
                int j = 1;
                String wbname = "br" + i;

                Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
                gw.Width = 200;
                gw.Height = 200;
                gw.Parent = panel1;
                gw.Name = wbname;
                gw.Location = new Point(gw.Width, panel1.Bottom + (i * 30));
                gw.Navigate("http://192.168.1.162:8080");
                geckowebbrouser.Add(gw);
                this.Controls.Add(gw);
                j = j*gw.Width;
            }
        }
    }
}

This will show the web control created at the last. I think the program should be more dynamic for that. What am I doing wrong?

The form output, that show only once.

enter image description here

with the new addition of code i have code that one of you provided. the code is

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDMScreenSharing
{
    public partial class Form2 : Form
    {
        private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;

        public Form2()
        {
            InitializeComponent();
           // this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
           // this.AutoSize = true;
            //this.Padding = new Padding(0, 0, 20, 20);
           // this.StartPosition = FormStartPosition.CenterScreen;
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
            this.flowLayoutPanel1.TabIndex = 0;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            int inputNumber =4;
           geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
           for (int i = 1; i <= inputNumber; i++)
           {

               String wbname = "br" + i;
               Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
               gw.Parent = flowLayoutPanel1;

               gw.Width = 200;
               gw.Height = 200;

               gw.Name = wbname;
               gw.Navigate("http://192.168.1.162:8080");
               geckowebbrouser.Add(gw);
               flowLayoutPanel1.Controls.Add(gw);

           }

        }

    }
}

but the problem in this case only one browser window navigate the page. like this

enter image description here

as you can see.

6
  • What happen if you set location after adding it to Form.Controls? Commented Dec 16, 2015 at 9:46
  • 1
    I think there are multiple overlapping web browser controls being added. Are you sure your point co-ordinate formula is working as expected? Commented Dec 16, 2015 at 9:53
  • nothing happen if i set the location after adding the control to form. Commented Dec 16, 2015 at 9:54
  • i don't think show. i am actually confuse in the point co-ordinate. i have tried multiple time. Commented Dec 16, 2015 at 9:55
  • What is panel1? You set gw.Parent = panel1; maybe it force the web browser to be inside panel1 Commented Dec 16, 2015 at 10:04

1 Answer 1

3

I think better approach will be using FlowLoayoutPanel so that you don't have to handle the position of each new control.

Go like this:

Add the FlowLayoutPanel through visual designer from toolbox. Then on your designer.cs you should automatically have:

// 
// flowLayoutPanel1
// 
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
this.flowLayoutPanel1.TabIndex = 0;

FormLoad event:

private void Form1_Load ( object sender, EventArgs e )
{
    int inputNumber = 5;
    geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
    for ( int i = 1; i <= inputNumber; i++ )
    {
        int j = 1;
        String wbname = "br" + i;
        var gw = new Skybound.Gecko.GeckoWebBrowser();
        gw.Width = 300;
        gw.Height = 300;
        gw.Name = wbname;
        geckowebbrouser.Add(gw);
        flowLayoutPanel1.Controls.Add(gw);

        gw.Navigate("http://www.google.com");
    }
}

Update

The only difference from the WebBrowser implementation is that you need to call the Navigate method after you've added the control to the panel. Check the updated code above.

I have tested with default Skybound.Gecko.GeckoWebBrowser control too, and it's working just fine too:

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

thank your @Evcimen. but the problem is still same. the navigation of link shows only once. i want to create multiple web control that show different navigation. in this solution only first control shows the navigation not remains.
will you send me the code also.. if you don't mind. i have also tried that but the default WebBrowser control is based on the internet explorer and it not support the streaming.
just substitute the Skybound.Gecko.GeckoWebBrowser with WebBrowser, that's the code I have too. I'll try to get it working with gecko browser too if I can find the library:)
i can send you if you provide your email id.
Email sharing is not a sympathic behaviour here, share a link yo the library in your question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.