Different form classes as parameters for one sub

V

Volker Jobst

Hi,

I have several forms in my application which are all inherited from
System.Windows.Forms.Form. Now I want to write one(!) function which gets
three parameters:

Sub OpenForm(frm as Class, userid As Integer, permission As Integer)
If CheckPermission(userid , permission) Then
Dim temp As frm = New frm
frm.ShowDialog()
Else
MsgBox("Access denied!!")
End if
End Sub

Call:
OpenForm(FormClass, 10, 20)

But I don't know how to pass the class into the sub. I tried Type, Control,
Class, Object and Form but nothing worked.

Thanks a lot.

volker jobst
 
D

Dominique Vandensteen

small example you can use i think...



Public Class Start
Public Shared Sub main()
Dim s As New Start
s.DoIt()

' Application.Run(New Form1)
End Sub

Public Sub DoIt()
Dim showOne As Boolean = False
If (showOne) Then
ShowIt(GetType(FormOne))
Else
ShowIt(GetType(FormToe))
End If
End Sub

Public Sub ShowIt(ByVal formType As Type)
Dim newForm As Form = DirectCast(Activator.CreateInstance(formType),
Form)
newForm.ShowDialog()

End Sub
End Class


Public Class FormOne
Inherits Form

Public Sub New()
Me.BackColor = Color.Red
End Sub
End Class

Public Class FormToe
Inherits Form

Public Sub New()
Me.BackColor = Color.Plum
End Sub
End Class
 
V

Volker Jobst

Thank you Dominique, it looks like this is exactly what I've been looking
for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top