-
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.
-
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
-
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 :)
-
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.