0

I am trying to do a function that is a textBox contains 2 dots (.) then the last dot(.) will be deleted, so it wont be possible to write 2 dots(.) in this textbox.

When I write the second dot in this textBox, then also the first is deleted, for example if I write this string:

"0.." -> here I want the last dot to be deleted but the first dot is also deleted. I cant figure out what I am doing wrong:

private void Value2_TextChanged(Object^ sender, TextChangedEventArgs^ e)
        {
            String^ GetValue2 = InputValue2->Text->Trim();
            int DotCount = 0;

            for( int i = 0; i < GetValue2->Length; i++ )
            {
                if( GetValue2->Substring(i, 1) == "." )
                {
                    DotCount = DotCount + 1;
                }
                if( DotCount == 2 )
                {
                    GetValue2 = GetValue2->Replace(GetValue2->Substring(i, 1), "");
                    Value2->Text = GetValue2;
                    Value2->SelectionStart = GetValue2->Length;
                    break;
                }
            }
}

Edited by Lukezzz: n/a

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by jonsca
0

Instead of simply counting the periods (dots) in the first if statement, why don't you store the location of the second one and remove it after the for loop exits? Your TextChanged() event will be firing each time you type anyway...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.