Colour values as valid data in a programme require secure inputs. The ColorButton component is available for direct but discreet input of colour values. The colour button (gb.form) allows the user to select a colour. The component has two notable properties and has only one special event.
A ColorButton has ColorButton.Color (type Integer) as its dominant property, whose value you can read or set. The property ColorButton.Value is a synonym for the property ColorButton.Color.The event ColorButton_Change() is triggered when you change the colour in the colour dialogue.
Figure 17.14.1.1: Safe input of colours → ColorButton
After clicking on the ColorButton, a colour selection dialogue opens. You can select the colour and then accept the colour value by clicking OK.
Illustration 17.14.1.2: ColorButton Dialogue
In the following source code, the colour value is displayed hexadecimally (→ Figure 17.14.1.1) and stored in a (global) variable iColourValue. This allows the colour value to be used further in the project:
' Gambas class file Public iFarbWert As Integer Public Sub Form_Open() FMain.Center FMain.Resizable = False PictureBox1.Stretch = True PictureBox1.Picture = Picture["Symbols/color.png"] ColorButton.Color = Color.Orange txtColor.Text = "&H" & Hex(ColorButton.Color, 6) End Public Sub btnClose_Click() FMain.Close End '****************************************************** Public Sub ColorButton_Change() iFarbWert = ColorButton.Color txtColor.Text = "&H" & Hex(ColorButton.Color, 6) End
In the Gambas sample editor, two ColorButtons are used to set the text colour and to set the text background colour.
In the procedure Form_Open() the initialisation of the text colour and the text background colour is done:
Object.Lock(ColorButton1) Object.Lock(ColorButton2) ColorButton1.Color = TextEdit1.Format.Color ColorButton2.Color = TextEdit1.Format.Background Object.UnLock(ColorButton1) Object.UnLock(ColorButton2)
Changes to the text colour and the text background colour are implemented in the editor source text like this:
Public Sub ColorButton1_Change() TextEdit1.Format.Color = ColorButton1.Color End Public Sub ColorButton2_Change() TextEdit1.Format.Background = ColorButton2.Color End
With a ColorButton, only the selected colour is visible in the original. If, on the other hand, you use a ButtonBox, you can read the (hexadecimal) colour value in the TextBox and see the colour in the recoloured icon:
Figure 17.14.3.1: ColorButtonBox in action
The source code is quite simple:
Public Sub ColorButtonBox_Click() Dim picColor As Picture If Dialog.SelectColor() Then ColorButtonBox.Text = "Farbwert" ColorButtonBox.Picture = Picture["Symbols/color.png"] Else ' ColorButtonBox.Text = Dialog.Color ' Anzeige dezimal ColorButtonBox.Text = "&H" & Hex(Dialog.Color, 6) picColor = New Picture(16, 12, False) picColor.Fill(Dialog.Color) ColorButtonBox.Picture = picColor Endif End
The original colour selection dialogue (→ Dialog.SelectColor) of the ColorButton is used.
Projects