Instancing a reference of a type

KrAcKeD

Member
Joined
Jul 14, 2006
Messages
9
Programming Experience
5-10
I'm working in VB.NET 2005

To optimise a program, I tried to make a function that would instanciate a windows form based on what type it recieves in parameter

Ex :

I have a form that's called FrmForm1 ( as an example )
and theres a variable named Dlg ( as System.windows.forms.form )
my function is like that

Private sub ShowForm(ByRef FrmForm as System.Windows.Forms.Form)
dlg = Nothing
dlg = CType(dlg, FrmForm)
dlg = new FrmForm()
dlg.show()
end sub

It's hard to explain exactly what i want to do in a formum, and even harder to search for something as precise as this on the net with search tools, i hope that with this example you can understand what exactly i want to do and tell me if there's a way to do it, thanks ( btw, this code doesn't work )
 
Sorry, no i don't understand why you would want to do that. You say that you want to instantiate a windows form based on the type it recieves in the argument? I don't see why you would want to do that or how it could possibly cause an optimisation. I think we need some more detail as to what exactly you are trying to achieve.
 
Oooh, big forum no, no. Something is telling me that a moderator will be along soon to merge or delete this thread.:)

Seriously though, i have just posted a reply to your first thread. Don't worry there aren't many threads on this form that dont get a response, if any!
 
ok



Public class ChooseForm
[...]
Dim DlgForm1 As Frmform1
Dim DlgForm2 As Frmform2
Dim DlgForm3 As Frmform3
Dim DlgForm4 As Frmform4
Dim DlgForm5 As Frmform5
Dim DlgForm6 As Frmform6
Dim DlgForm7 As Frmform7
Dim DlgForm8 As Frmform8
Dim DlgForm9 As Frmform9
Dim DlgForm10 As Frmform10
Dim DlgForm11 As Frmform11
Dim DlgForm12 As Frmform12
Dim DlgForm13 As Frmform13
Dim DlgForm14 As Frmform14
Dim DlgForm15 As Frmform15
Dim DlgForm16 As Frmform16
Dim DlgForm17 As Frmform17
Dim DlgForm18 As Frmform18
Dim DlgForm19 As Frmform19
Dim DlgForm20 As Frmform20

Private Sub CmdShowForm_Click([...])
Select Case Listbox1.SelectedValue
Case "Form1"
DlgForm1 = new FrmForm1
FrmForm1.Show()
* 20

To

Public class ChooseForm
[...]
Dim Dlg As System.Windows.Forms.Form

Private Sub CmdShowForm_Click([...])
Select ListBox1.SelectedValue
Case "Form1"
ShowReport(FrmForm1)

...
you see what i wanna do ?
 
Well yes i can, but i don't think it is a good way to go about it.

Scenario....

The user clicks the listbox once, then does it again, and then again....

you could end up with hundreds of different instances of the form floating around in memory.


what is it you are trying to accomplish on a program level, maybe one of us could find a different way round.
 
The reflection method is very easy, just two lines of code to create a form instance from its string name, but you should think about application design and also keeping track of all forms created if you use the Show method, you could for example add new instances to a collection and when user wants a form you first check if it is already instantiated. If you just want to show the forms modally as dialogs there is no need to maintain collection, just create new dialog, ShowDialog, then dispose. Here are examples of both:
VB.NET:
[SIZE=2][COLOR=#0000ff][COLOR=black]Dim[/COLOR][/COLOR][/SIZE][COLOR=black][SIZE=2] d [/SIZE][SIZE=2]As [/SIZE][SIZE=2]New[/SIZE][SIZE=2] Generic.Dictionary([/SIZE][SIZE=2]Of [/SIZE][SIZE=2]String[/SIZE][SIZE=2], Form)
[/SIZE][/COLOR]
 
Sub showformdialog(ByVal formname As String)
  Dim f As Form = createFormInstance(formname)
  f.ShowDialog()
  f.Dispose()
End Sub
 
Sub showform(ByVal formname As String)
  Dim f As Form
  If d.ContainsKey(formname) = True Then
    f = d.Item(formname)
    If f.IsDisposed = True Then
      f = createFormInstance(formname)
      d.Item(formname) = f
    End If
  Else
    f = createFormInstance(formname)
    d.Add(f.Name, f)
  End If
  f.Show()
  f.Activate()
End Sub
 
Function createFormInstance(ByVal formname As String) As Form
  Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
  Dim f As Form = asm.CreateInstance(asm.GetName.Name & "." & formname)
  f.Name = formname
  Return f
End Function
 
'example usage:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
  showformdialog("Form2")
  showform("Form2")
End Sub
 
Wow, very nice !!!

I though the generic dictionnary automatically populated itself with all forms that were included in your project... it seems that you have to populate them yourself tho, and i was wondering what was the way to do this, i tried playing with the setting but i cant find how to do it
 
System.Collections.Generic.Dictionary is the generic version of Dictionary where you can create a strong type version for your own use, like I did in example.

You confuse with the new default form instance feature in .Net 2.0. I guess they got some kind of collection for these instances behind the scenes, but that is inaccessible for application. Getting and using the default instance is not possible through reflection AFAIK.

Also, vis781 is right about your double posting, you could add post with more information about the problem to the existing post when there is no reply, be patient, it may take a day or so. Merged threads.
 
Back
Top