From text field to variable??


var myGuess as Integer
var rndNumber as Integer=System.Random.InRange(1, 20)

if myGuess = rndNumber then messageBox("You got it!")
If myGuess > rndNumber then messageBox ("You're too high.")
If myGuess < rndNumber then messageBox ("You're too low.")

In an attempt at a simple guessing game to guess an integer between 1 and 20, I’ve established two variables above. If a player’s myGuess equals the computer’s rndNumber, the player has guessed right.

I’ve scoured around and can’t find my answer to this simple question: How do I make myGuess inherit the value of the typed entry into a text field called GuessText so that the subsequent three IF statements can do their job? (There’s a text field called GuessText on the layout where the guess is posed.)

I could probably use the extensive reference guide to good advantage if I actually knew what to search for - - “move from,” “inherit from,” “make variable take on value from text field and change it to integer,” …… When students don’t have any idea how to spell a word, the teacher would say “look it up in the dictionary.” Well, if they don’t know how to spell it in the first place……

Thanks for any help.

myGuess = GuessText.Value.ToInteger

I agree that the docs could be better organised. But since the number you want is in a textfield, you search for that. Unfortunately then there’s no explicit indication on the Textfield page that the content you want is in the Value property, and that that property is a String type. If there were then also a link to the String type page you’d be home and dry.

You might want to click on the very bottom link on the Textfield page “Send Doc Feedback” and suggest all this, since typing a number into a text field, then getting the number entered as say an integer, then validating it (not in your case perhaps), is a very common thing to do.

What you’re trying to do would make a good example to be added to the Textfield page, IMO.

yes, i also start the search by input a class name, you get methods,propertys,events.
this is the online docu documentation.xojo.com/api/deprecated/textfield.html

you need to store the value in a integer property at your window because (dim,var) in a method only exists in this method.
create random value and store in a integer property.
let the user input a guess
button action
get integer from text
compare and feedback

instead of messageBox you can also use a label.
for a game state you could use a enumeration.

I would suggest you need to make sure the textfield contains a valid entry.

In the TextChange event for the textfield I do this:

Me.Text = stripNonInteger(Me.Text) 'Strip last character if not a valid integer digit

Where:

Public Function stripNonInteger(input As Text) as Text
  // Check last character input and delete if not integer
  
  If input = "" Then Return input
  
  Dim currentlocale As Xojo.Core.Locale
  currentLocale = Xojo.Core.Locale.Current
  Dim decimal As Text = currentlocale.DecimalSeparator
  
  Dim numbers As Text = "1234567890" + decimal
  Dim lastCharIn as Text
  
  lastCharIn = input.right(1)
  
  If numbers.IndexOf(lastCharIn) = -1 Then
    input = input.Left(input.Length - 1)
  end if
  
  Return input
End Function

If you’re just starting out, attempting to learn from the Language Reference can be challenging. To use your analogy, it would be like trying to learn how to write by reading a dictionary.

Instead you might want to start with the Introduction to Programming Guide, the QuickStart/Tutorials and the User Guide:

1 Like

Thanks, Paul. I have found these and am working my way into them. They are very helpful, indeed. There certainly is no lack of good help - - written, video, forum.

2 Likes

Much simpler is to use the Validation Mask in the TextField’s Inspector. Enter ## for up to two numerals, which is all the user needs to enter in this case.

Thumbs up on that suggestion, Tim. Difficult for me to confidently make my point though.

Sorry, should have mentioned that I’m on iOS where I don’t have the Validation Mask option.

Even better

Var myVar As Integer = Integer.Parse(mytextfield.value)

ToInteger could cause an exception. Integer.Parse give 0 when it fails

2 Likes

How would this work ? IsNumeric Statement
(Not my personal code but it is how I would normally check for a text box being numeric)

If not IsNumeric(txtNum.text) then AlertBox.Message = “This Is Not A Number” AlertBox.Title = “This Not A Number” AlertBox.Show Else //Do something else.