By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

creating WebBrowser programmatically

P: 5
Hi,

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();
  6.  
  7.             SuspendLayout();
  8.  
  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;
  21.  
  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);
  28.  
  29.             WindowState = FormWindowState.Maximized;
  30.         }
  31.  
Note as soon as I call Controls.Add(webBrowser) nothing appears. With that line removed I can see my form.

Thanks.
Nov 27 '08 #1
Share this Question
Share on Google+
3 Replies


nukefusion
Expert 100+
P: 221
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

P: 5
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)
to
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

nukefusion
Expert 100+
P: 221
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.