通过事件参数获取控制文本的事件 [英] Event to take control's text via event args

查看:42
本文介绍了通过事件参数获取控制文本的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在事件args中,为了获取 TextBox Text ,我应该订阅什么事件?

What is the event I should subscribe to in order to get TextBox's Text in the event args?

我已经尝试过 PreviewTextInput ,但是如果输入字符串是例如 122。框的文本(请参见代码)不带点,但eventArgs.Text为。并且输入字符串验证成功,TextBox.Text为 122 ..。我想做的是通过调用 decimal.TryParse 来验证输入字符串是否为十进制。

I've tried PreviewTextInput, but if input string is, for example, "122." the box's (see code) text is without the dot but eventArgs.Text is "." and the input string validating successfully and TextBox.Text is "122..". What I want to do is to validate if the input string is decimal by calling decimal.TryParse.

private void OnPreviewTextInput(object sender, TextCompositionEventArgs eventArgs)
{
    var box = sender as TextBox;
    if (box == null) return;

    eventArgs.Handled = !ValidationUtils.IsValid(box.Text + eventArgs.Text);
}


推荐答案

您可以尝试类似的方法:

You may try something like this:

private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    var textBox = (TextBox)sender;
    var text = textBox.Text.Insert(textBox.CaretIndex, e.Text);
    decimal number;
    if (!decimal.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out number))
    {
        e.Handled = true;
    }
}

这篇关于通过事件参数获取控制文本的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆