我想从Treeview节点使用WebBrowser控件打开表单。每个节点应该打开相同的表单,但WebBrowser控件的URL路径不同。这是我试过的:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
'Define new page in Tab control and Webrowser form, which opens in It
Dim New_Page As New TabPage()
Dim WB As New Web_Form
WB.TopLevel = False
WB.Dock = DockStyle.Fill
WB.FormBorderStyle = FormBorderStyle.None
'Select case for treenode clicks
Select Case TreeView1.SelectedNode.Name
Case Is = "Node1"
'If Web_Form is opened, don't open new one, just focus to It instead
If Application.OpenForms().OfType(Of Web_Form).Any Then
For Each page As TabPage In TabControl1.TabPages
If page.Text = "Pictures" Then
TabControl1.SelectedTab = page
End If
Next page
'And If It's not opened, then open It in Tab control with setting focus on It
Else
New_Page.Controls.Add(WB)
New_Page.Text = "Pictures"
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Pictures")
TabControl1.SelectedTab = New_Page
End If
Case Is = "Node2"
If Application.OpenForms().OfType(Of Web_Form).Any Then
For Each page As TabPage In TabControl1.TabPages
If page.Text = "Pictures" Then
TabControl1.SelectedTab = page
End If
Next page
Else
New_Page.Controls.Add(WB)
New_Page.Text = "Documents"
TabControl1.Visible = True
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Documents")
TabControl1.SelectedTab = New_Page
End If
End Select
End Sub
目前我的代码只打开一次表单,无论我点击哪个节点,都有正确的Webrowser网址。但那时它又不想再打开另一个了。有什么建议 ?提前谢谢!
答案 0 :(得分:0)
解决。如果有人需要这个 - 通过单击Treeview节点填充具有相同表单的Tabcontrol页面的代码。只有2个节点有点长,但可以缩短您的需求。随意提出更好的建议:
Private Sub TreeView1_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
'Define new page in Tab control and Webrowser form, which opens in It
Dim New_Page As New TabPage()
Dim WB As New Web_Form
WB.TopLevel = False
WB.Dock = DockStyle.Fill
WB.FormBorderStyle = FormBorderStyle.None
'If user clicks node
If e.Node.Name Is "Node1" Then
Dim counter As Integer = 0
'If Web_Form is opened, don't open new one, just focus to It instead
If Application.OpenForms().OfType(Of Web_Form).Any Then
For Each page As TabPage In TabControl1.TabPages
If page.Text = "Pictures" Then
TabControl1.SelectedTab = page
counter=1
End If
Next page
Select Case counter
Case 1
'Do nothing
Exit Sub
Case Else
'Create new Tabpage
New_Page.Controls.Add(WB)
New_Page.Text = "Pictures"
TabControl1.Visible = True
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Pictures")
TabControl1.SelectedTab = New_Page
End Select
'And If It's not opened, then open It in Tab control with setting focus on It
Else
New_Page.Controls.Add(WB)
New_Page.Text = "Pictures"
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Pictures")
TabControl1.SelectedTab = New_Page
End If
End if
If e.Node.Name Is "Node2" Then
Dim counter As Integer = 0
If Application.OpenForms().OfType(Of Web_Form).Any Then
For Each page As TabPage In TabControl1.TabPages
If page.Text = "Pictures" Then
TabControl1.SelectedTab = page
counter=1
End If
Next page
Select Case counter
Case 1
'Do nothing
Exit Sub
Case Else
'Create new Tabpage
New_Page.Controls.Add(WB)
New_Page.Text = "Documents"
TabControl1.Visible = True
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Documents")
TabControl1.SelectedTab = New_Page
End Select
Else
New_Page.Controls.Add(WB)
New_Page.Text = "Documents"
TabControl1.Visible = True
TabControl1.TabPages.Add(New_Page)
WB.Show()
WB.WebBrowser1.Navigate("C:\Documents")
TabControl1.SelectedTab = New_Page
End If
End if
End Sub