简体   繁体   English

如何区分用户输入,但允许应用程序本身更改文本

[英]How to distinguish between user input, but allow the application itself to change text

I've got a textbox, which does not allow users to enter "[" and "]" characters. 我有一个文本框,不允许用户输入“ [”和“]”字符。 However, I would like to set the text to be a value containing both "[" and "]" by default. 但是,我想将文本默认设置为同时包含“ [”和“]”的值。 How can I go about this? 我该怎么办? This is my code: 这是我的代码:

TextBox txtEdit = new TextBox();
txtEdit.TextChanged += txtEdit_TextChanged;
txtEdit.Text = "[Test]";

private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
    if ((sender as TextBox).Text.Contains("[") || (sender as TextBox).Text.Contains("]"))
    {
        MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
        (sender as TextBox).Text = "";
    }
}

Put the TextChanged Event after you change the Text property 更改Text属性后放置TextChanged事件

TextBox txtEdit = new TextBox();
txtEdit.TextChanged += txtEdit_TextChanged;

private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
    if (comboBoxYou.Text == txtEdit.Text) 
    {
        return;
    }
    else if ((sender as TextBox).Text.Contains("[") || (sender as TextBox).Text.Contains("]"))
    {
        MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
        (sender as TextBox).Text = "";
    }
}

I'd set a keydown event for the Textbox. 我将为文本框设置一个keydown事件。

If you don't want users enetering the '[' and ']' characters, then read in the key character and set e.Handled = true if it matches the character you don't want. 如果您不希望用户输入'['和']'字符,请读入关键字符并设置e.Handled = true如果它与您不需要的字符匹配)。

The answer of Toon Casteele is a good solution. Toon Casteele的答案是一个很好的解决方案。 Otherwise try adding a boolean to your form and when the user selects a value set it to true and check for the value in the TextChanged event if it is true. 否则,请尝试向表单中添加一个布尔值,当用户选择一个值时,将其设置为true,然后检查TextChanged事件中的值是否为true。

private bool IsSelectedItemText; //set it to true when an item is selected

And then do following check: 然后执行以下检查:

if(!IsSelectedItemText)
    //Check for square brackets
else
    //Add text from item

Also, and this is not directly related to the question but I strongly suggest that you cast the 'sender' object only once. 另外,这与问题没有直接关系,但我强烈建议您仅将“ sender”对象强制转换一次。 Complete, the code would look like: 完成后,代码如下所示:

private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox tb = sender as TextBox;

    if(!IsSelectedItemText)
    {            
        if (tb.Text.Contains("[") || tb.Text.Contains("]"))
        {
            MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
            tb.Text = "";
        }
    }
    else
    {
        //Set text from selected item
    }

    IsSelectedItemText = false;
}

Remember to set the boolean-variable to 'true' when an item is selected. 请记住,选择一个项目时,将布尔变量设置为“ true”。

If thats what you wanted. 如果那是您想要的。

private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
{
string a = (sender as TextBox).Text.substing(0,1).subsctring;
a = a.Remove(0, 1);
            a = a.Remove(a.Length - 1, 1);
            if (a.Contains('[') || a.Contains(']') )
            {
                MessageBox.Show("Cannot enter '[' or ']' characters!", "", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            (sender as TextBox).Text = "[" + a.Replace("[", "").Replace("]", "") + "]";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 允许用户只输入文字? - Allow user to only input text? 区分用户生成的和我自己的应用程序生成的鼠标移动 - Distinguish between user generated and my own application generated mouse moves 如何允许Windows商店应用程序的用户更改界面 - How to allow user change interface of Windows store application C#中如何区分多个输入设备 - How to distinguish between multiple input devices in C# 如何在UWP应用程序中区分System.UnauthorizedAccessException的不同“类型”? - How to distinguish between different “types” of System.UnauthorizedAccessException in UWP application? 如何区分用户点击链接和执行自动重定向的页面? - How to distinguish between a user clicking a link and the page doing an automatic redirect? 如何区分用户控件在窗体上加载和运行时加载 - How to distinguish between User Control load on form and load when runtime 如何区分依赖属性的变化是从类内部还是外部 - How to distinguish between Dependency Property change from inside the class, or outside 如何在 C# 事件中区分是代码更改还是用户更改? - How to distinguish in a C# event if a change was made from code or by the user? 区分由用户输入Vs触发的事件。 UWP背后的代码 - Distinguish Between Events Triggered by User Input Vs. Code Behind UWP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM