[英]How do I prepend text from one textbox to another?
Basically I'm making a simple program to help take notes at my job. 基本上,我正在编写一个简单的程序来帮助我做笔记。 I have a one line
textbox1
, and a multiple line textbox2
. 我有一个一行
textbox1
和一个多行textbox2
。
I want to be able to type whatever in textbox1
, and then press "enter" and it show up in the first line in textbox2
. 我希望能够在
textbox1
键入任何textbox1
,然后按“ enter”,它会显示在textbox2
的第一行中。 Any help would be appreciated. 任何帮助,将不胜感激。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
//in form constructor, or InitializeComponent method
textBox1.Validated += DoValidateTextBox;
//in another place of your class
private void DoValidateTextBox(object sender, EvenArgs e) {
textBox2.Text = ((TextBox)sender).Text + Environment.NewLine + textBox2.Text;
}
This should work: 这应该工作:
private void textBox1_KeyDown(object sender, KeyEventArgs e) // Keydown event in Textbox1
{
if (e.KeyCode == Keys.Enter) // Add text to TextBox2 on press Enter
{
textBox2.Text += textBox1.Text;
textBox2.Text+= "\r\n"; // Add newline
textBox1.Text = string.Empty; // Empty Textbox1
textBox1.Focus(); // Set focus on Textbox1
}
}
If you want to add text at the firstline of your textbox, then replace in the code above: 如果要在文本框的第一行添加文本,请替换上面的代码:
textBox2.Text = textBox1.Text + "\r\n" + textBox2.Text;
It depends on what you want the final result to be. 这取决于您希望最终结果是什么。 If all you want is the first line of the second textbox to equal the first then:
如果您想要的只是第二个文本框的第一行等于第一行,则:
void myEvent()
{
textbox2.Text = textbox1.Text;
}
If however you want whatever is in textbox1 to be appended to textbox2 every time you press a button, then you are better off using a ListView: 但是,如果您希望每次按下按钮时将textbox1中的内容追加到textbox2中,那么最好使用ListView:
void myEvent()
{
myListView.Items.add(textbox1.Text);
}
If you specifically want a textbox though (with the data always appended to the first line): 但是,如果您特别想要一个文本框(数据始终附加到第一行):
void myEvent()
{
textbox2.Text = textbox1.Text + Environment.NewLine + textbox2.Text;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.