none
Textbox RRS feed

  • Question

  • Is there a way so you can only put numbers or if they press enter and there are non-numbers in it, it goes back to the previous text or back to being blank?
    Sunday, February 1, 2015 9:23 PM

Answers

  • There is no need to check all digits in a loop!  Use:

    If Text.IsSubText("0123456789",char) Then
      ' character is numeric
    Else
      ' cahracter is not numeric
    EndIf
    

    If necessary you can do without the else, or  Text..... = "False"


    Jan [ WhTurner ] The Netherlands

    Monday, February 2, 2015 4:19 PM
    Answerer
  • Here is a sample.

    Program: SDD265

    Please note that some parts will need to be rewritten (size of text box, screen, text placement, variable names and so on) to accompany your program but, in its core, it filters everything except numbers. If you have any questions let me know. Good luck.

    Monday, February 2, 2015 3:46 AM
  • Thank you WhTurner33 for the help. I should be more familiar with the small basic library and check my logic, which is true for any language.
    Monday, February 2, 2015 11:18 PM

All replies

  • Try Textindow.ReadNumber() - it will only let you enter a numbers.
    Sunday, February 1, 2015 10:42 PM
    Moderator
  • Hello Dinomite07,

    do you mean filtering text/numbers in the text window or Controls.AddTextBox() and Controls.GetTextBoxText()?

    Sunday, February 1, 2015 10:59 PM
  • I'm using graphics window and I would like to only be able to put in numbers in a text box.
    Sunday, February 1, 2015 11:31 PM
  • Here is a sample.

    Program: SDD265

    Please note that some parts will need to be rewritten (size of text box, screen, text placement, variable names and so on) to accompany your program but, in its core, it filters everything except numbers. If you have any questions let me know. Good luck.

    Monday, February 2, 2015 3:46 AM
  • I did not understand this part of your code, Erza. I also did not understand what these variables are used for.  

    space = 8
    p = 0

      For i = 1 To keyWordL
          nums[i] = Text.GetSubText(input,i,1) 'breaks down input into seperate variables
          For j = 0 To 9 'compare one char. w/ all digits ea. keyWordL loop
            If (nums[i] <> digits[j]) Then 
              p = p + 1
            EndIf  
            If (p >= 10) Then 'if true then values of nums doesn't match any digits & thus isn't a number 
              nums[i] = ""
              space = 0
            Else
              space = 8 
            EndIf
          EndFor

    I made my own code using some of your code because I'm too lazy.

    My code has a counter. Every time there is a number it adds to the counter. The code gets the length of the text and if the counter does not equal the length of text then it means there are non-numbers in the text and the textbox is cleared. The code isn't working for some reason.

    gw = 400
    gh = 400
    GraphicsWindow.Height = gh 
    GraphicsWindow.Width = gw 
    GraphicsWindow.Left = 0
    GraphicsWindow.Top = 0
    
    createGUI()
    Controls.ButtonClicked = onButtonClick
    
    Sub createGUI
      bw = gw/2
      bh = gh/10
      bx = (gw - bw)/2
      by = (gh - bh)/2
      
      mainTxtBox = Controls.AddTextBox(bx,by)
      mainButton = Controls.AddButton("Enter",bx,by+bh)
      clearButton = Controls.AddButton("Clear",bx,by+bh*2)
      
      For a = 1 To 9
        digits[a] = a
      EndFor
      
    EndSub
    
    Sub onButtonClick
      p = 0
      If Controls.LastClickedButton = mainButton then  
        Text = Controls.GetTextBoxText(mainTxtBox) 'gets text from textbox
        maintextlength = Text.GetLength(Text) 'gets text length from textbox
        For a = 1 To maintextlength
          num[a] = Text.GetSubText(Text,a,1) 'gets and stores each character
          For a = 1 to 9
            If num[a] = digits[a] Then 
              p = p + 1 'if the character is a number then it adds to the counter
            EndIf
          EndFor
        EndFor
      EndIf
      
      If p < maintextlength Then 'for example, there are 5 numbers and 6 or more characters,  the textbox will then be cleared
        Controls.SetTextBoxText(mainTxtBox,"") 
      EndIf
      
      p = 0
      
      If Controls.LastClickedButton = clearButton then
        Controls.SetTextBoxText(mainTxtBox,"")
      EndIf
      
    EndSub


    • Edited by Dinomite07 Monday, February 2, 2015 5:51 AM
    Monday, February 2, 2015 5:24 AM
  • @ I'm using graphics window and I would like to only be able to put in numbers in a text box.

    Numeric Input TextBox, ID: XSZ207  (using LitDev Extension)

    Monday, February 2, 2015 2:11 PM
    Answerer
  • There is no need to check all digits in a loop!  Use:

    If Text.IsSubText("0123456789",char) Then
      ' character is numeric
    Else
      ' cahracter is not numeric
    EndIf
    

    If necessary you can do without the else, or  Text..... = "False"


    Jan [ WhTurner ] The Netherlands

    Monday, February 2, 2015 4:19 PM
    Answerer
  • I figured it out. I keep forgetting to use textwindow to write out variables. :/
    gw = 400
    gh = 400
    GraphicsWindow.Height = gh 
    GraphicsWindow.Width = gw 
    GraphicsWindow.Left = 0
    GraphicsWindow.Top = 0
    
    createGUI()
    Controls.ButtonClicked = onButtonClick
    
    Sub createGUI
      bw = gw/2
      bh = gh/10
      bx = (gw - bw)/2
      by = (gh - bh)/2
      
      mainTxtBox = Controls.AddTextBox(bx,by)
      mainButton = Controls.AddButton("Enter",bx,by+bh)
      clearButton = Controls.AddButton("Clear",bx,by+bh*2)
      
      For a = 1 To 9
        digits[a] = a
      EndFor
      
    EndSub
    
    Sub onButtonClick
      
      If Controls.LastClickedButton = mainButton then  
        
        p = 0
        
        Text = Controls.GetTextBoxText(mainTxtBox) 'gets text from textbox
        maintextlength = Text.GetLength(Text) 'gets text length from textbox
        For a = 1 To maintextlength
          num[a] = Text.GetSubText(Text,a,1) 'gets and stores each character
          For b = 1 to 9
            If num[a] = digits[b] Then 
              p = p + 1 'if the character is a number then it adds to the counter
            EndIf
          EndFor
        EndFor
        If p < maintextlength Then 'for example, there are 5 numbers and 6 or more characters,  the textbox will then be cleared
          Text = ""
        EndIf
        
        Controls.SetTextBoxText(mainTxtBox,text)
        p = 0
        
      EndIf
      
      If Controls.LastClickedButton = clearButton then
        Controls.SetTextBoxText(mainTxtBox,"")
      EndIf
      
    EndSub

    Monday, February 2, 2015 5:58 PM
  • Rewritten version

    Program: VNR260

    I would like to explain the reasoning behind my code but, as WhTurner33 pointed out (and I agree), my filtering algorithm is excessive. Here is a rewritten version, which works just like the previous one but with less code. Implementing your code should be easier. If, after adding your code, problems persist or if you're still confused or if need anymore help let me know. Take the previous version as a bad example for now.

    Note: If you are familiar with using extensions and don't want to use too much code then you could use pappa lapub's sample. I haven't tried it because I don't use extensions. However, according to the comments provided as well as looking at the code, It does exactly what you are looking for but you'll need to install the extension first to take advantage of it.

    Another Note: Allow me to briefly explain my reasoning behind the previous example, not the rewritten one. The variable "spacing" is used to determine the x coordinate of the number to be printed and increase by 8 so they don't overlap each other. When the "Enter" button is clicked, each character of "input", characters typed in the text box, is assigned to its own variable called nums[x]. For example, if "input" equals "hi" then it becomes "nums[1] = h" and "nums[2] = i". This method allows the program to compare each character individually. In the loop, For j = 0 To 9, each character, "nums", is compared with every value of "digits". Another variable, called p, tracks each time a character of "nums", is not equal to one the values of "digits". Based on this logic, if "p = 10" then this specific character, such as the value of nums[1], doesn't match with any of the ten values of "digits" and therefore isn't a number and will not be printed. Otherwise, if "p<10" then this indicates that it matches one of the values of "digits" and is a number.



    • Edited by Ezra94 Sunday, February 8, 2015 11:46 PM edit
    Monday, February 2, 2015 11:09 PM
  • Thank you WhTurner33 for the help. I should be more familiar with the small basic library and check my logic, which is true for any language.
    Monday, February 2, 2015 11:18 PM