在c#桌面应用程序

时间:2015-12-16 09:43:08

标签: c# .net visual-studio-2013

我只想在visual studio中使用c#创建多个Web控件。我已经编写了代码,但它只创建了一次,我认为它显示了在循环中最后创建的控件。

以下是代码:

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;
            }
        }
    }
}

这将显示最后创建的Web控件。我认为该计划应该更具活力。我做错了什么?

表单输出,只显示一次。

enter image description here

添加了新代码,我有你提供的代码。代码是

    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);

           }

        }

    }
}

但是在这种情况下问题只有一个浏览器窗口导航页面。 像这样

enter image description here

你可以看到

1 个答案:

答案 0 :(得分:3)

我认为更好的方法是使用FlowLoayoutPanel,这样您就不必处理每个新控件的位置。

像这样:

通过工具箱中的可视化设计器添加FlowLayoutPanel。 然后在designer.cs上,您应自动拥有:

// 
// 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事件:

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");
    }
}

<强>更新

WebBrowser实施的唯一区别是,您需要在将控件添加到面板后调用Navigate方法。检查上面的更新代码。

我已经使用默认Skybound.Gecko.GeckoWebBrowser控件进行了测试,并且它的工作也很好:

enter image description here