Modal Dialog
Hi @All
I was wondering now a while how to open a Form in modal mode.
in VB6 you are used to the Show-Command of a Form like this:
I found a Thread by Peter, where Manuel said something abound JDialog:
Common Questions to Forms (Open and Close)
but in the heck how to implement this?
in VB6 I am using modal Dialogs like in the following peace of Code:
Form1:
CommandButton: Command1
Form2:
TextBox: Text1, Text2
CommandButton: BtnOK, BtnCancel
in Jabaco you can do this very similar. What you need is a JDialog variable beacuse only the JDialog has the ability to show modal, and you simply transfer alle Controls from the Form to the JDialog.
Use this function in Jabaco to make a JDialog out of your Form:
Now the Code inside the forms, you can write it down something like this:
Form1:
Form2:
Hopefully that this can help a little bit,
greetings
OlimilO
I was wondering now a while how to open a Form in modal mode.
in VB6 you are used to the Show-Command of a Form like this:
|
|
Jabaco Source |
1 |
Form2.Show vbModal |
I found a Thread by Peter, where Manuel said something abound JDialog:
Common Questions to Forms (Open and Close)
but in the heck how to implement this?
in VB6 I am using modal Dialogs like in the following peace of Code:
Form1:
CommandButton: Command1
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Option Explicit Private m_Pt As MyPoint Public Sub Form_Load() Set m_Pt = New_MyPoint(123.456, 345.678) End Sub Private Sub Command1_Click() Dim F As New Form2 If F.ShowDialog(m_Pt) = vbOK Then MsgBox m_Pt.ToString End If End Sub |
Form2:
TextBox: Text1, Text2
CommandButton: BtnOK, BtnCancel
|
|
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 |
Option Explicit Private m_RetVal As VbMsgBoxResult Private m_Point As MyPoint Public Function ShowDialog(Point As MyPoint) As VbMsgBoxResult Set m_Point = Point Text1.Text = m_Point.X Text2.Text = m_Point.Y Call Me.Show(vbModal) ShowDialog = m_RetVal End Function Private Sub BtnOK_Click() m_RetVal = vbOK Call m_Point.Parse(Text1.Text, Text2.Text) Unload Me End Sub Private Sub BtnCancel_Click() m_RetVal = vbCancel Unload Me End Sub |
in Jabaco you can do this very similar. What you need is a JDialog variable beacuse only the JDialog has the ability to show modal, and you simply transfer alle Controls from the Form to the JDialog.
Use this function in Jabaco to make a JDialog out of your Form:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 |
Public Function GetModalDialog(Frm As Form) As JDialog GetModalDialog = New javax#swing#JDialog GetModalDialog.setModal(True) GetModalDialog.reshape(Frm.Left, Frm.Top, Frm.Width, Frm.Height) Dim D As New Dimension(Frm.Width, Frm.Height) GetModalDialog.setMinimumSize(D) 'transfer the whole stuff from the Form to the JDialog GetModalDialog.setContentPane(Frm.ContentFrame) GetModalDialog.setTitle(Frm.Caption) End Function |
Now the Code inside the forms, you can write it down something like this:
Form1:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Option Explicit Private m_Pt As MyPoint Public Sub Form_Load() m_Pt = New MyPoint(123.456, 345.678) End Sub Public Sub Command1_Click() Dim F As New Form2 F.Parent.hide If F.ShowDialog(m_Pt) = vbOK Then MsgBox m_Pt.toString F.Parent.dispose End If End Sub |
Form2:
|
|
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 |
Option Explicit Private m_Dlg As JDialog Private m_RetVal As VBMsgBoxResult Private m_Point As MyPoint Public Function ShowDialog(Point As MyPoint) As VBMsgBoxResult Set m_Point = Point Text1.Text = CStr(Point.X) Text2.Text = CStr(Point.Y) Set m_Dlg = GetModalDialog(Me): m_Dlg.show ShowDialog = m_RetVal Me.Parent.dispose End Function Public Sub BtnOK_Click() m_RetVal = vbOK m_Point.Parse(Text1.Text, Text2.Text) m_Dlg.dispose End Sub Public Sub BtnCancel_Click() m_RetVal = vbCancel m_Dlg.dispose End Sub |
Hopefully that this can help a little bit,
greetings
OlimilO
ModalDialog II
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:
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:
the class ModalDialog:
greetings
OlimilO
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
Location: Cologne, Germany
Occupation: Second Vice President of Distributed Junk and Trash Development
@Widera IT-Solutions:
showing a modal Dialog (as mentioned above from OlimilO) should be the easiest solution:
Greetings ... Peter
showing a modal Dialog (as mentioned above from OlimilO) should be the easiest solution:
|
|
Jabaco Source |
1 2 |
Dim myDialog As New Dialog1 myDialog.Show(vbModal) |
Greetings ... Peter
This post has been edited 1 times, last edit by "Peter" (Sep 19th 2010, 3:27am)
Dialog is a subclass of JDialog.
The implementation can be found here. Dialogs are typically called from within a Form or another Dialog.
However, it might be possible to use a Dialog as user interface of a complete application. But I've never seen this in practice.
Form is derived from AbstractForm as described here. Forms are used as containers for a complete application. They can have min/max icons, system menus, user menus, etc.
You might want to look at the code to get to know the differences in detail.
Greetings
A1880
The implementation can be found here. Dialogs are typically called from within a Form or another Dialog.
However, it might be possible to use a Dialog as user interface of a complete application. But I've never seen this in practice.
Form is derived from AbstractForm as described here. Forms are used as containers for a complete application. They can have min/max icons, system menus, user menus, etc.
You might want to look at the code to get to know the differences in detail.
Greetings
A1880
Similar threads
-
Allgemeine Themen, Fragen und Diskussionen »-
Programm startet nicht
(Nov 28th 2008, 7:46pm)
-
Tips, Tricks, Samples & Tutorials »-
InputBox
(Jan 28th 2009, 12:54pm)
-
General topics, questions and discussions »-
where is the jar-file?
(Jan 24th 2009, 1:49pm)
-
Suggestions »-
Printer object in Jabaco
(Dec 15th 2008, 10:40pm)
-
Allgemeine Themen, Fragen und Diskussionen »-
jar einbinden
(Dec 2nd 2008, 11:00am)
