|
-
October 30th, 2008, 02:17 PM
#1
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.
-
October 31st, 2008, 02:16 AM
#2
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
-
October 31st, 2008, 03:15 AM
#3
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!
-
November 1st, 2008, 02:41 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|