Numeric entry control with Window's Textbox

Source-code only - please, don't post questions here.
Posts: 3205
Joined: Jan 01, 2009 7:03

Numeric entry control with Window's Textbox

Postby BasicCoder2 » Aug 08, 2018 5:24

This thread was originally about numeric entry into a Windows GUI text box.
I didn't want to hijack the thread so I have made another one.
This is my take where any non numeric number in the two input columns is removed.
It removes anything typed into the total column which really shouldn't accept any key input in the first place.
Making the total column a list labels is perhaps the best solution.
There are other kinds of data as seen in a spread sheet. A common one being a date and/or time.
There are other features missing from the textbox such as aligning the text to the left, right or centre.

Code: Select all

'BasicCoder2  attempt at using
'8th August 2018
'Enter numbers into number1 and number 2 column
'Hit Calculate button to get total for each row and running total for numbers in TOTAL column.

#Include "C:\FreeBasic\WinGUI_FB\"

const GRIDX = 50          'top left corner of text box array
const GRIDY = 70

dim shared as integer colWidth(0 to 4)
dim shared as string  text         'text from text box
dim shared as integer mx,my,mb    'mouse variables

colWidth(0)=80              'width for each of the five columns

Dim shared As HWND Window_Main, grid(20, 10), displayText, labels(0 to 4), totalDisplay
Dim Shared As HWND Button_Calc

Dim shared As MSG msg
dim shared as integer maxCol,maxRow

'Create the window with a data grid:
Window_Main = Window_New(100, 100, 900, 500, "Totals Calculator")

'create display text box
displayText = EditBox_New( 10,10, 100, 20, "",, Window_Main)

sub setUpGrid()
    dim as integer nxtPos
    For j as integer = 0 To maxRow-1
        nxtPos = 0
       For i as integer = 0 To maxCol-1
            grid(i, j) = EditBox_New( i+nxtPos + GRIDX, j * 20 + GRIDY, colWidth(i), 20, "abc",, Window_Main)
          EditBox_SetText(grid(i, j), "")
            nxtPos = nxtPos + colWidth(i)

    'create labels
    Labels(0) = Label_New(GRIDX,GRIDY-20,80,20,"  date",, Window_Main)
    labels(1) = Label_New(GRIDX+80,GRIDY-20,200,20,"   Description",, Window_Main)
    labels(2) = Label_New(GRIDX+280,GRIDY-20,80,20,"number 1",, Window_Main)
    labels(3) = Label_New(GRIDX+360,GRIDY-20,80,20,"number 2",, Window_Main)
    labels(4) = Label_New(GRIDX+440,GRIDY-20,80,20," TOTAL  ",, Window_Main)
    for j as integer = 0 to maxRow-1
        Labels(j) = Label_New(GRIDX-40,GRIDY+j*20,40,20,"row " & str(j),, Window_Main)
    next j
end sub

maxCol = 5
maxRow = 4
setUpGrid()  'set up grid

'create calculate button
Button_Calc = Button_New(10, 360, 100, 20, "Calculate!",, Window_Main)
totalDisplay = Label_New(120,10,100,20,"Running Total",, Window_Main)

dim as single num1,num2,total,runningTotal

   SELECT CASE msg.message

         IF msg.hwnd = Button_Calc THEN
            runningTotal = 0
            for j as integer = 0 to 7
               num1 = val(EditBox_GetText(grid(2,j)))
               num2 = val(EditBox_GetText(grid(3,j)))
               total = num1 + num2
               runningTotal = runningTotal + total
               EditBox_SetText(grid(4,j), str(total))
            next j
        End if

       'this erases non numerical entries
       dim as single num
       dim as string strNum
       For j as integer = 0 To maxRow-1
          For i as integer = 0 To maxCol-1
               num = val(EditBox_GetText(grid(i,j)))  'get value
               if num = 0  then
                   strNum = ""
                   strNum = str(num)
               end if
             EditBox_SetText(grid(i, j), strNum)  'convert to string

Loop Until Window_Event_Close(Window_Main, msg)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest