CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    11

    multiLine textBox

    Hello,

    is it possible to controll content of textEdit controll input, I mean I want user to be able only to type in 10 digit long numbers and then new line

    make it possible only to accept such text, for example:

    1111111111
    1212121212
    1234567890

    and not accept:
    11 11 11 11 11
    1234567890123
    12as34sd21

    I hope that im clear.

    thank for any hint


    User should be also able to type in
    1111111111

    then change controll and afet commin back make it like
    1111111111
    1234567890


    for example ofcourse.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: multiLine textBox

    Just an idea that might help...

    You could perhaps split the contents of the textbox at each newline ( ie. Enter ), then check each split element's character length, and based on that, accept or not.
    Just a couple of String Manipulations functions

  3. #3
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: multiLine textBox

    I couldn't resist playing around with this. I know it might be homework, but here goes:

    Code:
            bool go = true;
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text.Length > 10 && go)
                {
                    string oldtext = textBox1.Text;
                    string newtext = "";
                    string leftover = "";
                    string[] split = oldtext.Split(Environment.NewLine.ToCharArray());
                    foreach (string substring in split)
                    {
                        string temp = leftover + substring;
                        if (temp.Length > 10)
                        {
                            newtext += temp.Substring(0, 10) + Environment.NewLine;
                            leftover = temp.Substring(10);
                        }
                        else if (temp.Length == 10)
                            newtext += temp + Environment.NewLine;
                        else
                            newtext += temp;
                    }
                    newtext += leftover;
    
                    if (newtext != "")
                    {
                        go = false;
                        textBox1.Clear();
                        textBox1.AppendText(newtext);
                        go = true;
                    }
                }
            }
    If someone has a more elegant solution, I'd like to know it
    It's not a bug, it's a feature!

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: multiLine textBox

    Code:
            void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {      
                int c = 0;
                try {
                    c = textBox1.Lines[textBox1.GetLineFromCharIndex(textBox1.SelectionStart)].Length;
                }
                catch { }
    
                string reg = c < 10 ? "[0-9]|\r|\n|\b" : "\r|\n|\b";
                Regex regex = new Regex(reg);
                e.Handled = !(regex.IsMatch(e.KeyChar.ToString()));            
            }
    I am not very good with regular expressions, so someone who is could probably make this more concise.
    Last edited by BigEd781; November 1st, 2008 at 02:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured