Home > DeveloperSection > Forums > Multiline issue WPF TextBox
john rob

Total Post:108

Points:756
Posted on    September-03-2013 7:16 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1255  View(s)
Rate this:

I creating multiline TextBox with this Link its work better but if I want to set TextBox text counter

label1.Content = textBox1.Text.Length;

with above line work fine but problem is that when I press enter in the TextBox counter it will increase 2 characters in TextBox counter.

How can I do this task please help me.

Any help appreciated!



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-03-2013 7:48 AM

Hi John,

If you need just one character on "Enter" then you can just handle PreviewKeyDown event on TextBox and paste following handler:

    private void Txt_OnPreviewKeyDown(object sender, KeyEventArgs e)

    {

        if (e.Key == Key.Enter)

        {

            var txtBox = e.Source as TextBox;

            var selectionStart = txtBox.SelectionStart;

            txtBox.Text = txtBox.Text.Insert(selectionStart, "\n");

            txtBox.Select(selectionStart + 1, 0);

            e.Handled = true; 

        }

    }


Don't want to miss updates? Please click the below button!

Follow MindStick