Having a form name , how to show this form ?
-
Hi, If know a form named "Form1", how to get the form object and show it ? '************************************** Dim myFormName As String="Form1" Dim myFormObject As System.Windows.Forms.Form '... 'How to get myFormObject of "Form1" ? 'How to show "Form1" ? '... '************************************** Thanks kaiwnyt
-
Hi, If know a form named "Form1", how to get the form object and show it ? '************************************** Dim myFormName As String="Form1" Dim myFormObject As System.Windows.Forms.Form '... 'How to get myFormObject of "Form1" ? 'How to show "Form1" ? '... '************************************** Thanks kaiwnyt
-
Hi, If know a form named "Form1", how to get the form object and show it ? '************************************** Dim myFormName As String="Form1" Dim myFormObject As System.Windows.Forms.Form '... 'How to get myFormObject of "Form1" ? 'How to show "Form1" ? '... '************************************** Thanks kaiwnyt
You're question and code sample are very confusing and don't lend themselves to an clear explaination of what you're doing. THe best I can tell you without a better description of the problem is:
Dim myForm As New Form1 myForm.Show()
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Hi, If know a form named "Form1", how to get the form object and show it ? '************************************** Dim myFormName As String="Form1" Dim myFormObject As System.Windows.Forms.Form '... 'How to get myFormObject of "Form1" ? 'How to show "Form1" ? '... '************************************** Thanks kaiwnyt
Do you mean that you just know the name of the form and you would like to display it? You can try to look in the Forms collection to match the name and display that form, using the Show method. Hope this helps Chandra
-
Hi, If know a form named "Form1", how to get the form object and show it ? '************************************** Dim myFormName As String="Form1" Dim myFormObject As System.Windows.Forms.Form '... 'How to get myFormObject of "Form1" ? 'How to show "Form1" ? '... '************************************** Thanks kaiwnyt
This is not a trivial task. If all you have is the simple form name as a string, then you need to use some sort of Reflection to create an instance of that type, AND you have to use the full name of the type, which is "Namespace.Type". Here's one approach: "Loading Classes on the Fly" http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet10082002.asp Here's a similar way, that does not use a configuration file. I just used the assembly name, which should work, unless you or another developer has placed the forms in a different namespace. Either way, you have to know the namespace of the type. If it happens to be the same as the assembly, which usually it will be, then you're in good shape. If not, you just have to know the namespace, and prepend it to the form name. (And don't forget to use proper exception handling. Do as I say, not as I do.)
Imports System.Reflection
Module Module1
Friend Function GetFormUsingGetType(ByVal formName As String) As Form
Dim myAssembly As [Assembly] = [Assembly].GetExecutingAssembly
Dim myForm As Form
Dim myFormName As String
Dim myType As Type
Dim myAssemblyNameObj As AssemblyName = myAssembly.GetExecutingAssembly.GetName
Dim myAssemblyName As StringmyAssemblyName = myAssemblyNameObj.Name myType = myAssembly.GetType(myAssemblyName & "." & formName, True, True) myFormName = myType.FullName myForm = CType(myAssembly.CreateInstance(myFormName), Form) Return myForm
End Function
Friend Function GetFormUsingActivator(ByVal formName As String) As Form
Dim myForm As Form
Dim myFormName As String
Dim myAssemblyNameObj As AssemblyName = [Assembly].GetExecutingAssembly.GetName
Dim myAssemblyName As String
Dim obj As System.Runtime.Remoting.ObjectHandlemyAssemblyName = myAssemblyNameObj.Name myFormName = myAssemblyName & "." & formName obj = Activator.CreateInstance(myAssemblyName, myFormName) myForm = CType(obj.Unwrap, Form) Return myForm
End Function
End Module