获取panel内活动的窗体,在线等!!!
在一个主窗体上有左边是treeview控件,右边是panel控件,我的用意是当点击treeview控件中的节点1就是在panel内显示窗体1,当我点击treeview控件中的节点2的时候,在panel控件内显示窗体2并且关闭窗体1,如何做到,谢谢! --------------------编程问答-------------------- 窗体能放入PANEL中吗 --------------------编程问答-------------------- --------------------编程问答--------------------当然可以。你把窗体的添加到panel中就行
--------------------编程问答-------------------- treeview不大熟悉,但是listbox的话很容易的~
frmCheckProjectType cpt = new frmCheckProjectType(this);//你要添加的窗体
cpt.TopLevel = false;
cpt.Parent = this.panel1;//设置parent为panel1
cpt.WindowState = FormWindowState.Maximized;
cpt.Dock = DockStyle.Fill;
cpt.FormBorderStyle = FormBorderStyle.None;
cpt.Show();
比如你有4个form,listbox1里有4个选项,form里有1个listbox,1个panel,你可以参考以下代码
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Panel1.Controls.Clear() '(创意点,想象form是一个control)这样可以关闭掉所有的窗口先
Select Case ListBox1.SelectedIndex
Case 0
'显示form1
Dim form As New Form1
form.TopLevel = False
Panel1.Controls.Add(form)
form.Show()
Case 1
'显示form2
Dim form As New Form2
form.TopLevel = False
Panel1.Controls.Add(form)
form.Show()
Case 2
'显示form3
'代码就自己打咯
Case 3
'显示form4
'代码就自己打咯
End Select
End Sub
在此谢谢Chinajiyong的指导,让我知道原来panel可以加入form的 --------------------编程问答--------------------
不只是panel,GroupBox这些都可以 --------------------编程问答-------------------- Imports System.Windows.Forms
Imports System.Runtime.InteropServices '使用到DllImport需要这个命名空间
Imports System.Reflection '使用到Assembly需要用到这个命名空间
Imports System.Reflection.Emit '使用到ILGenerator 需要用到这个命名空间
Public Class Welcome
Private Sub Welcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MenuManage As New MenuManage
MenuManage.TopLevel = False
Me.MainPanel.Controls.Add(MenuManage)
MenuManage.Dock = DockStyle.Fill
MenuManage.Show()
ds = Service.MenuShow()
buttonx()
End Sub
Private ds As New DataSet
Private Sub buttonx()
'Dim sql As String = "select * from Menu where Root=0 order by id desc"
'Dim cmd As New SqlClient.SqlDataAdapter(sql, Conn)
'Dim ds As New Data.DataTable
'cmd.Fill(ds)
Dim j As Button
For Each i As DataRow In ds.Tables("Menu").Select("WS_MenuClass=0")
j = New Button
'With ds.Rows(i)
j.Name = i("id")
j.Text = i("WS_MenuName")
j.Tag = i("id")
j.Dock = DockStyle.Top
j.TabIndex = i("id")
j.Height = 40
j.Font = New System.Drawing.Font("宋体", 11.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
j.BackgroundImage = System.Drawing.Bitmap.FromFile("di.jpg")
j.FlatStyle = FlatStyle.Flat
j.FlatAppearance.BorderSize = 0
j.ForeColor = Color.White
j.Cursor = System.Windows.Forms.Cursors.Hand
Me.LeftPanel.Controls.Add(j)
'End With
AddHandler j.Click, AddressOf x
AddHandler j.MouseMove, AddressOf ButtonMouseMove
AddHandler j.MouseLeave, AddressOf ButtonMouseLeave
Next
'ds.Reset()
End Sub
Private Sub x(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ClickBtn As Button = CType(sender, Button)
Dim TabIndex As Integer = ClickBtn.TabIndex
For Each btn As Control In LeftPanel.Controls ' [criteria] sort by btn.tabindex desc
If btn.GetType.ToString.Equals("System.Windows.Forms.Button") Then
If btn.TabIndex <> TabIndex Then btn.Dock = DockStyle.Bottom
If btn.TabIndex = TabIndex Then btn.Dock = DockStyle.Top
End If
Next btn
ListView1.Items.Clear()
Dim myvalues(0) As String
Dim listitem As ListViewItem
For Each i As DataRow In ds.Tables("Menu").Select("WS_MenuClass=" & CInt(sender.Tag))
myvalues(0) = i("WS_MenuName")
listitem = New ListViewItem(myvalues)
listitem.Tag = i("WS_FormName") & "|" & i("WS_NameSpace")
listitem.ImageIndex = i("id")
listitem.Font = New System.Drawing.Font("宋体", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
'If flag(sysflag, dx.Rows(i).Item("id")) = True Then
ListView1.Items.Add(listitem)
'End If
Next
'dx.Reset()
AddHandler ListView1.ItemActivate, AddressOf openform
End Sub
Public Function OpenOnce(ByVal myform As Form) As Boolean
Dim form As Form
For Each form In Me.MdiChildren
If form.Text = myform.Text Then
form.Activate()
Return True
End If
Next
Return False
End Function
Private Sub openform(ByVal sender As Object, ByVal e As EventArgs)
Try
Me.MainPanel.Controls.Clear()
Dim namespacestr As Object = CType(sender, ListView).FocusedItem.Tag.ToString
namespacestr = Split(namespacestr, "|")
Dim frm As New Form
If namespacestr(1) <> Application.ProductName Then
Dim A As Assembly = Assembly.LoadFrom(namespacestr(1) & ".dll") '加载程序集
Dim T As Type = A.GetType(namespacestr(1) & "." & namespacestr(0))
frm = Activator.CreateInstance(T)
frm.Text = namespacestr(0).ToString
Else
Dim asm As Assembly = System.Reflection.Assembly.GetExecutingAssembly()
frm = CType(asm.CreateInstance(namespacestr(1) & "." & namespacestr(0)), Form)
frm.Text = sender.focuseditem.text.ToString
End If
frm.TopLevel = False
Me.MainPanel.Controls.Add(frm)
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
frm.Dock = DockStyle.Fill
frm.Show()
Catch ex As Exception
MsgBox("窗体不存在!" + ex.Message.ToString, MsgBoxStyle.Information, "系统提示")
Exit Sub
End Try
End Sub
Private Sub ButtonMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim ClickBtn As Button = CType(sender, Button)
ClickBtn.BackgroundImage = System.Drawing.Bitmap.FromFile("di2.jpg")
End Sub
Private Sub ButtonMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ClickBtn As Button = CType(sender, Button)
ClickBtn.BackgroundImage = System.Drawing.Bitmap.FromFile("di.jpg")
End Sub
End Class
你可以试试这样的 --------------------编程问答-------------------- 你不是要获得活动窗体么?怎么提问和题目不一样啊! --------------------编程问答-------------------- 你不是要获得活动窗体么?怎么提问和题目不一样啊!
补充:.NET技术 , VB.NET