471,604 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,604 software developers and data experts.

creating WebBrowser programmatically


Background - I've create a windows application in VS and added a WebBrowser component and all works well.

I'm now trying to do this programmatically. My application now listens on a socket and when it receives a request it creates a new Form and adds a new WebBrowser.

The problem - All works well without the WebBrowser, the form is visible, but when I do "Controls.Add(webBrowser)" I see nothing, not even something in the windows bar.

Heres the constructor from my class that extends Form...
Expand|Select|Wrap|Line Numbers
  1.     public partial class Browser : Form
  2.     {
  3.         public Browser()
  4.         {
  5. //            InitializeComponent();
  7.             SuspendLayout();
  9.             webBrowser = new WebBrowser();
  10.             webBrowser.AllowWebBrowserDrop = false;
  11.             webBrowser.Dock = DockStyle.Fill;
  12.             webBrowser.IsWebBrowserContextMenuEnabled = false;
  13.             webBrowser.Location = new System.Drawing.Point(0, 0);
  14.             webBrowser.MinimumSize = new System.Drawing.Size(20, 20);
  15.             webBrowser.Name = "webBrowser";
  16.             webBrowser.ScrollBarsEnabled = false;
  17.             webBrowser.Size = new System.Drawing.Size(292, 273);
  18.             webBrowser.TabIndex = 0;
  19.             webBrowser.WebBrowserShortcutsEnabled = false;
  20.             webBrowser.Visible = true;
  22.             AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  23.             AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  24.             ClientSize = new System.Drawing.Size(292, 273);
  25.             Controls.Add(webBrowser);
  26.             Name = "Browser";
  27.             ResumeLayout(true);
  29.             WindowState = FormWindowState.Maximized;
  30.         }
Note as soon as I call Controls.Add(webBrowser) nothing appears. With that line removed I can see my form.

Nov 27 '08 #1
3 9203
221 Expert 100+
I've just copied the code sample into a new form and it appears to run fine. I can see the web browser with no problems and it appears in the taskbar as it should. Maybe the problem is elsewhere in your code?
Nov 27 '08 #2
I've been trying to produce some cut down code to illustrate what i'm doing but can not get anything to work :(

So, i'd appreciate it if u could give me some pointers. Ultimately my app will be a console app or service. I started off with a windows app just to get to grips with Form's and WebBrowser. I've been able to produce a very simple windows app that creates a form with a WebBrowser in, easy!!

I tried to modify the Main method from
Expand|Select|Wrap|Line Numbers
  1. Application.Run(new Browser)
Expand|Select|Wrap|Line Numbers
  1. Thread t = new Thread((new Daemon).Start); t.start()
where Daemon listens for a socket connection then calls New Browser().

I believe it is the lack of call to Application.Run() that is at the heart of the problem.

Basically, how would I create a WebBrowser from a console application?
Nov 27 '08 #3
221 Expert 100+
I think your assumption that the missing Application.Run() statement is to blame would be correct. From what I remember of the top of my head, Application.Run starts a windows message pump for the form that is passed in as a parameter.
With your code example a message pump is not being started, just another thread. I would guess that this is your problem.
This is not something I've ever tried to do myself. Maybe you need to modify your daemon object to initiate an Application.Run for your browser form?
Nov 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Chris Bingham | last post: by
9 posts views Thread by philip | last post: by
2 posts views Thread by Vadim Rapp | last post: by
8 posts views Thread by Ludwig Wittgenstein | last post: by
2 posts views Thread by M R | last post: by
1 post views Thread by =?Utf-8?B?Q2FtZXJvbiBUYXlsb3I=?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.