Trimming strings

I am looking for a way to deny the user to type certain symbols into a name, because later I want to use that name to create a default filename or folder.
 
A mask would be nice, but I haven't found one.

I can trim from both ends, but not from the middle, and also trimming moves the cursor to the beginning of the line.

After a zillion failures I came up with the idea below, but unfortunately the Undo method does not work as I expected and may even cause the application to crash.
Any ideas

Regards,

  Guido


private
void myTextBox_TextChanged(object sender, EventArgs e)

{if (myTextBox.Text.Contains("*")) myTextBox.Undo();}



Answer this question

Trimming strings

  • Delmer Johnson

    AFAIK MaskedTextBox only gives masks for the format of a string, not for the content.
    Your link does not really help me, since it's a different language and it assumes a lot of knowledge that I don't have.

  • TomHope

    Hi,

    you can look with regular expressions for illegal characters. You can search the string after the user put it in or is putting it in and you will get a objects containing informations about the found things in the string in a match collection. Then you can cycle through the match collection and react on the characters. Here is a linkt to the regex namespace:


    http://msdn.microsoft.com/library/default.asp url=/library/en-us/script56/html/reconintroductiontoregularexpressions.asp

    Or you can try MaskedTextBox class .net 2.0



  • THaines

    Got it! All that remained to do was to change the Deselect call into a select Call. Idea

    private
    void myTextBox_TextChanged(object sender, EventArgs e)

    {int x=0; string s1, s2;
    if (myTextBox.Text.Contains("*"))
    {
    // myTextBox.Undo(); // goes crazy
    x = myTextBox.Text.IndexOf("*");
    s1 = myTextBox.Text.Substring(0, x);
    s2 = myTextBox.Text.Substring(x + 1);
    myTextBox.Text = s1 + s2;
    myTextBox.Select(x,0);}}

    Regards,

      Guido


  • Sangeetha_AS

    Thanks Peter,

    That is a lot shorter! Big Smile

    Regards,

      Guido

  • bsoft16834

    This is the closest I can get, but it still places the cursor at the beginning of the text. Is the position of the cursor stored anywhere, so I can get and (re)set it

    private void myTextBox_TextChanged(object sender, EventArgs e)

    {int x=0; string s1, s2;
    if (myTextBox.Text.Contains("*"))
    {
    // myTextBox.Undo(); // goes crazy
    x = myTextBox.Text.IndexOf("*");
    s1 = myTextBox.Text.Substring(0, x);
    s2 = myTextBox.Text.Substring(x + 1);
    myTextBox.Text = s1 + s2;
    myTextBox.DeselectAll();}}


  • David Silverlight

    I would handle the KeyPress event and set KeyPressEventArgs.Handled to true for those characters I want "masked-out", for example:

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if(e.KeyChar == '*')
        {
            e.Handled =
    true;
        }
    }

     


  • Trimming strings