简体   繁体   English

如何将一个角色更改为另一个角色

[英]how to change a Character to another Character

I Wanna Know how to change a Character that user type in an usual Textbox into another Character 我想知道如何将用户在普通文本框中键入的字符更改为另一个字符

for Example: 例如:

if user type " A " in the Textbox , I wanna the programme to type " B " 如果用户在文本框中输入“ A ”,我想输入“ B ”的程序

and if user type " N " in the Textbox , I wanna the programme to type " M " 如果用户在文本框中输入“ N ”,我想在程序中键入“ M

and if user type " Y " in the Textbox , I wanna the programme to type " S " 如果用户在文本框中输入“ Y ”,我想在程序中键入“ S

And so.. 所以..

so when user type " ANY " i need the text in the textbox to be " BMS " 因此,当用户键入“ ANY ”时,我需要文本框中的文本为“ BMS

ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

i have solved the problem from Custom TextBox Control that Switches Keyboard Language Automatically c# WPF 我已经解决了自定义文本框控件的问题,该控件可自动切换键盘语言c#WPF

You need to implement the textbox's OnTextChanged / TextChanged event (which depends on whether you are doing win forms or .net). 您需要实现文本框的OnTextChanged / TextChanged事件(这取决于您是否正在执行赢表或.net)。

You can access the current value of the text box using the textbox's Text value. 您可以使用文本框的“ Text值访问文本框的当前值。

You will need to save the old value to compare against so you can keep track of which characters you have already changed. 您将需要保存旧值以进行比较,以便可以跟踪已更改的字符。

If you are using Visual Studio, make a textbox and double click on it. 如果您使用的是Visual Studio,请创建一个文本框并双击它。 Then a method shows up, and you insert the following code: 然后出现一个方法,并插入以下代码:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == "ANY")
            textBox1.Text = "BMS";
    }

The method checks if your textbox contains the string "ANY" and changes it to "BMS". 该方法检查您的文本框是否包含字符串“ ANY”,并将其更改为“ BMS”。 You can also alter this method so it changes the letters for every single letter you write. 您还可以更改此方法,以便它为您写的每个单个字母更改字母。 For instance: 例如:

A -> B
BN -> BM
BMY -> BMS

This will give the feeling that the word changes while it is being written instead of when the entire word has been written. 这会给人一种感觉,单词在写入时会发生变化,而不是整个单词都已写入。

First create a dictionary that contains information about what to change with what like this 首先创建一个字典,其中包含有关如何更改内容的信息

Dictionary<string, string> myDict = new Dictionary<string, string>
{
    {"A", "B"},
    {"N", "M"},
    {"Y", "S"},            
};

You can simply do that by overriding the OnTextInput method in your own custom class. 您可以通过在您自己的自定义类中重写OnTextInput方法来简单地做到这一点。 So your complete code for your own custom class will be 因此,您自己的自定义类的完整代码将是

namespace WpfApplication
{
    public class MyTextBox : TextBox
    {
        Dictionary<string, string> myDict = new Dictionary<string, string>
        {
            {"A", "B"},
            {"N", "M"},
            {"Y", "S"},            
        };

        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            string str;
            if (myDict.TryGetValue(e.Text, out str))
            {
                e.Handled = true;
                if (SelectionLength == 0)
                    Text = Text.Insert(CaretIndex, str)
                else
                {
                    SelectedText = str;
                    SelectionLength = 0;
                }

                CaretIndex += Text.Length;
            }

            base.OnTextInput(e);
        }
    }
}

Try this. 尝试这个。 It may solve your problem. 它可以解决您的问题。

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string finalValue;
        if (!String.IsNullOrEmpty(textBox1.Text))
        {

            byte[] ascii = Encoding.ASCII.GetBytes(textBox1.Text);

            List<byte> newB = new List<byte>();
            foreach (byte b in ascii)
            {
                byte s = b;
                s++;
                newB.Add(s);
            }

            finalValue = Encoding.ASCII.GetString(newB.ToArray());
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM