-1

i went through these , but they didn't help me in my situation

this doesn't work in my case because it it contains KeyData or KeyCode methods in answers which shows error when i try to run. How to add number in each new line in a multiline textbox C# / ASP.net

i tried this one which doesn't work in my case because it used when a button is pressed Adding new line of data to TextBox

this one just adds a a few strings like first, second , third and that is , which doesn't help in my case. https://www.codeproject.com/Questions/320713/Adding-new-line-in-textbox-without-repalcing-exist

I went through 20 search results in Google , many of them were not relevant while others weren't helpful in my situation.

Dyary
  • 753
  • 7
  • 14

2 Answers2

0

If I understand well the question, you want to add the line number of each new line inside the textbox. In order to do just this, you would need to add a TextBox in your view with the KeyDown event

<TextBox KeyDown="TextBox_KeyDown" x:Name="txtBox" />

Then, in the code-behind of the view, you can check for the key to be the "enter" key and add the line number to the text

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        string txt = txtBox.Text;
        int lineNumber = txt.Split('\n').Length + 1;
        txtBox.Text += '\n' + lineNumber.ToString();
        txtBox.CaretIndex = txtBox.Text.Length;
    }
}

Notes

  • The line txtBox.CaretIndex = txtBox.Text.Length; places the caret at the end of the text after editing it. Otherwise when you press enter, the caret is reset to the beginning of the text.
  • You would need to append the first line number when the textbox is empty.
  • You would probably need to handle the editing, i.e. so that the user cannot remove the line numbers for example.

Edit

If using the AcceptsReturn property of the TextBox, the event KeyDown is directly handled by the TextBox and note bubbled up to the parents. In this case, you have to use the PreviewKeyDown event, see MSDN Routing Strategies

computoms
  • 46
  • 1
  • 5
  • Thank you for the answer. for some reason the KeyDown event for the Enter key doesn't work . I tried the KeyUp but it gives me the wrong result. it will create an empty line and then a line number. For example when i first press enter with no text in the TextBox it gives 3 then on the next Enter it creates a space then a number 5 and so on. – Dyary Jun 12 '18 at 23:37
  • I edited the answer to include the explanation of the behavior you encountered. This is why you had extra lines when using other events with the `AcceptsReturn=true`, because I explicitely handled the new line in the `KeyDown` event as it wasn't handled by the TextBox. – computoms Jun 13 '18 at 07:21
0

I tried the answer by computhoms it didn't work exactly how i wanted to.

First the KeyDown event didn't generated any results , it seems the when pressing the Enter key the KeyDown event doesn't fire and doesn't generate any results. so instead i used PreviewKeyDown event which works.

Second it didn't Write the line number for the first line. Also for subsequent numbers it creates the number in a new line then move the cursor to another line so the effect would be like this:

some text

2

some text

3

some text

and so on.

the result i want is i want it to be like this :

1-some text.

2-some text.

3-some text.

so i modified the code given by Computhos which gave me the right results. here is the code i used:

After Creating a TextBox in xaml and creating the PreviewKeyDown event like this

  <TextBox  PreviewKeyDown="LifeGoalsTextBox_PreviewKeyDown" x:Name="LifeGoalsTextBox" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" />

I went to the code behind and wrote this code:

private void LifeGoalsTextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string txt = LifeGoalsTextBox.Text;
                int lineNumber = txt.Split('\n').Length ;
                string lineNumberString = lineNumber.ToString();

                if (lineNumber == 0)
                {
                    LifeGoalsTextBox.Text = LifeGoalsTextBox.Text.Insert(0, "1") + '-';
                }
                else
                {
                    lineNumber++;
                    int lastLine = 1 + LifeGoalsTextBox.Text.LastIndexOf('\n');
                    LifeGoalsTextBox.Text = LifeGoalsTextBox.Text.Insert(lastLine, lineNumberString+ '-') ;

                    LifeGoalsTextBox.CaretIndex = LifeGoalsTextBox.Text.Length;
                }
            }
        }
Dyary
  • 753
  • 7
  • 14