如何用字符串打开窗体
我将窗体名称存在数据库里,主窗体启动时将窗体名称加载到treeview控件,
我想单击节点就打开相应的窗体(以子窗体模式打开),如果子窗体为最小化
或在其他窗体后面就将它还原或显示在最前面?
麻烦各位帮帮我?
谢谢!
[解决办法]
public Sub showfrm(byval frmname as String)
Dim tt As Type = Type.GetType(frmname, True)
Dim obj As ConstructorInfo = tt.GetConstructor(Type.EmptyTypes)
Dim ttobj As Form = obj.Invoke(Nothing)
ttobj.Show()
End sub
[解决办法]
部分代码:
Imports System.Runtime.Serialization.Formatters.Binary
Imports Microsoft.Win32
Public Class PublicSharedFunctions
' ' ' <summary>
' ' ' 取得以%分割的字符串其中的一个值(索引从1开始)
' ' ' </summary>
' ' ' <param name= "strPara "> 传入的字符串 </param>
' ' ' <param name= "intNum "> 要取得第几位的字符串 </param>
' ' ' <returns> </returns>
' ' ' <remarks> </remarks>
Public Shared Function GetSubString(ByVal strPara As String, ByVal intNum As Integer) As String
Dim strReturn() As String
Try
strReturn = Split(strPara, "% ", -1, CompareMethod.Text)
If intNum <= strReturn.Length And intNum > 0 Then
GetSubString = strReturn.GetValue(intNum - 1)
Else
GetSubString = "Err "
End If
Catch ex As Exception
Throw ex
End Try
End Function
' ' ' <summary>
' ' ' 取得以用户自定的分割符分割的字符串其中的一个值(索引从1开始)
' ' ' </summary>
' ' ' <param name= "strPara "> 传入的字符串 </param>
' ' ' <param name= "intNum "> 要取得第几位的字符串 </param>
' ' ' <param name= "strSplit "> 用来分割的字符 </param>
' ' ' <returns> </returns>
' ' ' <remarks> </remarks>
Public Shared Function GetSubString(ByVal strPara As String, ByVal intNum As Integer, ByVal strSplit As String) As String
Dim strReturn() As String
Try
strReturn = Split(strPara, strSplit, -1, CompareMethod.Text)
If intNum <= strReturn.Length And intNum > 0 Then
GetSubString = strReturn.GetValue(intNum - 1)
Else
GetSubString = "Err "
End If
Catch ex As Exception
Throw ex
End Try
End Function
' ' ' <summary>
' ' ' 打开子窗体
' ' ' </summary>
' ' ' <param name= "objDockingTabs "> 父窗体 </param>
' ' ' <param name= "strAllName "> 窗体类名(包括名字空间) </param>
' ' ' <param name= "strFormID "> 窗体编号 </param>
' ' ' <returns> </returns>
' ' ' <remarks> </remarks>
Public Shared Function CallSubSysRun(ByVal CYUserInfo As clsUserInfo, ByVal CYSysInfo As clsSysInfo, ByVal objDockingTabs As C1.Win.C1Command.C1DockingTab, ByVal strAllName As String, ByVal strFormID As String, ByVal objFormPara As Object)
Try
Dim strLocation As String = " "
Dim strNamespace As String
Dim strType As String ' 窗体类名
Dim intLen As Integer
intLen = strAllName.Split( ". ").Length
strNamespace = PublicSharedResource.PublicSharedFunctions.GetSubString(strAllName, 1, ". ") & ". " & PublicSharedResource.PublicSharedFunctions.GetSubString(strAllName, 2, ". ")
strType = strNamespace & ". " & PublicSharedResource.PublicSharedFunctions.GetSubString(strAllName, intLen, ". ")
Dim objRun As New clsDynamic(strLocation, strFormID, strType)
Dim asmContainingForm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(strLocation)
Dim typToLoad As Type = asmContainingForm.GetType(objRun.Type)
If CTabIsRuning(objDockingTabs, strFormID) = False Then
Dim objGenericInstance As Object
objGenericInstance = Activator.CreateInstance(typToLoad)
Dim objFrm As frmBase = CType(objGenericInstance, frmBase)
Dim CTab As New C1.Win.C1Command.C1DockingTabPage
CTab.Name = strFormID
CTab.Text = objFrm.Text
CTab.TabBackColorSelected = Color.LightGoldenrodYellow
objFrm.FormID = strFormID
objFrm.CYUserInfo = CYUserInfo
objFrm.CYSysInfo = CYSysInfo
If Not (objFormPara Is Nothing) Then
objFrm.objFormPara = objFormPara
End If
objFrm.TopLevel = False
objDockingTabs.TabPages.Add(CTab)
CTab.Show()
objFrm.Parent = CTab
objFrm.FormBorderStyle = FormBorderStyle.None
objFrm.WindowState = FormWindowState.Maximized
objFrm.Show()
End If
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
Catch ex As Exception
' 设置鼠标状态为常规
'
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
Throw ex
End Try
End Function
End Function
[解决办法]
樓主這個問題是經典的
以下是調用窗體代碼:
Dim initForm As New DataAccess
frm = initForm.LoadForm(strFormName)
For Each frm In FrmMain.MdiChildren
If frm.Name = Replace(strFormName, "HRJokevinMs. ", " ") Then
frm.MdiParent = FrmMain
frm.Focus()
frm.Show()
frm.WindowState = FormWindowState.Normal
frm.StartPosition = FormStartPosition.CenterScreen
Exit Function
End If
Next
frm = initForm.LoadForm(strFormName)
frm.MdiParent = FrmMain
'frm.Focus()
frm.Show()
frm.WindowState = FormWindowState.Normal
frm.StartPosition = FormStartPosition.CenterScreen
以下是由字符串轉為窗體
Public Function LoadForm(ByVal frm As String) As Object
Dim frmNew As Object = " "
'Dim aa As String = " "
'Dim f As New frm_DepInfo
'frm = f.GetType.FullName
Try
Dim ftype As Type
ftype = Type.GetType(frm)
Dim fassembly As System.Reflection.Assembly
fassembly = System.Reflection.Assembly.GetAssembly(ftype)
frmNew = fassembly.CreateInstance(ftype.ToString)
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "System Information! ")
Finally
End Try
Return frmNew
End Function