| 1 |
#Region "Microsoft.VisualBasic::916f52d8290217d38cc37ac363cb21f1, Microsoft.VisualBasic.Core\ApplicationServices\Tools\WinForm\SingleInstanceFormEntry.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#End Region
|
| 48 |
|
| 49 |
Imports System.Windows.Forms
|
| 50 |
Imports System.Drawing
|
| 51 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
|
| 52 |
Imports Microsoft.VisualBasic.Linq
|
| 53 |
|
| 54 |
Namespace Windows.Forms
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
Public Class SingleInstanceFormEntry(Of TForm As Form)
|
| 61 |
|
| 62 |
Dim _clickEntry As Control
|
| 63 |
Dim __getPosition As Func(Of Control, Form, Point)
|
| 64 |
Dim _parentForm As Form
|
| 65 |
Dim _ShowModel As Boolean
|
| 66 |
|
| 67 |
Public ReadOnly Property Form As TForm
|
| 68 |
|
| 69 |
Public Property Arguments As Object()
|
| 70 |
|
| 71 |
Sub New(ControlEntry As Control,
|
| 72 |
Optional ParentForm As Form = Nothing,
|
| 73 |
Optional GetPosition As Func(Of Control, Form, Point) = Nothing,
|
| 74 |
Optional ShowModelForm As Boolean = True)
|
| 75 |
|
| 76 |
_clickEntry = ControlEntry
|
| 77 |
__getPosition = GetPosition
|
| 78 |
_parentForm = ParentForm
|
| 79 |
_ShowModel = ShowModelForm
|
| 80 |
|
| 81 |
AddHandler _clickEntry.Click, AddressOf __invokeEntry
|
| 82 |
|
| 83 |
If GetPosition Is Nothing AndAlso Not _parentForm Is Nothing Then
|
| 84 |
__getPosition = AddressOf __getDefaultPos
|
| 85 |
End If
|
| 86 |
End Sub
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
Sub New(Optional ShowModelForm As Boolean = True)
|
| 93 |
_ShowModel = ShowModelForm
|
| 94 |
End Sub
|
| 95 |
|
| 96 |
Public Sub [AddHandler](Handle As Action(Of Object, EventArgs))
|
| 97 |
AddHandler _clickEntry.Click, New EventHandler(Sub(obj, args) Call Handle(obj, args))
|
| 98 |
End Sub
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
Private Shared Function __getDefaultPos(Control As UserControl, Form As Form) As Point
|
| 108 |
Dim Pt As Point = Form.PointToScreen(Control.Location)
|
| 109 |
Pt = New Point(CInt(Pt.X + Control.Width / 2), CInt(Pt.Y + Control.Height / 2))
|
| 110 |
Return Pt
|
| 111 |
End Function
|
| 112 |
|
| 113 |
Public Sub Invoke(ParamArray InvokeSets As NamedValue(Of Object)())
|
| 114 |
If Not _shown Then
|
| 115 |
_shown = True
|
| 116 |
__invokeSets = InvokeSets
|
| 117 |
Call __invokeEntry(Nothing, Nothing)
|
| 118 |
Else
|
| 119 |
|
| 120 |
End If
|
| 121 |
End Sub
|
| 122 |
|
| 123 |
Dim _shown As Boolean = False
|
| 124 |
Dim __invokeSets As NamedValue(Of Object)()
|
| 125 |
|
| 126 |
Private Sub __invokeEntry(sender As Object, EVtargs As EventArgs)
|
| 127 |
_Form = DirectCast(Activator.CreateInstance(GetType(TForm), Arguments), TForm)
|
| 128 |
|
| 129 |
If Not __getPosition Is Nothing Then
|
| 130 |
Dim pt As Point = __getPosition(_clickEntry, Form)
|
| 131 |
Form.Location = pt
|
| 132 |
End If
|
| 133 |
|
| 134 |
If Not __invokeSets.IsNullOrEmpty Then
|
| 135 |
Dim setValue As New SetValue(Of TForm)()
|
| 136 |
|
| 137 |
For Each arg As NamedValue(Of Object) In __invokeSets
|
| 138 |
Call setValue.InvokeSetValue(Form, arg.Name, arg.Value)
|
| 139 |
Next
|
| 140 |
End If
|
| 141 |
|
| 142 |
If _ShowModel Then
|
| 143 |
Call Form.ShowDialog()
|
| 144 |
Call __clean()
|
| 145 |
Else
|
| 146 |
Call Form.Show()
|
| 147 |
AddHandler Form.FormClosed, AddressOf __clean
|
| 148 |
End If
|
| 149 |
End Sub
|
| 150 |
|
| 151 |
Private Sub __clean()
|
| 152 |
Call Form.Free()
|
| 153 |
_shown = False
|
| 154 |
End Sub
|
| 155 |
End Class
|
| 156 |
End Namespace
|