Web browser control in VB 2003

Y

Yuk Tang

Another step on the road to enlightenment, aka producing a UI. I've
added a webbrowser control to a form, and I want it to fill the space
of the form. Playing around with docking, left, top, width, height
resolutely refuses to do this, as it remains docked top-left at around
100,100 within the form, with a size of approx 300,250.

How do I fill the winform with the wb control?

Reading further, it looks like another argument for upgrading to 2005,
which has the webbrowser as standard. Is it any better behaved in
2005?
 
C

Cerebrus

Hi Yuk Tang,

If you want any control to fill all available space, set it's Dock
property to "Fill". This includes the WebBrowser control.

Although, there may be many reasons to upgrade to VS 2005, this does
not seem to be one of them, since the functionality works in VS 2003.

HTH,

Regards,

Cerebrus.
 
Y

Yuk Tang

Cerebrus said:
Hi Yuk Tang,

If you want any control to fill all available space, set it's Dock
property to "Fill". This includes the WebBrowser control.

Although, there may be many reasons to upgrade to VS 2005, this does
not seem to be one of them, since the functionality works in VS 2003.

Here's the bit of code that instantiates the forms within the panels,
as suggested in an earlier thread. There's quite a delay before the
first subform is shown, but I'm sure I'll get to the bottom of that.

Form1 has 2 panels, Panel1 and Panel2. Form2 opens into Panel1 quite
satisfactorily, although with an annoying delay (damn compiler). Form3
containing a webbrowser control opens into Panel2. The webbrowser is
already docked to fill at design time, but I've stuck in the code to
make sure.



Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim f As New Form2
f.TopLevel = False
f2.ControlBox = False
f2.Text = ""
f.WindowState = FormWindowState.Maximized
Me.Panel1.Controls.Add(f)
f.Show()
Dim f2 As New Form3
f2.TopLevel = False
f2.ControlBox = False
f2.Text = ""
f2.WindowState = FormWindowState.Maximized
Me.Panel2.Controls.Add(f2)
f2.AxWebBrowser1.Dock = DockStyle.Fill
f2.Show()
f2.AxWebBrowser1.Navigate2("C:\Documents and Settings\Y Tang\My
Documents\Visual Studio Projects\WindowsApplication6\bin\temp
\notfound.htm")
End Sub



To find out what's happening, I've stuck 4 labels on Form3 to tell me
the size and whereabouts of AxWebBrowser1. When the Form1 loads
maximised, it tells me that AxWebBrowser1 is at 0,0, size 292,273.
Experimenting with stretching and shrinking AxWebBrowser1 tells me the
maximised size should be 809,690, while it does not matter where
AxWebBrowser1 is on the form, Top and Left remain 0,0. Also,
AxWebBrowser1 jumps around the form when I run it on different
occasions (just clicking on the exe), offset by anything up to 150,150
from top left and still telling me that top and left are 0,0.

The strange thing is that on each successive running of the exe, it
jumps further towards the centre of the form, until it's offset by as
much as 150,150. Once it's reached that limit, the next time will see
it jump back to around 32,32. All the while it tells me it's at 0,0,
despite what my eyes tell me.

I've tried setting top and left before setting Dock, I've tried setting
them all before showing the form, I've tried setting them after showing
the form. No joy.
 
C

Cor Ligthert [MVP]

Yuk,

The webbrower .Net 2005 has not the anoying things from the axwebbrowswer
used in 2003

This you see directly if you instance the webbrowsers dynamicly.

I never succeeded in the last option.

For positioning a webbrowser is in my opinion the best to use a statusbar
docked at the bottom a kind of toolbar at the top and the browser docked
full. Have a look on that for the ZOrder. (The sequence they are placed
(add) on the form). That makes how they fill in.

I hope this helps,

Cor
 
S

Simmo

There are some frustrations when working with the webbrowser control and I
have found in the past that answers are extremely difficult to come by.
I've managed to get it to behave properly, and it had to do with some tricks
like setting theatermode to 'off'. I think if you are using a statusbar you
need to set the statusbar property to true. Try setting the anchor in code
rather than the property:

Web.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or
AnchorStyles.Bottom Or AnchorStyles.Right

You may need to set the theatermode setting in code too. I can't remember
all the tricks off the top of my head, but if you get stuck I can make a
working sample to send to you. Please try the different property settings
first though, even the menubar and fullscreen settings. Take note of any
properties that sneakily change themselves after you run the project and
force the property in code.


Troy
 
Y

Yuk Tang

Yuk Tang said:
Another step on the road to enlightenment, aka producing a UI.
I've added a webbrowser control to a form, and I want it to fill
the space of the form. Playing around with docking, left, top,
width, height resolutely refuses to do this, as it remains docked
top-left at around 100,100 within the form, with a size of approx
300,250.

How do I fill the winform with the wb control?

Thanks for the replies. Doing some more experimenting, I've
discovered that the webbrowser is indeed filling the form, and
anchored to the top left. However, it's the form which is not
filling the panel. Here is the code again, with the control box and
title bar back so you can see where the form is inside the panel.



Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim f2 As New Form3
Dim f As New Form2
f.TopLevel = False
f.WindowState = FormWindowState.Maximized
Me.Panel1.Controls.Add(f)
f.Show()
f2.TopLevel = False
f2.WindowState = FormWindowState.Maximized
Me.Panel2.Controls.Add(f2)
f2.Show()
f2.AxWebBrowser1.Navigate("C:\Documents and Settings\Y Tang
\My Documents\Visual Studio Projects\WindowsApplication6\bin\temp
\notfound.htm")
End Sub



As you can see, the code for f and f2 are exactly the same, and I've
c & p'd their designtime properties into PSP to check they're exactly
the same. My next time was to delete the webbrowser control, and lo,
f2 now appears maximised within the panel. I put the webbrowser back
in and lo, f2 appears somewhere in the middle of Panel2.

I've got around this by instancing the webbrowser at runtime instead
of designtime, but how do I refer to the control now? Although
axWebBrowser1 has been added to f2's controls collection, it doesn't
have the name AxWebBrowser1, so f2.AxWebBrowser1 no longer works. Is
there a neater way than finding its index number and referring to it
thus?

Here is the code again, with the troublesome control instantiated and
added within the procedure. The line has been commented out because
while AxWebBrowser1 has been added to f2's controls collection, it is
no longer recognised as f2.AxWebBrowser1.



Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim f2 As New Form3
Dim f As New Form2
f.TopLevel = False
f.WindowState = FormWindowState.Maximized
Me.Panel1.Controls.Add(f)
f.Show()
f2.TopLevel = False

Dim AxWebBrowser1 As New AxSHDocVw.AxWebBrowser
AxWebBrowser1.Dock = DockStyle.Fill
f2.Controls.Add(AxWebBrowser1)

f2.WindowState = FormWindowState.Maximized
Me.Panel2.Controls.Add(f2)
f2.Show()
'f2.AxWebBrowser1.Navigate("C:\Documents and Settings\Y Tang
\My Documents\Visual Studio Projects\WindowsApplication6\bin\temp
\notfound.htm")
End Sub
 
Y

Yuk Tang

Yuk Tang said:
Here is the code again, with the troublesome control instantiated
and added within the procedure. The line has been commented out
because while AxWebBrowser1 has been added to f2's controls
collection, it is no longer recognised as f2.AxWebBrowser1.

[in Form1, the topmost form]
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal
e
As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim f2 As New Form3
Dim f As New Form2
f.TopLevel = False
f.WindowState = FormWindowState.Maximized
Me.Panel1.Controls.Add(f)
f.Show()
f2.TopLevel = False

Dim AxWebBrowser1 As New AxSHDocVw.AxWebBrowser
AxWebBrowser1.Dock = DockStyle.Fill
f2.Controls.Add(AxWebBrowser1)

f2.WindowState = FormWindowState.Maximized
Me.Panel2.Controls.Add(f2)
f2.Show()
'f2.AxWebBrowser1.Navigate("C:\Documents and Settings\Y
Tang
\My Documents\Visual Studio Projects\WindowsApplication6\bin\temp
\notfound.htm")
End Sub

And so with my latest tale of woe. I've found that creating the
WebBrowser at runtime rather than designtime will allow the form to
maximise normally within the panel. But this involves adding the wb
control to the form's control collection without a distinctive name,
thus necessitating knowledge of its index number if I wanted to do
anything with it. So this bright spark decides to confine everything
to the form in true OOP style (I told you I'm getting the hang of
this), creating the webbrowser control as one of its methods and
keeping track of its index number in a property.



[in Form3, the form with the webbrowser control in it]

Public Property WebBrowserIndex() As Integer
Public Property WebBrowserExists() As Boolean
Public Sub LoadWebBrowser()
If WebBrowserExists = False Then
Dim ax As New AxSHDocVw.AxWebBrowser
ax.Dock = DockStyle.Fill
Me.Controls.Add(ax)
WebBrowserExists = True
WebBrowserIndex = Me.Controls.Count - 1
End If
End Sub



Damn. Stack overflow error when I try to set a property in Form3. I
can get Form3.Controls.Count without problems, but an error is thrown
when I assign values to the two properties. The problem is true even
when I use an intermediary, ie. dimming an integer i, passing
Form3.Controls.Count to it and passing i to WebBrowserIndex.

I suspect I'm adding to the sum of human wisdom by showing people how
not to do things.
 
Y

Yuk Tang

Cor Ligthert said:
Yuk,

The webbrower .Net 2005 has not the anoying things from the
axwebbrowswer used in 2003

This you see directly if you instance the webbrowsers dynamicly.

Just found this when browsing through all the VS groups.

http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csha
rp/browse_frm/thread/fa09d17c61bf1f7f

'some people have strange issues with the AxWebBrowser especially
when nested within panels etc'

Describes my problem perfectly.
 
Y

Yuk Tang

Yuk Tang said:
[in Form3, the form with the webbrowser control in it]

Public Property WebBrowserIndex() As Integer
Public Property WebBrowserExists() As Boolean

Damn. Stack overflow error when I try to set a property in Form3.
I can get Form3.Controls.Count without problems, but an error is
thrown when I assign values to the two properties. The problem is
true even when I use an intermediary, ie. dimming an integer i,
passing Form3.Controls.Count to it and passing i to
WebBrowserIndex.

I suspect I'm adding to the sum of human wisdom by showing people
how not to do things.

I am right as always, in saying that I'm adding to the sum of human
wisdom in this way. Perhaps it would help if I expanded the two
properties.



Public Property WebBrowserIndex() As Integer
Get
Return WebBrowserIndex
End Get
Set(ByVal Value As Integer)
WebBrowserIndex = Value
End Set
End Property
Public Property WebBrowserExists() As Boolean
Get
Return WebBrowserExists
End Get
Set(ByVal Value As Boolean)
WebBrowserExists = Value
End Set
End Property



Now why would that generate overflow errors? Idiot.
 
C

Charles Law

Public Property WebBrowserIndex() As Integer
Get
Return WebBrowserIndex
End Get
Set(ByVal Value As Integer)
WebBrowserIndex = Value
End Set
End Property
Public Property WebBrowserExists() As Boolean
Get
Return WebBrowserExists
End Get
Set(ByVal Value As Boolean)
WebBrowserExists = Value
End Set
End Property

Now why would that generate overflow errors? Idiot.

Your properties are recursive because they return/set themselves. Try this

<snip>
Private m_WebBrowserIndex As Integer
Private m_WebBroswerExists As Boolean

Public Property WebBrowserIndex() As Integer
Get
Return m_WebBrowserIndex
End Get
Set(ByVal Value As Integer)
m_WebBrowserIndex = Value
End Set
End Property
Public Property WebBrowserExists() As Boolean
Get
Return m_WebBrowserExists
End Get
Set(ByVal Value As Boolean)
m_WebBrowserExists = Value
End Set
End Property
</snip>

HTH

Charles
 
Y

Yuk Tang

Charles Law said:
Your properties are recursive because they return/set themselves.

I know. That's why I called myself an idiot. As I said, I'm adding
to the sum of human wisdom by showing what not to do.

[snip helpful hints that I should have realised in the first place]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top