Hi Cor,
I was hoping that since my shared sub was inside a class, vb somehow new
what type of class the shared sub was called from.
I'm building a CRM system for my company. I want to have a base form that
can inherit all my form from. This base form will have many base stuff ....
forexample I will have three diferent ways to create and show a form ...
1) Normal - just create the form and show it.
2) Single instance - if the form is not yet created, create the form an show
it.
3) Single instance by ID - if for example the customer-form with customer
ID=xxx is not showing, create the form and show it, else show it.
Hope you get my pont.
Here's my code so far (beware of wrappings)....
Public Class BaseForm
Private _singleInstance ID As Guid
Private Shared _singleInstance s As Dictionary(Of Guid, BaseForm)
Public Shared Sub CreateAndShowNo rmal(Optional ByVal owner As
IWin32Window = Nothing)
Dim f As Object =
Activator.Creat eInstance(Refle ction.MethodBas e.GetCurrentMet hod.DeclaringTy pe)
DirectCast(f, BaseForm).Show( owner)
End Sub
Public Shared Sub CreateAndShowSi ngleInstace(Opt ional ByVal owner As
IWin32Window = Nothing)
Call CreateAndShowSi ngleInstace(Gui d.Empty, owner)
End Sub
Public Shared Sub CreateAndShowSi ngleInstace(ByV al ID As Guid, Optional
ByVal owner As IWin32Window = Nothing)
If _singleInstance s Is Nothing Then
_singleInstance s = New Dictionary(Of Guid, BaseForm)
End If
Dim f As BaseForm
If Not _singleInstance s.ContainsKey(I D) Then
Dim fa As Object =
Activator.Creat eInstance(Refle ction.MethodBas e.GetCurrentMet hod.DeclaringTy pe)
f = DirectCast(fa, BaseForm)
f._singleInstan ceID = ID
_singleInstance s.Add(ID, f)
AddHandler f.FormClosed, AddressOf SingleInstanceC losed
Else
f = _singleInstance s(ID)
End If
f.Show()
f.Focus()
End Sub
Private Shared Sub SingleInstanceC losed(ByVal sender As Object, ByVal e
As FormClosedEvent Args)
_singleInstance s.Remove(Direct Cast(sender,
BaseForm)._sing leInstanceID)
End Sub
End Class
If there's a better way to do it, please let me know.
Thanks!
:o)
M O J O
"Cor Ligthert [MVP]" wrote:
Mojo,
It is possible to create objects from classes. But never objects from
objects. (Although object is a class itself as well. To be more precise the
highest class from which every thing derives).
The startup form is a kind of strange thing in VB.Net (but handy) it has
inbuild a Sub main, where in it creates an object from itself. For the same
case you use a Module or Shared Class in which you can create your form
object. Than you can create as much objects from that class as you wish.
I hope this gives an idea.
Cor
"M O J O" <MO**@discussio ns.microsoft.co mschreef in bericht
news:88******** *************** ***********@mic rosoft.com...
Hi,
Instead of doing this....
Public Class Form1
Public Shared Sub CreateAndShow()
Dim f As New Form1
f.Show()
End Sub
End Class
I would like to make it generic by doing something like...
Public Class Form1
Public Shared Sub CreateAndShow()
Dim f As New Me
f.Show()
End Sub
End Class
... but "Me" is not accepted, neither is "Dim f As New TypeOf(Me)".
I would like to put the CreateAndShow in a base form and then inherit from
it and still be able to use the CreateAndShow method.
Any suggestions? TANKS!!! :o)
M O J O