PrimeGamer25
PrimeGamer25

Reputation: 33

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.

Upvotes: 0

Views: 249

Answers (2)

JohnyL
JohnyL

Reputation: 7142

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

Upvotes: 0

Tobias Tengler
Tobias Tengler

Reputation: 7454

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.

Upvotes: 2

Related Questions