不使用输入控件更新文本框

发布于 2024-12-10 07:46:02 字数 309 浏览 0 评论 0原文

我有一个 Windows 窗体应用程序,它由一堆控件组成,更具体地说,是两个 textBox。其中之一是只读的。只读的 textBox 值应该与用户可以输入的 textBox 相同。

因此,如果用户在 textBox A 中键入“Hello World”,则 textBox B 中的值应自动更新为“Hello World”。

我该怎么做呢?我知道我只需要设置文本值,我只是不确定将代码放在哪里来自动完成,而不是在单击按钮或类似的操作时执行。

I have a windows form application which consists of a bunch of controls, but more specifically, two textBoxes. One of them is read only. The read only textBox value is supposed to be the same as the textBox that the user can type into.

So if the user types "Hello World" into textBox A, the value in textBox B should be automatically updated to "Hello World".

How do I go about doing this? I know I just need to set the text values, I'm just not sure where I place the code to get it done automatically rather than executed when a button is click or something along those lines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕头说它不想醒 2024-12-17 07:46:02

文本更改事件:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Text = textBox1.Text;
        }

TextChanged event:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Text = textBox1.Text;
        }
A君 2024-12-17 07:46:02

听起来您想要类似的东西:

writableTextBox.TextChanged += delegate {
    readonlyTextBox.Text = writableTextBox.Text;
};

换句话说,每当一个文本框中的文本发生更改时,请更新另一个文本框中的文本。这使用 Control.TextChanged事件。

It sounds like you want something like:

writableTextBox.TextChanged += delegate {
    readonlyTextBox.Text = writableTextBox.Text;
};

In other words, whenever the text in one textbox changes, update the other. This uses the Control.TextChanged event.

半世晨晓 2024-12-17 07:46:02

如果您希望在 textBoxA 的文本更改后(即用户在 textBoxA 中按下某个键后立即)更新 textBoxB,则事件为 TextChanged:

    this.textBoxA.TextChanged += new System.EventHandler(this.textBoxA_TextChanged);

    private void textBoxA_TextChanged(object sender, EventArgs e)
    {
        textBoxB.Text = textBoxA.Text;
    }

如果您希望仅在用户完成编辑后更新 textBoxB 中的文本textBoxA,您应该使用离开事件:

    this.textBoxA.Leave += new System.EventHandler(this.textBoxA_Leave);

    private void textBoxA_Leave(object sender, EventArgs e)
    {
        textBoxB.Text = textBoxA.Text;
    }

If you want textBoxB to be updated as soon as the text of textBoxA is changed (i.e immediately after the user press a key in textBoxA) the event is TextChanged:

    this.textBoxA.TextChanged += new System.EventHandler(this.textBoxA_TextChanged);

    private void textBoxA_TextChanged(object sender, EventArgs e)
    {
        textBoxB.Text = textBoxA.Text;
    }

If you prefer to update the text in textBoxB only after the user has finished to edit textBoxA, you should use the Leave event:

    this.textBoxA.Leave += new System.EventHandler(this.textBoxA_Leave);

    private void textBoxA_Leave(object sender, EventArgs e)
    {
        textBoxB.Text = textBoxA.Text;
    }
时间海 2024-12-17 07:46:02

这应该可以满足您的需要:

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}

This should do what you need:

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
凝望流年 2024-12-17 07:46:02

比事件方法更短(更好?)的是使用 winform 的数据绑定。只需在 InitializeComponents 调用之后立即使用它即可:

readonlyTextBox.DataBindings.Add("Text", writableTextBox, "Text");

Even shorter (better?) than the event approach is using winform's databinding. Just use this right after the InitializeComponents call:

readonlyTextBox.DataBindings.Add("Text", writableTextBox, "Text");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文