0
votes

I have two related problems, need your help to figure out.

in my program I write the any phrase in textBox1, then I hit the enter button on my keyboard and see this text in the textBox2.

but when I hit the enter to show my phrase in textBox2, cursor in textBox1 goes to the next line and creates line break

But after hit enter and cleaning, I want return cursor to the beginning of textBox1.

I tried it this way:

textBox1.Focus();
textBox1.Select(0, 0);

and this, but doesn't works for me:

textBox1.Select(textBox1.Text.Length, 0);

besides that I just want to return the cursor to the beginning, this line break violates the order in a text document, because I write this lines to the text document. line by line after each hit on enter.

For example, if with button use I have this order in result:

phrase1   
phrase2
phrase3
...

with Enter I got this:

phrase1 

phrase2

phrase3

I think that the solution of this problem can't solve following one, so as they are related, it would be good to solve this one too, because I have no idea, how to do it.

also I have to avoid the white-space which can be left at the end of the line, before I hit the enter. It is also violates the order in my text document. I don't want the phrase or word with white-space at the end:

phrase1< without white-space 
phrase2 < with unwanted white-space at the end 
...

here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XXX_TEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ActiveControl = textBox1;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox2.AppendText(textBox1.Text + "\n");
                textBox1.Text = "";
            }

            }
        }
}

EDIT:

Solution with String.TrimEnd() function and e.SuppressKeyPress = true;:

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace XXX_TEST
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                ActiveControl = textBox1;
            }

            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                     textBox2.AppendText(textBox1.Text.TrimEnd() + "\n");
                     textBox1.Text = "";
                     e.SuppressKeyPress = true;
                }

                }
            }
    }
1
Could you please rephrase your question? Currently I don't understand at all what you're trying to achieve. - Visual Vincent
@Visual Vincent hello, done, hope now better - user6711365
So you basically want to clear TextBox1 every time the user hits Enter? You say the caret doesn't go back to the beginning, which I don't understand since when the TextBox have no text in it the caret will be in the beginning. - Visual Vincent
Hold on, I think I see your problem. Let me write an answer... - Visual Vincent

1 Answers

0
votes

I think I see the reason for your first two problems. When the KeyDown event runs you check for the Enter key, and if it was pressed you do your actions and clear textBox1. However, you still have not stopped the Enter key message from being passed onto the TextBox.

The fix is to set the e.SuppressKeyPress property to true which will cause the key press to not be passed to the TextBox, thus it won't add another new line in it.

As for your third request about removing any trailing spaces you just have to utilize the String.TrimEnd() function.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        textBox2.AppendText(textBox1.Text.TrimEnd() + "\n"); //TrimEnd() removes trailing white-space characters.
        textBox1.Text = "";
        e.SuppressKeyPress = true; //Do not pass the Enter key press onto the TextBox.
    }
}