简体   繁体   中英

changing specific chars in textbox

i am a noob in here so i might have some mistakes while asking, i apologize from now. i have a project, in this project i have 3 textboxes. and i need to have the textBox1.Text converted into a int value that i set.

 private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string a = "a";
        string b = "b";
        if (textBox1.Text.Contains(a))
        {
            textBox3.Text = a.Replace("a", "1");
        }
        if (textBox1.Text.Contains(b))
        {
            textBox3.Text = b.Replace("b","2");
        }

    }

but it just converts it if there is only a or b, not when you enter like bab ab aa bb cab etc.

i need it to convert all a or b chars.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text.Contains("a"))
        textBox3.Text = textBox3.Text.Replace("a", "1");

    if (textBox1.Text.Contains("b"))
        textBox3.Text = textBox3.Text.Replace("b", "2");
}

What you need to do is call the Replace() method on the actual content of the TextBox ( textBox3.Text ) and then store the replaced Text back into the TextBox.

string input = @"ababa baba aabb";
string pattern = @"a|b";
string x = Regex.Replace(input, pattern, m => m.Value == "a" ? "1" : m.Value == "b" ? "2" : m.Value);
// Output: 12121 2121 1122

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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