簡體   English   中英

同時輸入兩個文本框

[英]Type in two text boxes simulatenously

我在jQuery中遇到過類似的問題(在這里找到),我試圖根據我在C#中的需求對其進行修改,但我沒有設法做到這一點。 我想要的是標題所說的內容,我希望用戶在一個文本框中鍵入一些文本,並將結果同時顯示在另一個文本框中。 這是我的看法(很明顯,它不起作用),我試圖將第一個文本框的KeyUp事件的參數傳遞給第二個文本框的相應事件,但看起來並不那么容易:

public mainFrm()
{
    InitializeComponent();

    this.txtFormat.KeyUp += new KeyEventHandler(txtFormat_KeyUp);
    this.txtNewFormat.KeyUp += new KeyEventHandler(txtNewFormat_KeyUp);
}

private void txtFormat_KeyUp(object sender, KeyEventArgs e)
{
    txtNewFormat_KeyUp(sender, e);
}

private void txtNewFormat_KeyUp(object sender, KeyEventArgs e)
{
}

感謝您的幫助。

最簡單的方法是只聽一個TextBoxTextChanged事件並將當前狀態轉發給另一個TextBox

public mainFrm()
{
    InitializeComponent();

    txtFormat.TextChanged += OnTextChanged;
}

private void OnTextChanged(object sender, EventArgs e)
{
    txtNewFormat.Text = txtFormat.Text;
}

這將使兩個文本框保持同步:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        textBox1.TextChanged+=new EventHandler(textBox_TextChanged);
        textBox2.TextChanged+=new EventHandler(textBox_TextChanged);            
    }

    void textBox_TextChanged(object sender, EventArgs e)
    {
        string text=(sender as TextBox).Text;
        if(!textBox1.Text.Equals(text)) { textBox1.Text=text; }
        if(!textBox2.Text.Equals(text)) { textBox2.Text=text; }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM