User Tools

Site Tools


k12:k12.4:k12.4.4:k12.4.4.2:start

12.4.4.2 Own Dialogues - Project 2

WEDNESDAY.

In the second project - a graphics program - image sizes, tranzparency values and image types can be selected in 3 different combo boxes (CB). At runtime, the respective values stored in the CB.List property are to be changed in three dialogues. At the very first program start, the CB lists are filled with default values. At the end of the program, the current CB lists are saved in three files in a gambas-specific format. At each subsequent program start, these three files are read out and the contents are assigned to the corresponding CB lists. In project 2, all controls not relevant to the three dialogues are combined in the main program in the block '* GRAPHICS PROGRAM*':

B1
Figure 12.4.4.2.1: Graphics program (IDE)

In the 2nd project, these questions also need to be answered:

  • (F1) In which mode is the dialogue called?
  • (F2) Is the dialogue displayed as a preview window of the (main) program or at runtime of the (main) program?
  • (F3) Should a title (window line) be displayed in the dialogue?
  • (F4) Do data have to be passed to the dialogue for modification? With which data type will this data be passed to the dialogue (type, array, collection)?
  • (F5) Will data be read from the dialogue? Is it more than one data? What is the data type of this data?
  • (F6) What should the graphical user interface for the dialogue window look like (controls, arrangement)?

These are the answers that apply to all three dialogues in the same way, as only the arguments change for the dialogue:

  • (A1) The dialogue is always called modally → RunDialog(..) method.
  • (A2) Display at runtime of the (main) program.
  • (A3) Yes → There is a separate dialogue title text for each individual dialogue.
  • (A4) Yes, they are lists of the data type string array.
  • (A5) Yes - one list each. Therefore, the return value of the RunDialog(..) method used to call the dialogue has the same data type string array.
  • (A6) Figure 12.4.4.2.1 shows 3 labels, three combo boxes and three buttons.

In the first project, the special _call(..) method was used to call the dialogue. It allows to use an object syntactically like a function. So what could be more obvious than to write our own function to start a dialogue with its call, to pass data to the dialogue and to read processed data from the dialogue. We give this function the appropriate name RunDialog(..). This function is used in project 2; presented here and then commented on:

[1] Public Function RunDialog(aValue As String[], Optional sTitel As String) As String[]
[2]   Dim hForm As Form
[3]
[4]   hForm = New FEditList
[5]   If Not sTitel Then sTitel = "Edit the data!"
[6]   Me.Text = sTitel
[7]   aTransfer = aValue
[8]
[9]   If Me.ShowModal() = 0 Then ' If the dialogue is aborted, the value 0 is returned
[10]      Return aTransfer
[11]   Else
[12]      Return lboxList.List ' The data is edited in the dialogue in a ListBox
[13]   Endif
[14]
[15] End ' RunDialog(..)

Comment:

  • Two arguments are passed to the RunDialog(..) function in line 1; the second being optional. The first argument is used to pass the data to be processed (data type string array) and the optional argument is used to pass a title to be displayed in the window line. The function value has the same data type as the data to be processed - but this is not mandatory.
  • In contrast to the _call(..) method, you have to take care of creating the dialogue in lines 2 and 4 yourself.
  • Via lines 5 and 6 you realise that a suitable text is always displayed in the dialogue window line.
  • The data transferred to the dialogue are stored in the variable aTransfer (data type string array) so that, among other things, the original data are available if the dialogue is cancelled (line 7).
  • In lines 9 to 13, either the original data are returned or the processed data from the list box list.

Next to the upper combo boxes are buttons, each of which opens a dialogue. To open the dialogue for editing the list of image types, a context menu was added to the corresponding combo box.

B2
Figure 12.4.4.2.2: Main program

When you start the dialogue for editing the image widths, you can edit the displayed list. Even deleting the complete list is possible.

B3
Figure 12.4.4.2.3: Dialogue for editing special data

The above dialogue is realised by this procedure in the main program:

Public Sub btnEditWidth_Click()
  Dim sTitel As String
 
  sTitel = "Edit the list of image widths!"
  cmbImageWidth.List = FEditList.RunDialog(cmbImageWidth.List, sTitel)
  If cmbImageWidth.Index < 0 Then ' List empty?
     cmbImageWidth.Add("640")
  Else
     cmbImageWidth.Text = cmbImageWidth.List[0]
  Endif
End ' btnEditWidth_Click()

Note that only one dialogue is implemented in the next section. This is sufficient, as in all three cases only string arrays are processed, each of which is passed as an argument in the RunDialog(..) function. In case you want to process data with different data types, you would have to create different dialogues.

The source texts for the second project are presented in full for the main program and the dialogue:

Source code (main) program (FMain.class):

[1] ' Gambas class file
[2]
[3] Public hFile As File
[4] Public sFilePath As String
[5] Public sProjectPath As String = Application.Path
[6]
[7] Public Sub Form_Open()
[8]   FMain.Center
[9]   FMain.Resizable = False
[10]   FMain.Text = "Graphics program 'Kritzel 2.0'"
[11]
[12] ' Initialisations:
[13] ' Image width
[14]   sFilePath = sProjectPath &/ cmbImageWidth.Name & ".list"
[15]   If Not Exist(sFilePath) Then
[16]      cmbImageWidth.List = ["240", "480", "640", "820", "1024"]
[17]   Else
[18]      cmbImageWidth.List = ImportData(sFilePath)
[19]   Endif ' Not Exist(sFilePath) ?
[20]   cmbImageWidth.Text = cmbImageWidth.List[2]
[21]   cmbImageWidth.ReadOnly = True
[22]
[23] ' Image transparency
[24]   sFilePath = sProjectPath &/ cmbImageTransparency.Name & ".list"
[25]   If Not Exist(sFilePath) Then
[26]      cmbImageTransparency.List = ["0", "10", "25", "50", "75", "85", "100"]
[27]   Else
[28]      cmbImageTransparency.List = ImportData(sFilePath)
[29]   Endif ' Not Exist(sFilePath) ?
[30]   cmbImageTransparency.Text = cmbImageTransparency.List[0]
[31]   cmbImageTransparency.ReadOnly = True
[32]
[33] ' Image-typ (Extension)
[34]   sFilePath = sProjectPath &/ cmbImageType.Name & ".list"
[35]   If Not Exist(sFilePath)
[36]      cmbImageType.List = ["png", "jpg", "gif", "ico", "svg"]
[37]   Else
[38]      cmbImageType.List = ImportData(sFilePath)
[39]   Endif ' Not Exist(sFilePath) ?
[40]   cmbImageType.Text = cmbImageType.List[0]
[41]   cmbImageType.ReadOnly = True
[42]
[43] End ' Form_Open()
[44]
[45] Public Sub btnEditWidth_Click()
[46]   Dim sTitel As String
[47]
[48]   sTitel = "Edit the list of image widths!"
[49]   cmbImageWidth.List = FEditList.RunDialog(cmbImageWidth.List, sTitel)
[50]   If cmbImageWidth.Index < 0 Then ' List empty?
[51]      cmbImageWidth.Add("640")
[52]   Else
[53]      cmbImageWidth.Text = cmbImageWidth.List[0]
[54]   Endif
[55]
[56] End ' btnEditWidth_Click()
[57]
[58] Public Sub btnEditTransparency_Click()
[59]   Dim sTitel As String
[60]
[61]   sTitel = "Edit the list of transparency values!"
[62]   cmbImageTransparency.List = FEditList.RunDialog(cmbImageTransparency.List)
[63]   If cmbImageTransparency.Index = -1 Then ' Alternative query to line 50: List empty?
[64]      cmbImageTransparency.Add("0")
[65]   Else
[66]      cmbImageTransparency.Text = cmbImageTransparency.List[0]
[67]   Endif
[68]
[69] End ' btnEditTransparenz_Click()
[70]
[71] Public Sub cmbImageType_Menu()
[72]   Dim mnuImageType As Menu
[73]   Dim mnuMenuItem As Menu
[74]
[75] ' A new menu object is created for the image type ComboBox:
[76]   mnuImageType = New Menu(FMain, False)
[77] ' 1. Menu item in the menu mnuImageType
[78]   mnuMenuItem = New Menu(mnuImageType) As "mnuEditTyp"
[79]   mnuMenuItem.Text = "Liste bearbeiten"
[80]   mnuMenuItem.Picture = Stock["edit"]
[81] ' The generated menu is assigned to the image type ComboBox as a PopUp menu
[82]   mnuImageType.Popup
[83]
[84] End ' cmbImageTyp_Menu()
[85]
[86] ' Action when the context menu of the 1st ComboBox has been selected
[87] Public Sub mnuEditTyp_Click()
[88]   Dim sTitel As String
[89]
[90]   sTitel = "Edit the list of image types!"
[91]   cmbImageType.List = FEditList.RunDialog(cmbImageType.List, sTitel)
[92]   If cmbImageType.Count = 0 Then  ' List empty?
[93]      cmbImageType.Add("png")
[94]   Else
[95]      cmbImageType.Text = cmbImageType.List[0]
[96]   Endif
[97]
[98] End ' mnuEditTyp_Click()
[99]
[100] Public Sub Form_Close()
[101] ' Daten-Export
[102]   sFilePath = sProjectPath &/ cmbImageWidth.Name & ".list"
[103]   ExportData(sFilePath, cmbImageWidth.List)
[104]   sFilePath = sProjectPath &/ cmbImageTransparency.Name & ".list"
[105]   ExportData(sFilePath, cmbImageTransparency.List)
[106]   sFilePath = sProjectPath &/ cmbImageType.Name & ".list"
[107]   ExportData(sFilePath, cmbImageType.List)
[108] End ' Form_Close()
[109]
[110] Public Sub btnClose_Click()
[111]   FMain.Close
[112] End ' btnClose_Click()
[113]
[114] ' This routine loads data from a binary, gambas-specific file into a string array
[115] Private Function ImportData(sPath As String) As String[]
[116]   hFile = Open sPath For Read
[117]     Return Read #hFile As Array
[118]   Close #hFile
[119]   Catch
[120]   Message.Error("The data import was faulty!" & gb.NewLine & "Error: " & Error.Text)
[121] End ' ImportData(...)
[122]
[123] ' This routine stores data from a string array in a binary file specific to gambas
[124] Private Sub ExportData(sPath As String, aData As String[])
[125]   If aData.Count = 0 Then
[126]      Return
[127]   Else
[128]      hFile = Open sPath For Write Create
[129]      Write #hFile, aData As Array
[130]      Close #hFile
[131]      Catch
[132]      Message.Error("The data export was faulty!" & gb.NewLine & "Error: " & Error.Text)
[133]   Endif ' aData.Count = 0 ?
[134] End ' ExportData(...)

Source code dialogue (FLogin.class):

[1] ' Gambas class file
[2]
[3] Public aTransfer As String[]
[4]
[5] Public Function RunDialog(aValue As String[], Optional sTitel As String) As String[]
[6]   Dim hForm As Form
[7]
[8]   hForm = New FEditList
[9]   If Not sTitel Then sTitel = "Edit the data!"
[10]   Me.Text = sTitel
[11]   aTransfer = aValue
[12]
[13]   If Me.ShowModal() = 0 Then
[14]      Return aTransfer
[15]   Else
[16]      Return lboxList.List
[17]   Endif
[18]
[19] End ' RunDialog(..)
[20]
[21] Public Sub Form_Open()
[22]   FEditList.Center
[23]   FEditList.Resizable = False
[24]
[25]   lboxList.List = aTransfer
[26]   If lboxList.Count Then
[27]      lboxList.Index = 0
[28]      txtElement.Text = lboxList[0].Text
[29]      txtElement.SetFocus
[30]   Endif
[31]   Refresh
[32] End ' Form_Open()
[33]
[34] Public Sub btnCancel_Click()
[35]   Me.Close(0)
[36] End ' btnCancel_Click()
[37]
[38] Public Sub btnOK_Click()
[39]   Me.Close(1)
[40] End ' btnOK_Click()
[41]
[42] Public Sub btnInsertElement_Click()
[43]   lboxList.Add("Neu_" & CStr(lboxList.Count + 1))
[44]   Refresh
[45]   lboxList.Index = lboxList.Count - 1
[46]   txtElement.Text = lboxList.Current.Text
[47]   txtElement.SetFocus
[48] End ' btnInsertSenderURL_Click()
[49]
[50] Public Sub btnDeleteElement_Click()
[51]   Dim iCurrentIndex As Integer
[52]
[53]   iCurrentIndex = lboxList.Index
[54]   If iCurrentIndex < 0 Then Return
[55]
[56]   If lboxList.Count > 1 Then
[57]      lboxList.Remove(iCurrentIndex)
[58]   Else
[59]      If Message.Question("Delete last item?", "Delete", "Cancel") <> 1 Then Return
[60]      lboxList.Remove(iCurrentIndex)
[61]   Endif
[62]
[63]   If iCurrentIndex >= lboxList.Count Then Dec iCurrentIndex
[64]   lboxList.Index = iCurrentIndex
[65]   txtElement.Text = lboxList[iCurrentIndex].Text
[66]   Refresh
[67]
[68] End '  btnDeleteRadioURL_Click()
[69]
[70] Public Sub btnDown_Click()
[71]   Dim iCurrentIndex As Integer
[72]
[73]   iCurrentIndex = lboxList.Index
[74]   If iCurrentIndex < (lboxList.Count - 1) Then
[75]      Swap lboxList[iCurrentIndex].Text, lboxList[iCurrentIndex + 1].Text
[76]      lboxList.Index = iCurrentIndex + 1
[77]   Endif ' iCurrentIndex < (lstValue.Count - 1)
[78]
[79] End ' btnDown_Click()
[80]
[81] Public Sub btnUp_Click()
[82]   Dim iCurrentIndex As Integer
[83]
[84]   iCurrentIndex = lboxList.Index
[85]   If iCurrentIndex > 0 Then
[86]      Swap lboxList[iCurrentIndex].Text, lboxList[iCurrentIndex - 1].Text
[87]      lboxList.Index = iCurrentIndex - 1
[88]   Endif ' iCurrentIndex > 0
[89]
[90] End ' btnUp_Click()
[91]
[92] Public Sub btnDeleteList_Click()
[93]   If Message.Question("Delete complete list?", "Delete", "Cancel") <> 1 Then Return
[94]   lboxList.Clear
[95]   Refresh
[96] End ' btnDeleteList_Click()
[97]
[98] Public Sub lboxList_Click()
[99]   If Not lboxList.Current Then Return
[100]   txtElement.Text = lboxList.Current.Text
[101]   txtElement.SetFocus
[102]   txtElement.SelectAll
[103] End ' lsbList_Click()
[104]
[105] Public Sub txtElement_Change()
[106]   If lboxList.Current Then
[107]      lboxList.Current.Text = txtElement.Text
[108]   Endif ' lsbCBList.Current ?
[109] End ' txtElement_Change()
[110]
[111] Private Sub Refresh()
[112]   Dim bEnabled As Boolean
[113]
[114]   bEnabled = lboxList.Count
[115]
[116]   txtElement.Enabled = bEnabled
[117]   btnDeleteElement.Enabled = bEnabled
[118]   btnUp.Enabled = bEnabled
[119]   btnDown.Enabled = bEnabled
[120]   btnDeleteList.Enabled = bEnabled
[121]
[122]   If Not bEnabled Then txtElement.Clear
[123]
[124] End ' Refresh()

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k12/k12.4/k12.4.4/k12.4.4.2/start.txt · Last modified: 01.02.2022 (external edit)

Page Tools