This CP project might be useful for you: "HandlEdInput - Powerful and Highly Customizable Input Handler for TextBox, RichTextBox and ComboBox" [
^]
Quote:
I want to save it to the database as it is written one at a time.
One "what" at a time ? This is not a good idea unless you want slow performance: write to the database when you have accumulated data
and validated it.
For controlling entry into a TextBox in the way you describe:
private int currentLineLength = 0;
private int lineLengthLimit = 5;
private int currentLineNdx;
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
currentLineNdx = textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
currentLineLength = (currentLineNdx == 0)
? textBox1.Text.Length
: textBox1.Lines[currentLineNdx].Length;
if (e.KeyChar == '\r')
{
if (currentLineLength == lineLengthLimit)
{
currentLineLength = 0;
return;
}
e.Handled = true;
return;
}
if (e.KeyChar == '\b') return;
if (currentLineLength == lineLengthLimit)
{
e.Handled = true;
return;
}
if (Char.IsDigit(e.KeyChar))
{
currentLineLength++;
return;
}
e.Handled = true;
}
This is taken from my own code for a sub-classed WinForm TextBox: the logic/code should be valid in WPF.