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;
}
}
}
}
TextBox1every 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