Hi @all,
The last solution maybe is suitable if you have only one modal dialog in your project. But as it grows bigger and bigger you maybe gain for a simpler and more generalized solution.
Of course there are some things that can be extracted and put togehter in one single class: ModalDialog
The class should do the main work for us that is always the same in every modal dialog.
* showing the form/Dialog
* substitute the contentpane (needed here)
* transfering any desired object (that will be changed by the user in the Form)
* reacting on the the click-events
* returning the result-value (OK/Cancel)
* disposing the form (unloading)
What other solutions could be thought of?
Maybe another solution would be to have a property-switch in the property-Editor of Jabaco that makes a modal Form out of a normal Form, or maybe you could select a base class others than VB/Form.
Such solutions also look better to me, but in the meantime you can use this small and simple approach:
* Every form that you want to show in modal mode has to do theses two things:
1) implement the interface IModalDialog that only consists of two methods:
|
Jabaco Source
|
1
2
3
4
|
Public Sub Init(md As ModalDialog, obj As Object)
End Sub
Public Function Accept() As Boolean
End Function
|
2) hold a membervariable of the desired object that the user wants to change with the dialog
and implement the procedures to show, validate and to pass editable values.
this is an example of the form:
|
Jabaco Source
|
1
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
|
Option Explicit
Implements IModalDialog
Private m_Pt As MyPoint
Public Sub Init(md As ModalDialog, obj As Object)
'Do some initializing stuff
'First set the buttons
md.SetButtons(Me.BtnOK, Me.BtnCancel)
'Set the desired object
m_Pt = Cast(obj, MyPoint)
'Show the values from the object in the textboxes of the dialog
Me.TxtX.Text = CStr(m_Pt.X)
Me.TxtY.Text = CStr(m_Pt.Y)
End Sub
Public Function Accept() As Boolean
'
' implement something like this
' here the variables will be validated
' and in case everything is valid
' the user input will be passed and
' the function returns True
'
Dim errmsg As String
Dim msg As String = "Please give a numeric value for "
If Not IsNumeric(txtX.Text) Then _
errmsg = errmsg & msg & "x: " & txtX.Text & vbCrLf
If Not IsNumeric(txtY.Text) Then _
errmsg = errmsg & msg & "y: " & txtY.Text & vbCrLf
'if there were errors the errormessage is not null
If Len(errmsg) > 0 Then
MsgBox errmsg
Accept = False
Else
m_Pt.Parse(Me.TxtX.Text, Me.TxtY.Text)
Accept = True
End If
End Function
|
the class ModalDialog:
|
Jabaco Source
|
1
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
Option Explicit
Implements java#awt#Event#MouseListener
Private m_Result As VBMsgBoxResult
Private m_Form As Form
Private m_IForm As IModalDialog
Private m_BtnAccept As CommandButton
Private m_BtnCancel As CommandButton
Public Sub ModalDialog(aFrm As IModalDialog)
m_IForm = aFrm
m_Form = Cast(m_IForm, Form)
m_Form.Visible = False
Me.setContentPane(m_Form.ContentFrame)
Me.setLocation(New Point(m_Form.Left, m_Form.Top))
Me.setSize(New Dimension(m_Form.Width, m_Form.Height))
Me.setTitle(m_Form.Caption)
Me.setModal(True)
End Sub
Public Function ShowDialog(obj As Object) As VBMsgBoxResult
m_IForm.Init(Me, obj)
Me.show
ShowDialog = m_Result
End Function
Public Sub SetButtons(aBtnOK As CommandButton)
Me.SetButtons(aBtnOK, Nothing)
End Sub
Public Sub SetButtons(aBtnOK As CommandButton, aBtnCancel As CommandButton)
m_BtnAccept = aBtnOK
m_BtnCancel = aBtnCancel
m_BtnAccept.Parent.addMouseListener(Me)
m_BtnCancel.Parent.addMouseListener(Me)
End Sub
Public Sub mouseClicked(arg2 As MouseEvent)
If arg2.getComponent = m_BtnAccept Then
If m_IForm.Accept Then
m_Result = vbOK
m_Form.dispose
m_Form = Nothing
Me.dispose
End If
ElseIf arg2.getComponent = m_BtnCancel Then
m_Result = vbCancel
Me.dispose
End If
End Sub
Public Property Get DialogResult As VBMsgBoxResult
DialogResult = m_Result
End Property
Public Property Get IsAccepted() As Boolean
IsAccepted = (m_Result = vbOK)
End Property
Public Property Get IsCanceled() As Boolean
IsCanceled = (m_Result = vbCancel)
End Property
' =+=+=+=+=+=+=+=+ ' will not be used ' =+=+=+=+=+=+=+=+ '
Public Sub mousePressed(arg2 As MouseEvent)
End Sub
Public Sub mouseReleased(arg2 As MouseEvent)
End Sub
Public Sub mouseEntered(arg2 As MouseEvent)
End Sub
Public Sub mouseExited(arg2 As MouseEvent)
End Sub
|
greetings
OlimilO