0

我有一个文本框,我试图通过两种方式进行限制:

1 - 我只想允许数值,没有小数

2 - 我只想接受 <= 35 的数字

我有以下事件来处理这个:

private void TextBoxWorkflowCountPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!IsNumeric(e.Text, NumberStyles.Integer)) e.Handled = true;
}

public bool IsNumeric(string val, NumberStyles numberStyle)
{
    double result;
    return double.TryParse(val, numberStyle, CultureInfo.CurrentCulture, out result);
}

private void TextBoxWorkflowCountTextChanged(object sender, TextChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(textBoxWorkflowCount.Text) && Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true;
    else
    {
        MessageBox.Show("Must not be higher then 35");
        textBoxWorkflowCount.Text = "35";
    }
}

这在表面上工作得很好 -除非用户将数据粘贴到文本框(似乎不可避免)或更奇怪的是 - 如果用户输入一个数字然后点击退格键(使文本框再次空白)消息框让用户知道它们的值 > 35 出现(即使绝对不是这种情况)。如果必须,我可以忍受的第一个问题 - 但第二个问题是游戏中断,在尝试解决它 30 分钟后,我无处可去。帮助!

4

2 回答 2

1

您的代码不符合第一个条件,因为

string.IsNullOrEmpty(textBoxWorkflowCount.Text) 

评估为真,因此它落入“其他”,并显示消息。

if (string.IsNullOrEmpty(textBoxWorkflowCount.Text) || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true; 

应该做的伎俩

于 2012-08-11T16:15:34.150 回答
1

几个月前,我写了一篇关于 TextBox 行为(Windows Phone 7.5 平台)的博客文章,其中之一是 TextBoxInputRegexFilterBehavior,它允许您过滤输入文本。因此,如果您熟悉 Behaviors 的工作原理,您可以使用此代码

using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

/// <summary>
/// UI behavior for <see cref="TextBox"/> to filter input text with special RegularExpression.
/// </summary>
public class TextBoxInputRegexFilterBehavior : Behavior<TextBox>
{
    private Regex regex;

    private string originalText;
    private int originalSelectionStart;
    private int originalSelectionLength;

    /// <summary>
    /// Gets or sets RegularExpression.
    /// </summary>
    public string RegularExpression 
    {
        get
        {
            return this.regex.ToString();
        } 

        set 
        {
            if (string.IsNullOrEmpty(value))
            {
                this.regex = null;
            }
            else
            {
                this.regex = new Regex(value);
            }
        } 
    }

    /// <inheritdoc/>
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.TextInputStart += this.AssociatedObjectTextInputStart;
        this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged;
    }

    /// <inheritdoc/>
    protected override void OnDetaching()
    {
        base.OnDetaching();

        this.AssociatedObject.TextInputStart -= this.AssociatedObjectTextInputStart;
        this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged;
    }

    private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs e)
    {
        if (this.originalText != null)
        {
            string text = this.originalText;
            this.originalText = null;
            this.AssociatedObject.Text = text;
            this.AssociatedObject.Select(this.originalSelectionStart, this.originalSelectionLength);
        }
    }

    private void AssociatedObjectTextInputStart(object sender, TextCompositionEventArgs e)
    {
        if (this.regex != null && e.Text != null && !(e.Text.Length == 1 && char.IsControl(e.Text[0])))
        {
            if (!this.regex.IsMatch(e.Text))
            {
                this.originalText = this.AssociatedObject.Text;
                this.originalSelectionStart = this.AssociatedObject.SelectionStart;
                this.originalSelectionLength = this.AssociatedObject.SelectionLength;
            }
        }
    }
}

因此,通过这种行为,您可以编写简单的正则表达式来过滤用户输入,例如 RegularExpression="(3[0-5])|([0-2]?[0-9])"

于 2012-08-11T18:42:31.763 回答