Page 1 of 1

SpinBar Precision

Posted: Sunday 24th December 2023 9:11pm
by PartierSP
Hey guys! Hope all is well. It's been a while since I was last here but today I am going to break down and finally ask this question that has been bugging me for the past year.

I have a project where I want to set the percision (number of decimal places) on a SpinBar to 4 (aka 0.0001). No matter what I do I can't get it work. It usually just gives me 3 places instead (0.001) or sometimes even resets it to zero places (0).

My end goal is to have the precision set at run time depending on the measuring units the user wants to use. Thus if the user wants to enter the value in millimeters, the SpinBox will have a range of 0.000 to 0.400, or if using inches then the SpinBox will have a range of 0.0000 to 0.0160.

In my program I have tried setting the SpinBox directly with values for Max and Step without success. Thinking it might be Gambas assuming I am using a different datatype, I tried the following:
Public Sub CBUnits_Click()

  Dim fStep As Float
  Dim fMax As Float
  
  If CBUnits.Index = 0 Then 
    fStep = 0.0001
    fMax = 0.0160
  Else 
    fStep = 0.001
    fMax = 0.400
  Endif

  SBMeasured.Step = fStep
  SBMeasured.MaxValue = fMax

End

But again, no success. Any ideas?

Re: SpinBar Precision

Posted: Monday 25th December 2023 1:20pm
by cogier
I don't think you can do what you want with a SpinBar. So I suggest you create your own control. Try running the code below in a Graphical Application.

Image

HBox1 As HBox
LabelText As Label
LabelValue As Label
Slider1 As Slider

Public Sub Form_Open()
  
  BuildControl 
  LabelText.Text = "Select value "
  
End

Public Sub BuildControl()
  
  With Me
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .W = 500
    .H = 50
  End With
  
  With HBox1 = New HBox(Me)
    .H = 28
  End With
  
  With LabelText = New Label(HBox1) As "LabelText"
    .AutoResize = True
    .Padding = 5
  End With
  
  With Slider1 = New Slider(HBox1) As "Slider1"
    .Expand = True
    .maxValue = 4000
    .MinValue = 0
  End With
  
  With LabelValue = New Label(HBox1) As "LabelValue"
    .W = 75
    .Padding = 5
    .Text = "0.0000"
  End With
  
End

Public Sub Slider1_Change()
  
  LabelValue.Text = Format(Slider1.Value / 10000, "0.0000")
  
End

Re: SpinBar Precision

Posted: Tuesday 26th December 2023 4:55am
by vuott
PartierSP wrote: Sunday 24th December 2023 9:11pma range of 0.0000 to 0.0160.
By manipulating the "SpinBar" Control a bit, I got :? this:
Public Sub Form_Open()

  Dim da As DrawingArea
  Private tw As Integer
  Private th As Integer

  With SpinBar1
    .W = 100
    .H = 50
    .X = 0
    .MinValue = 0.0
    .MaxValue = 16.0
  End With
 
  With da = New DrawingArea(SpinBar1.Children[0]) As "DASpin"
    .W = SpinBar1.W - 19
    .H = SpinBar1.H - 2
    .X = 1
    .Y = 1
    .Font.Size = 11
    .Background = Color.White
  End With

  System.Language = "en_US.UTF-8"

End


Public Sub DASpin_Draw()

  With Paint
    .Brush = .Color(&2f8cc5)
    .Rectangle(0, 0, (SpinBar1.W * SpinBar1.Value) / 16.0, da.H)
    .Fill
    tw = .Font.TextWidth("0.0000")
    th = .Font.TextHeight("0.0000")
    .Brush = .Color(Color.Black)
    .DrawText(Format(SpinBar1.Value / 1000, "#.0000"), da.W - tw, (da.H / 2) - (th / 2), tw, th)
    .End
  End With
  
End


Public Sub SpinBar1_Change()

  Me.Title = Format(SpinBar1.Value / 1000, "#.0000")

End

Re: SpinBar Precision

Posted: Tuesday 26th December 2023 7:30am
by PartierSP
Thanks guys. I am going to try both of those to see which one I prefer. :D

I have done very little work on custom controls so these techniques are little new to me.

Re: SpinBar Precision

Posted: Tuesday 26th December 2023 2:51pm
by vuott
cogier's solution, suggest you create ex novo your own Control, is the most expedient and most appropriate.