Dynamic Controls - referencing

M

msdev

Hello,

I am creating my own webbrowser to learn VB .Net. I am stuck on an issue
with regards to dynamically-created controls, in this case tabs on a
tabcontrol and webbrowsers created within each new tab.

Using AddHandler in a class, I can get the delegate Sub to display the name
of the webbrowser that has been clicked (by using msgbox(sender.tag), where
I set the .tag value when creating the control).
What I need to be able to do is use Back, Forward, Stop, Print buttons etc
on ANY webbrowser currently with focus.

In a nutshell at runtime the user might have 6 tabs in a tabcontrol contain
a webbrowser each. How do I pass the value of the currently-selected
webbrowser, dynamically created, to these buttons from the class where I
created the tabs/webbrowsers?

I guess to do something like (quazi code)

webbrowser(x).goback

is what I seek....any ideas? Completely stumped

O.
 
L

lazlo

O

Having the same problem myself - if anyone answers this, it will be a
miracle?

Gray
 
L

lazlo

jvb

yep started there first of all. Problem is, it gives me the tabcontrol tab
that has been selected, but not the webbrowser contained within that tab.

I have been playing with addhandler, as I am sure it had something to do
with it, but to no avail.

What I need to do is to send the value of the currently-selected browser to
some "central settings" point, so that one set of buttons can control any
number of dynamically-created webbrowsers. The stumbling block for me is
trying to send the "currently selected" webbrowser to the goback, goforward,
print, stop buttons, so they only apply to that webbrowser in focus. If the
user selects another tab/webbrowser, the buttons will switch to work with
that focussed browser and so on.

I am probably not explaining it too well! Sorry

Did this help?
 
J

jvb

If you know tab page selected, can you make the assumption that the
browser on that page is active? So in your code, in the button's click
event, you could try...

CType(TabControl1.SelectedTab.Controls.Item(TabControl1.SelectedTab.Controls.GetChildIndex(<Your
Dynamically Created Web Control>)), AxSHDocVw.AxWebBrowser).GoBack()

AxSHDocVw.AxWebBrowser is the browser control i use, not sure if you
are using the same.
 
J

Jeff Jarrell

You can always track the references yourself. i.e. when you dynamically
create the tab, put in a hashtable the TabIdentifier as the key and a
reference to the webbrowser control contained on that tab. Then as you
change tabs you can set the object reference of web browser (the reference
that is wired to the events) to the one from the hashtable.

something like this (fake code)

dim myBrowserControl as webbrowser

form_load
addhandler browser.next, someproc

init_tab
create new tab tab
myhash(tabindex) = webrowsercontrol on this tab

sub TabChange
myBrowserControl = myHash(tabIndex)
end
 
G

gray

Jeff,

I think you might be on to something here. Is there any chance you can
expand on your code a bit? I am new to dynamic controls in vb.net 2005.

If I replace webbrowser1.goback with mybrowsercontrol.goback, will this
work too?

G
 
J

Jeff Jarrell

It is going to look a little like this. I'd be careful to pick a good key
for the dictionary. In this case I am using the count of tab pages as part
of the key but that won't work if you ultimately take tab pages out. You
might also want to look at the "InitializeComponent" on similar components
created at design time and set those same properties on the creation of the
dynamic controls.

Dim _dctWebBrowserReferences As New Hashtable

Friend WithEvents _webbrowser As WebBrowser

Private Sub newPage()

Dim myNewTabPage As New TabPage

myNewTabPage.Name = "Page" & Me.TabControl1.TabCount + 1

myNewTabPage.Text = "Page" & Me.TabControl1.TabCount + 1

Dim myNewBrowser As New WebBrowser

myNewBrowser.Name = "WebBrowser" & Me.TabControl1.TabCount + 1

myNewBrowser.Dock = DockStyle.Fill

Dim key As String

key = myNewTabPage.Text

_dctWebBrowserReferences(key) = myNewBrowser

myNewTabPage.Controls.Add(myNewBrowser)

Me.TabControl1.Controls.Add(myNewTabPage)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

newPage()

End Sub

Private Sub TabControl1_TabIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TabControl1.TabIndexChanged

Dim myTabPage As TabPage

myTabPage = TabControl1.SelectedTab

Dim key As String

key = myTabPage.Text

Me._webbrowser = CType(Me._dctWebBrowserReferences(key), WebBrowser)

End Sub
 

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