TimeBox formatting

Post

Posted
Rating:
#1 (In Topic #1388)
Avatar
Regular
mandarin is in the usergroup ‘Regular’
 How can I format a TimeBox, to display only hours and minutes (not seconds)? I use Gambas3 version 3.19.5 .

(Setting: TimeBox1.Format = "hh:mm", or TimeBox1.Format = "HH:mm", doesn't work.)

Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
You can't as the feature you want is not implemented until Gambas 3.20. You could make one with a couple of SpinBoxes.

EDIT 15:40

Run this in a Graphical application:-

Code (gambas)

  1. HBox1 As HBox
  2. SpinBoxH As SpinBox
  3. SpinBoxM As SpinBox
  4. Label1 As Label
  5.  
  6. Public Sub Form_Open()
  7.  
  8.   BuildForm
  9.   SpinBoxH.Text = Hour(Now)
  10.   SpinBoxM.Text = Minute(Now)
  11.  
  12.  
  13. Public Sub BuildForm()
  14.  
  15.   With Me
  16.     .Arrangement = Arrange.Vertical
  17.     .Padding = 6
  18.     .Height = 50
  19.     .Width = 124
  20.  
  21.   With HBox1 = New HBox(Me) As "HBox1"
  22.     .Height = 32
  23.     .Width = 112
  24.  
  25.   With SpinBoxH = New SpinBox(HBox1) As "SpinBoxH"
  26.     .Height = 32
  27.     .Width = 48
  28.     .Border = False
  29.  
  30.   With Label1 = New Label(HBox1)
  31.     .Height = 32
  32.     .Width = 16
  33.     .Background = Color.White
  34.     .Alignment = Align.Center
  35.     .Text = ":"
  36.  
  37.   With SpinBoxM = New SpinBox(HBox1) As "SpinBoxM"
  38.     .Height = 32
  39.     .Width = 48
  40.     .Border = False
  41.  
  42.  

<IMG src="https://www.cogier.com/gambas/SpinBoxes.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
mandarin is in the usergroup ‘Regular’
 Thanks! I' ll try to insert the workaround code in my program.

Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
You could manually add a function to hide/show Timebox seconds yourself…
Looking at the code for TimeBox it looks like the new ShowSecond property only hides/shows the last 2 objects the : separator and the last SpinBox.
comp/src/gb.form/.src/Date/TimeBox.class · master · Gambas / gambas · GitLab

(Many compound controls can be hacked/modified in a similar way :) )

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   ShowTimeBoxSeconds(TimeBox1, False)
  5.  
  6.  
  7.  
  8. Public Sub ShowTimeBoxSeconds(tb as TimeBox, Value As Boolean)
  9.  
  10. ' First Try the new way,,,
  11. Try tb.ShowSecond = Value
  12. If Not Error Then Return ' no error so must be new gambas, job done so exit
  13.  
  14. ' if we are here the above method did error so it's older gambas,
  15. ' Emulate the new property ourselves, get a pointer to the main DrawingArea
  16. ' in the TimeBox and hide/show the last 2 Children (the seconds spinbox and
  17. ' the separator), and lastly set seconds value to 0 if hiding.
  18.   Dim hObj As Object = tb.Children[0]
  19.   hObj.Children[hObj.Children.Max].Visible = Value
  20.   hObj.Children[hObj.Children.Max - 1].Visible = Value
  21.  
  22.   If Not Value Then
  23.     hObj = hObj.Children[hObj.Children.Max]
  24.     hObj.Value = 0
  25.  
  26.  
  27.  
  28.  

EDIT: i adjusted the code to use the builtin method if it exists in the gambas version :)
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
mandarin is in the usergroup ‘Regular’
BruceSteers :

The code works, produces this display:


Image

(Click to enlarge)


Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

mandarin said

BruceSteers :

The code works, produces this display:


TimeBox-without-seconds.png

Indeed . i was not happy with the look though.
Even the new built in TimeBox.ShowSecond property just hides the last 2 objects but does not resize the TimeBox to fit the controls so it changes the button style.

I changed the code to adjust the size when changing, i think it looks better…

Code (gambas)

  1.  
  2. Public Sub ShowTBSeconds(TB As TimeBox, Value As Boolean)
  3.  
  4.   Dim dw As DrawingArea = TB.Children[0]
  5.   Dim sb As SpinBox
  6.  
  7.   If Value = dw.Children[dw.Children.Max].Visible Then Return ' do nothing if already set.
  8.  
  9. ' add or subtract the size of a spacer and a SpinBox
  10.   If Value Then
  11.     TB.W += dw.Children[0].W + dw.Children[1].W
  12.   Else
  13.     TB.W -= dw.Children[0].W + dw.Children[1].W
  14.  
  15.   Try TB.ShowSecond = Value
  16.  
  17.   dw.Children[dw.Children.Max].Visible = Value
  18.   dw.Children[dw.Children.Max - 1].Visible = Value
  19.  
  20.   If Not Value Then
  21.     sb = dw.Children[dw.Children.Max]
  22.     sb.Value = 0
  23.  
  24.  

Image

(Click to enlarge)

Online now: No Back to the top
1 guest and 0 members have just viewed this.