繁体   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