Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Don';在文本框中不允许使用067或045之类的数字。Windows Phone应用程序_C#_Windows Phone 7_Textbox_Numbers - Fatal编程技术网

C# Don';在文本框中不允许使用067或045之类的数字。Windows Phone应用程序

C# Don';在文本框中不允许使用067或045之类的数字。Windows Phone应用程序,c#,windows-phone-7,textbox,numbers,C#,Windows Phone 7,Textbox,Numbers,我有个问题。我希望我的文本框不接受不存在的数字。我已经做了修改,所以我的文本框不接受十进制。但当我输入例如0865时,我希望它立即转换为865。我不知道怎么做。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Doc

我有个问题。我希望我的文本框不接受不存在的数字。我已经做了修改,所以我的文本框不接受十进制。但当我输入例如0865时,我希望它立即转换为865。我不知道怎么做。这是我的密码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;

namespace KeepThemTogether
{
    public partial class MainPage : PhoneApplicationPage
    {
        int from_a, to_b, generatedNumber;

        public MainPage()
        {
            InitializeComponent();
        }

        private void from_text(object sender, TextChangedEventArgs e)
        {

        }

        private void to_change(object sender, TextChangedEventArgs e)
        {

        }

        private void ShowGen()
        {
            ShGenTxt.Text = Convert.ToString(generatedNumber);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            from_a = Int32.Parse(FromTxt.Text);
            to_b = Int32.Parse(ToTxt.Text);        
            Random generate = new Random();
            generatedNumber = generate.Next(from_a, to_b);
            ShowGen();
        }

        private void from_up(object sender, KeyEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text.Contains('.'))
            {
                txt.Text = txt.Text.Replace(".", "");
                txt.SelectionStart = txt.Text.Length;
            }
        }

        private void to_up(object sender, KeyEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text.Contains('.'))
            {
                txt.Text = txt.Text.Replace(".", "");
                txt.SelectionStart = txt.Text.Length;
            }
        }
    }
}

我会使用regex并使用方法_TextChanged对输入运行它

private void textBox1_TextChanged(object sender, EventArgs e)
 {
     if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", textBox1.Text))
     {
         MessageBox.Show("Please enter only numbers.");
         textBox1.Text.Remove(textBox1.Text.Length - 1);
     }
 }

如果您只想删除前导零,可以使用它来处理TextChanged事件

private void RemoveLeadingZeros(object sender, EventArgs e)
{
    TextBox txt = (TextBox)sender;
    txt.Text = txt.Text.TrimStart('0');
}

此链接:显示如何仅创建数字。也许你可以适应限制数量。哇,这真是出乎意料的容易。非常感谢你的帮助。