User Tools

Site Tools


16.8 ButtonBox

The component ButtonBox (gb.qt4) is a composite component. It contains a TextBox and a small button on the (right) side. If you set the ButtonBox. editor. mask property, you can administrate the mask of the internal MaskBox to use the full range of functions. You already know fields of application for a ButtonBox from other contexts such as project selection in the start window of the Gambas IDE (→ chapter 16.6 Text Boxes).

Selected properties are listed and described in the following table:

ButtonBooleanTrueDetermines whether a button is displayed or returns its display status. Without a button the ButtonBox behaves like a TextBox.
PicturePicture….The image on the button is returned or set.
TextStringNullSets the text to be displayed in the ButtonBox or returns this text.
EditorMaskBox~Returns the internal MaskBox of the ButtonBox. All masks of MaskBox.Mask (gb. form) can be used!
ReadOnlyBooleanFalseSpecifies whether the user can change the text in the ButtonBox or not.

Table 16.8.1: Overview of ButtonBox properties

16.8.1 Example 1 - ButtonBox

The use of a ButtonBox is ideal for secure path input. On the one hand, ButtonBox. ReadOnly = True in the dialog only valid path specifications can be selected and on the other hand, the selected path in the. text property is available as a character string for further use in the program. A mask is not used in the first example. An icon from the Gambas collection is used as a picture on the button:


Figure ButtonBox

After a click on the internal button in the ButtonBox opens a dialog:


Figure Directory selection dialog

After selection, all components are displayed in the selected project directory.


Figure Display of the directory path in the ButtonBox

Finally, you can create an archive of all component files of the selected Gambas3 project. The complete project can be found in the download area. Only a section of the source code is displayed here:

' Gambas class file
Public Sub Form_Open()
  FMain.Resizable = True
  ButtonBoxProject.Picture = Picture["icon:/16/open"]
  ButtonBoxProject.ReadOnly = True
  btnList.Enabled = False
  btnMakeArchive.Enabled = False
End ' Form_Open()
Public Sub ButtonBoxProject_Click() ' Diese Prozedur wird nach einem Klick auf das Icon in der ButtonBox generiert
  If Dialog.SelectDirectory() Then Return
  ButtonBoxProject.Text = Dialog.Path
  btnList.Enabled = True
End ' ButtonBoxProject_Click()

16.8.2 Example 2 - ButtonBox with input mask

For example, in order to allow only dates to be entered, a DateChooser dialog box could be displayed after clicking on the button to select a date. You will now say that there is the DateBox class for this. But this is only a modified ButtonBox, as you will find confirmed after a look at the Gambas source code under comp/src/gb.form/.src/Date/DateBox.class. A very small class that actually contains only one ButtonBox, whose mask is based on dates and times and opens a dialog with DateChooser. In the second example, this approach is implemented for a selection of a date. Since the specification of a mask is not automatically followed by a check of the entered date, you are required to allow only valid data from a secure entry:


Figure ButtonBox with mask (date)


Figure ButtonBox with error message when date entered

More secure is the selection of a (valid) date in the date selection dialog. It opens after a click on the small date button:


Figure Date selection dialog


Figure Display of the selected date

The source text is completely specified:

' Gambas class file
Public dDatum As Date
Public Sub Form_Open()
  Me.Resizable = False
End ' Form_Open()
Public Sub btnboxDate_Click()
  btnboxDate.Text = Format(dDatum, gb.ShortDate)
End ' btnboxDate_Click()
Private Sub UpdateMask()
  Dim sMaske As String
  sMaske = Format(Date(1111, 11, 11), gb.ShortDate)
  btnboxDate.Editor.Mask = Replace(sMaske, "1", "0")
End ' UpdateMask()
Public Sub btnboxDate_Activate()
  Label1.Text = ""
  If Not IsDate(btnboxDate.Text) Then
     Label1.Text = String.Chr(187) & " Die Eingabe " & btnboxDate.Text & " ist kein Datum!"
     Label1.Text = String.Chr(187) & " Das Datum " & btnboxDate.Text & " ist o.k."
  Endif ' Not IsDate(bbDate.Text) ?
End ' btnboxDate_Activate()
Public Sub btnboxDate_Change()
  Label1.Text = ""
End ' btnboxDate_Change()

The contents of the procedure UpdateMask () are significant. Only ones are formatted as gb. ShortDate. This observes the current language settings, so that in Germany dd. mm. yyyyy appears. They cannot be zeros, because such a zero date would not generate any output! Now that the format is correct, will the ones for the correct syntax of the MaskBox be replaced with zeros, where 0 stands for any number? chapter 16.7 MaskBox (MaskBox. Mask) and only dates are accepted in the ButtonBox.

Quetext from FDateChooser.class:

' Gambas class file
Public Sub Form_Open()
  With Me
    .Resizable = False
    .Arrangement = Arrange.Vertical
    .Margin = True
    .Spacing = True
  End With
  DateChooser1.Expand = True
  DateChooser1.Mode = DateChooser1.DateOnly ' Only date selection
End ' Form_Show()
Public Sub DateChooser1_Activate()
   FMain.dDatum = DateChooser1.Value ' Saving the date in a (global) variable
End ' DateChooser1_Activate()


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.
k16/k16.8/start.txt · Last modified: 29.09.2023 by emma

Page Tools