Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Only Number in textField C#

Discussion in 'Immediate Mode GUI (IMGUI)' started by Babator, Jan 11, 2010.

  1. Babator

    Babator

    Joined:
    Dec 3, 2009
    Posts:
    13
    Hi and happy new year evryone.
    I try to make an input control in a TextField but i can not use the system.windows.form under Mono.
    Is there a way to only write number and use delete and suppr key in a textField with C#.
    thanks
     
  2. Babator

    Babator

    Joined:
    Dec 3, 2009
    Posts:
    13
    Ok I find a nice way to do it :D
    with thoses two lines
    Code (csharp):
    1.  
    2. using System.Text.RegularExpressions;
    3.  
    4. private string onlyNumbers = "";
    5.  
    6. void OnGUI()
    7. {
    8. onlyNumbers= GUI.TextField(new Rect(30, 110, 30, 20), onlyNumbers, 3);
    9. onlyNumbers= Regex.Replace(onlyNumbers, "[^0-9]", "");
    10. }
    11.  
    thanx to Fishypants and Timmer :wink:
     
    dpw likes this.
  3. 53B45

    53B45

    Joined:
    Sep 26, 2013
    Posts:
    6
    Thanks Babator for sharing your solution.
     
  4. dpw

    dpw

    Joined:
    May 20, 2014
    Posts:
    6
    Very helpful, thanks!!!!
     
  5. RASKALOF

    RASKALOF

    Joined:
    Jul 9, 2012
    Posts:
    56
    You are hero, man! Thanx!