Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which form problem...

Status
Not open for further replies.

RMasters

Programmer
Nov 14, 2000
36
0
0
GB
I want to open a form in code, but I don't know which form class to use until runtime. Lets say I have 3 different form classes
frmMISC1
frmMISC2
frmMISC3

I want to pass a variable and open the required form based on this:
ie- dim form as new frmMISC & "1"
form.show

The problem is that I don't know how many form classes there will be and so it needs to be dynamic.
Thanks for any help, I hope this is all clear.
 
I would use the select case block for this ..

Select Case WhichForm
Case 1
Dim Frm1 As New FormOne : Frm1.Show
Case 2
Dim Frm2 As New FormTwo : Frm2.Show
Case Else
Dim Frm3 As New FormThree : Frm3.Show
End Select

Hope this helps.


Becca

Somtimes, the easy answer is the hardest to find. :)

Still under construction ...
 
Thanks for your prompt reply, the problem is that there could be 2 form classes or twenty and being lazy I don't want to update a case statement for each project, therefore I still need to apply a variable to select the form class that I want. Any ideas ?
 
Use and HashTable. Load the forms into the HashTable and then use the number as the key.

Dim myForm1 As New Form1
Dim myForm2 As New Form1
Dim myForm3 As New Form1



Dim myForms As Hashtable

myForms.Add("1", myForm1)
myForms.Add("2", myForm2)
myForms.Add("3", myForm3)

Then access it using the key

Dim myNewForm As FormatException = CType(myForms(1), Form1)

If you post your code I could show you with real code.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Hi
the following code will creat the form with the string or name of the form passed as parameter
you can pass in the CreateInstance(textbox1.text with the name of your form like
textbox1.text="YouProjectName.YourFormName" like
Textbox1.text="WindowsApplication1.Form1"
Regards
Nouman

Try

Dim tempAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

Dim frm1 As Form = CType(tempAssembly.CreateInstance(textBox1.Text), Form) ' as Form;

frm1.Show()

Catch ex As Exception

MessageBox.Show("Error creating: " + ex.ToString())

End Try

Nouman Zaheer
Software Engineer
MSR
 
Nouman that is exactly what I needed, thanks.
 
Hi Nouman,

I tried above code but it is giving error at line: 'frm1.show'

the error is: 'Object reference not set to an instance of an object'

Do you have any idea that why it is giving this error?

thanks for ur help..

Ash

 
Hi Ash
what how you are passing to the above funciton
I am pretty much sure that the full form name is different in your case means
textbox1.text="YouProjectName.YourFormName" 'check whats your projectName is and whats the form name so if ur project is PROJECT1 and there is a form called FORM1 then
you can use
textbox1.text="PROJECT1.FORM1"
Regards


Nouman Zaheer
Software Engineer
MSR
 
Hi Nouman,

Its working perfectly fine!!!!!!!!

by mistake I was just passing 'formname' and not 'Projectname.formname'

Thanks a lot!

Ash
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top