Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to enter data between 50 and 100 lines in a 5-character textbox. But I want it not to allow except 5 characters. How can I do this and how can I read the value entered in the textbox.

I want to textbox value:

12345
14256
56888
14579
45796

and I want to save it to the database as it is written one at a time.

What I have tried:

<TextBox x:Name="textBoxList"  HorizontalAlignment="Left" Height="150" Margin="60,207,0,0" TextWrapping="Wrap" Text="TextBox" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" VerticalAlignment="Top" Width="200"/>


This code is show

123451425656888..... // I can write.But I want to

12345
14256
56888

So I don't want to write after 5 characters.

For example:

Entered two value

12345
78457

two value save database table.

Person TABLE

PERSONID COLUMN IS


12345
78457

OR

Entered four value

12345
78457
45789
45785

two value save database table.

Person TABLE

PERSONID COLUMN IS

12345
78457
45789
45785


using (DataContext dt=new DataContext())
{
// when I say save the entered lines, how to take a line by line
}
Posted
Updated 2-Sep-19 6:26am
Comments
Richard MacCutchan 2-Sep-19 4:42am    
You need to capture the text changed event for the text box and add the newline after each 5 characters.

1 solution

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;

// set 'AcceptsTab, and 'MultiLine, to 'true

private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // measure current line
    currentLineNdx = textBox1.GetLineFromCharIndex(textBox1.SelectionStart);

    currentLineLength = (currentLineNdx == 0)
        ? textBox1.Text.Length
        : textBox1.Lines[currentLineNdx].Length;
    
    // carriage return ?
    if (e.KeyChar == '\r')
    {
        // now CR is okay
        if (currentLineLength == lineLengthLimit)
        {
            currentLineLength = 0;
            return;
        }

        e.Handled = true;
        return;
    }

    // backspace is always ok
    if (e.KeyChar == '\b') return;

    // at the limit for line: cancel
    if (currentLineLength == lineLengthLimit)
    {
        e.Handled = true;
        return;
    }
    
    // numbers always ok
    if (Char.IsDigit(e.KeyChar))
    {
        currentLineLength++;
        return;
    }

    // ignore non-numeric, punctuation, control chars, etc.
    e.Handled = true;
}
This is taken from my own code for a sub-classed WinForm TextBox: the logic/code should be valid in WPF.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900