.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 20th, 2004, 06:28 PM
VBMike VBMike is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 VBMike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 38 sec
Reputation Power: 0
Calling a Form with a string in VB.NET

Hello,

I need to be able to display a form given only a string containing the name of the form. My first attempt looks something like this:
Dim strFormName As String
Dim objform As New Form

strFormName = "frmTest1"
objform = CreateObject(strFormName)

objform.ShowDialog()

I have a form named frmTest1. This gives me an error when CreateObject is executed. Of course, I know that I could just do this:

Dim objForm1 as New frmTest1
Dim objForm2 as New frmTest2
Dim objForm2 as New frmTest3
...
Select case FormNumber
case 1
objForm1.ShowDialog()
case 2
objForm2.ShowDialog()
case 3
objForm3.ShowDialog()
case ...
End Select

But, I do not know whick form I need to display before this function is called. And declaring an instace of all of my app's forms is bringing my system to its knees. I need to find a way to only declare an instance of the form I need.

Thank you for you help,

Mike

Reply With Quote
  #2  
Old April 29th, 2004, 07:54 PM
funkyonex funkyonex is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 funkyonex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Mike,

AFAIK, you do this using reflection.

Code:
 
Imports System
Imports System.Windows.Forms
Imports System.Reflection
 
Public Class ObjectFinder
Public Shared Function CreateObjectInstance(ByVal objectName As String) As Object ' Creates and returns an instance of any object in the assembly by its type name. Dim obj As Object Try
If objectName.LastIndexOf(".") = -1 Then 'Appends the root namespace if not specified.
objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
End If obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
Catch ex As Exception
obj = Nothing
End Try
Return obj End Function Public Shared Function CreateForm(ByVal formName As String) As Form ' Return the instance of the form by specifying its name.
Return DirectCast(CreateObjectInstance(formName), Form)
EndFunction
End Class


Then all you need to do is make a call like this in your code:

Code:
  
Dim frm As Form = ObjectFinder.CreateForm("MyForm")


HTH,
-Beth

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > Calling a Form with a string in VB.NET


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT