简体   繁体   English

如何将文本从一个文本框添加到另一个文本框?

[英]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.

相关问题 从一个视图切换到另一个视图时,如何保持用户控件的 Textbox Text 属性的值? - How do I persist the value of the Textbox Text property of a user control when switching from one view to another? 如何从文本框中转义文本? - how do i escape text from a textbox? 如何将文本从一个表单中的 DataGridView 传输到另一个表单中的私有 TextBox? - How can I transfer text from a DataGridView in one Form to a private TextBox in another Form? C#问题:如何在一种表单上处理另一种表单上的文本框? - C# issue: How do I manipulate a Textbox on one form from another form? C#WinForms - 如何通过另一种形式从一个表单上的文本框中检索数据? - C# WinForms - How Do i Retrieve Data From A Textbox On One Form Via Another Form? 使用JavaScript从一个文本框到另一个文本框的文本 - Text from one textbox to another using JavaScript C#:如何在字符串中的每一行前加上文本? - C#: How do I prepend text to each line in a string? 如何从textBox将文本写入文本文件,然后从文件读回textBox? - How do i write from textBox the text to text file and read back from the file to the textBox? 我如何预先准备一个流? - How do I prepend a Stream? 如何从<entry />并使用 Xamarin 从一页到另一页显示此文本? - How do I transferred text from <Entry/> and display this text from one page to another using Xamarin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM