User Tools

Site Tools


k7:k7.2:k7.2.2:start

7.2.2 Project with structures

The following section specifies the full source code for a project that uses a structure and an array of elements of type Struct. If the project reminds you of a database application, you are on the right track.

7.2.2.2.1 Project description

Some special features of the project must be mentioned:

  • You can only enter a maximum of 22 records and you cannot change the number of records at runtime! The terms Struct, Record and Data Set are used synonymously here.
  • By using ComboBoxes and a DateBox, input errors are minimized if the lists for the three ComboBoxes have been filled with care. There is no data check for the last name to be entered in the project!
  • You can navigate through the individual records - as elements of the array CourseList[22] - and display them. You can overwrite individual records, but you cannot delete them.
  • When you exit the program, all data - which are only in memory - will be deleted.
  • A memory management has been implemented for the project to store the contents of the array rate list[22] in a file (data export) or to restore the array rate list[22] with the stored data.
  • The display of all datasets in a TextArea was only used for control purposes during the project test.

Oberfläche
Figure 7.2.2.1.1: Program interface

7.2.2.2.2 Project source code

' Gambas class file
 
Public Struct Student
  Grade As Integer
  BirthDate As Date
  Surname As String
  DG1Course As String
  DG2Course As String
End Struct
 
Public CourseParticipant As New Student
Public Const MAX_STUDENT As Integer = 22
Public CourseList[22] As Struct Student
 
Public iRecordNumber As Integer = -1
Public iCurrentRecord As Integer
 
Public Sub Form_Open()
  FMain.Center
  FMain.Resizable = False
  cmbDGF1.Text = cmbDGF1[0].Text
  cmbDGF2.Text = cmbDGF2[1].Text
  tlblRecords.Foreground = Colour.Red
  tlblRecords.Visible = False
  Status(False)
  btnUpdate.Enabled = False
  btnSave.Enabled = False
  txbSurname.SetFocus
End ' Form_Open()
 
Public Sub SaveNewRecord()
  CourseParticipant.Grade = cmbJGS.Text
  Student.LastName = txbLastName.Text
  Student.BirthDate = dbBirthDate.Value
  Student.DG1Course = cmbDGF1.Text
  CourseParticipant.DG2Course = cmbDGF2.Text
' Store new record in the array CourseList[].
  CourseList[iRecordNumber] = CourseParticipant
End ' SaveNewRecord()
 
Public Sub UpdateCurrentRecord()
  CourseParticipant.Grade = cmbJGS.Text
  Student.LastName = txbSurName.Text
  Student.BirthDate = dbBirthDate.Value
  Student.DG1Course = cmbDGF1.Text
  CourseParticipant.DG2Course = cmbDGF2.Text
' Store current record in array CourseList[].
  CourseList[iCurrentRecord] = CourseParticipant
End ' SaveNewRecord()
 
Public Sub ShowCurrentRecord()
  cmbJGS.Text = CourseList[iCurrentRecord]. Grade
  txbSurName.Text = CourseList[iCurrentRecord].SurName
  dbBirthDate.Value = CourseList[iCurrentRecord].BirthDate
  cmbDGF1.Text = CourseList[iCurrentRecord].DG1Course
  cmbDGF2.Text = CourseList[iCurrentRecord].DG2Course
  tlblRecords.Text = iCurrentRecord + 1
End ' ShowCurrentRecord()
 
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
Public Sub btnNew_Click()
  btnUpdate.Enabled = False
  cmbJGS.Text = cmbJGS[0].Text
  txbSurname.Clear
  dbBirthDate.Value = "6/1/1995"
  cmbDGF1.Text = cmbDGF1[0].Text
  cmbDGF2.Text = cmbDGF2[1].Text
  btnSave.Enabled = True
  Status(True)
  txbLastName.SetFocus
End ' btnNew_Click()
 
Public Sub btnSave_Click()
  If txbSurname.Text = Zero Then
     Message.Warning("The Surname must be entered.!")
     txbSurname.SetFocus
     Return
  Endif ' txbLastname.Text = Zero?
  Inc iRecordNumber
  iCurrentRecord = iRecordNumber
  SaveNewRecord()
  ShowRecords()
  tlblRecords.Visible = True
  tlblRecords.Text = iRecordNumber + 1
  If iRecordNumber = MAX_STUDENT- 1 Then
     Message.Info("Attention!\nThe course enrolment list is full.")
     btnSave.Enabled = False
     btnNew.Enabled = False
     btnUpdate.Enabled = True
     Return
  Endif ' iRecordNumber = MAX_STUDENT-1 ?
  btnSave.Enabled = False
  btnUpdate.Enabled = True
  btnExport.Enabled = True
End ' btnSave_Click()
 
Public Sub btnBack_Click()
  If iCurrentRecord > 0 Then
     Dec iCurrentRecord
     ShowCurrentRecord()
  Endif ' iCurrentRecord > 0 ?
End ' btnBack_Click()
 
Public Sub btnForward_Click()
  If iCurrentRecord < iRecordNumber Then
     Inc iCurrentRecord
     ShowCurrentRecord()
  Endif ' iCurrentRecord < iRecordNumber ?
End ' btnBack_Click()
 
Public Sub btnFirst_Click()
  If iRecordNumber > 0 Then
     iCurrentRecord = 0
     ShowCurrentRecord()
  Endif ' iRecordNumber > 0 ?
End ' btnFirst_Click()
 
Public Sub btnLast_Click()
  If iRecordNumber > 0 Then
     iCurrentRecord = iRecordNumber
     ShowCurrentRecord()
  Endif ' iRecordNumber > 0 ?
End ' btnLast_Click()
 
Public Sub btnUpdate_Click()
  UpdateCurrentRecord()
  ShowRecords()
  btnExport.Enabled = True
End ' btnUpdate_Click()
 
Public Sub Status(iStatus As Boolean)
  If iStatus = True Then
     cmbJGS.Enabled = True
     txbSurname.Enabled = True
     dbBirthDate.Enabled = True
     cmbDGF1.Enabled = True
     cmbDGF2.Enabled = True
  Else
     cmbJGS.Enabled = False
     txbSurname.Enabled = False
     dbBirthDate.Enabled = False
     cmbDGF1.Enabled = False
     cmbDGF2.Enabled = False
     btnExport.Enabled = False
  Endif
End ' Status(iStatus As Boolean)
 
Public Sub ShowRecords()
  Dim iCount As Integer
 
  TextArea1.Clear
  For iCount = 0 To iRecordNumber
   TextArea1.Insert(CourseList[iCount].Grade & gb.NewLine)
   TextArea1.Insert(CourseList[iCount].SurName & gb.NewLine)
   TextArea1.Insert(Format(CourseList[iCount].BirthDate, "dddd - dd. mmmm yyyy") & gb.NewLine)
   TextArea1.Insert(CourseList[iCount].DG1Course & gb.NewLine)
   TextArea1.Insert(CourseList[iCount].DG2Course & gb.NewLine)
   If iCount < iRecordNumber Then TextArea1.Insert("--------------------------" & gb.NewLine)
  Next ' iCount
  TextArea1.Pos = Len(TextArea1.Text) ' ---> Jump to the last line
 
End ' ShowRecords()
 
Public Sub btnExport_Click()
  Dim hFile As File
  Dim iCount As Integer
 
  Dialog.Title = "Export records from a StructDataBase file!"
  Dialog.Filter = ["*.sdb", "StructDataBase files"]
 
  If Dialog.SaveFile() Then Return
  hFile = Open Dialog.Path For Write Create
  For iCount = 0 To iRecordNumber
      Write #hFile, CourseList[iCount] As Student
  Next ' iCount
  Close #hFile
 
  Catch
    Message.Error(Error.Text)
End ' btnExport_Click()
 
Public Sub btnImport_Click()
  Dim hFile As File
  Dim iCount As Integer
 
  Dialog.Title = "Import records from a StructDataBase file!"
  Dialog.Filter = ["*.sdb", "StructDataBase Files"]
 
  If Dialog.OpenFile(False) Then Return
  hFile = Open Dialog.Path For Read
  iCount = 0
  While Not Eof(hFile)
    CourseList[iCount] = Read #hFile As Student
    Inc iCount
  Wend
  Close #hFile
 
  iRecordNumber = iCount - 1
  Print iRecordNumber
  If iCount Then
     Status(True)
     ShowRecords()
     tlblRecords.Visible = True
     btnUpdate.Enabled = True
     iCurrentRecord = 0
     ShowRecords()
     ShowCurrentRecord()
  Else
     Message.Info("The import file is empty!")
     Return
  Endif ' iIndexCount?
 
  Catch
    Message.Error("The data import was faulty!" & gb.Newline & gb.NewLine & "Error:   "
                  & Error.Text)
End ' btnImport_Click()

7.2.2.2.3 Data export and data import

The project has a memory management system that allows you to save a structure in a file (data export) or import the contents of an export file into a structure (data import). All data records are written to the file stream serialized via WRITE, because Gambas supports the “serialization” of variables when they are written to a stream. Gambas formats the data in such a way that they can be recovered from the file later (data import). However, the data in the export file can only be read by Gambas programs, because they are formatted in a gambas-specific format. The extension of the export file is freely selectable; it would be well suited for. txt or. dat or. sdb (StructDataBase). The order of the data in the export file corresponds to the declaration of the structure, as shown in the following excerpt from an export file (age group, date of birth, surname (Last name), course1, course2):

Byte (hex)	0B 00 00 00 36 DF 25   00 00 00 00 00 05 41 61 64 6D 03 42 49 4F 03 43 48 45
Plain text:	11 .  .  .  01 06 1995 .  .  .  .  .  .	A  d  a  m  .  B  I  O  .  C  H  E
Conversion date:	CFloat(Date(1995, 06, 01, 0, 0, 0)) = 2881974 (dez) = 25DF36 (hex)

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.
k7/k7.2/k7.2.2/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools