从表单外部更新 textInput

发布于 2024-09-01 06:55:59 字数 153 浏览 5 评论 0原文

我有一个带有多行 textInput 的表单。我需要从表单外部的对象更新 textInput 的内容。

我怎样才能实现这个目标?我应该使用事件,还是将 textInput 传递给外部对象的构造函数?

I have a form with a multiline textInput. I need to update the content of the textInput from an object outside the form.

How can I achieve this? Should I use events, or perhaps pass the textInput to the outside object's constructor?

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

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

发布评论

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

评论(3

凡尘雨 2024-09-08 06:55:59

看一下 MVP 模式 - 您可以让您的表单实现 IView 接口。您的另一个对象是演示者,例如,当某些内容发生更改时,它会调用 IView.UpdateText() (或者让您的视图订阅演示者事件 - 我更喜欢方法方法)。

这将您的关注点分开,并使您的解决方案更具可测试性,因为您可以模拟 IView、IPresenter 和 IModel 的实现。

表单应检查是否 this.InvokeRequired == true 以确定传入请求是否位于 UI 线程上。如果不是,您将需要使用委托。

public delegate void UpdateTextDelegate(string text);

public void UpdateText(string text)
{
  // Check we are on the right thread.
  if (!this.InvokeRequired)
  {
      // Update textbox here
  }
  else
  {
      UpdateTextDelegate updateText = new UpdateTextDelegate(this.UpdateText);

      // Executes a delegate on the thread that owns the control's underlying window handle.
      this.Invoke(updateText, new object[] { text });
  }

}

Have a look at the MVP pattern - you could have your form implement an IView interface. Your other object would be the Presenter that would call, for example, IView.UpdateText() when something changes (or have your view subscribe to the presenters events - i prefer the method approach).

This separates your concerns and makes your solution more testable as you can mock up implementations of IView, IPresenter and IModel.

The form should check if this.InvokeRequired == true to determine if the incoming request is on the UI thread. If it is not you will need to use a delegate.

public delegate void UpdateTextDelegate(string text);

public void UpdateText(string text)
{
  // Check we are on the right thread.
  if (!this.InvokeRequired)
  {
      // Update textbox here
  }
  else
  {
      UpdateTextDelegate updateText = new UpdateTextDelegate(this.UpdateText);

      // Executes a delegate on the thread that owns the control's underlying window handle.
      this.Invoke(updateText, new object[] { text });
  }

}

比忠 2024-09-08 06:55:59

我不会将文本传递给对象。如果您只需要它作为初始化值,则将文本传递给表单的构造函数就可以了。但反之亦然。

非常简单的解决方案:
为您的表单提供一个公共 SetTextValue(string Text) 方法,用于设置文本。

事件也可以工作,但对于这样一个简单的问题似乎有点过于强大。

i would not pass the text to the object. If you just need it as initialization value, passing the text to the constructor of the form would be fine. But not other way round.

Very simple solution:
Give your form a public SetTextValue(string Text) method, that sets the text.

Events will work also, but seems a bit overpowered for such a simple problem.

你不是我要的菜∠ 2024-09-08 06:55:59

有很多方法可以实现这一点,具体取决于您正在处理的具体情况。

从表单内更新文本字段?

txtField.Text = someObject.SomeProperty;

在表单的构造函数中设置值?

SomeFormClass form1 = new SomeFormClass(aString);
form1.Show();

从外部对象调用表单上的方法?

public void SetText(string text) { txtField.Text = text; }

form1.SetText(aString);

使用数据绑定?

txtField.DataBindings.Add(new Binding("Text", someObject, "SomeProperty");

在不了解更多细节的情况下很难回答。

There are a lot of ways to accomplish this depending on the specifics of what you're working on.

Update the text field from within the form?

txtField.Text = someObject.SomeProperty;

Set the value in the form's constructor?

SomeFormClass form1 = new SomeFormClass(aString);
form1.Show();

Call a method on the form from an external object?

public void SetText(string text) { txtField.Text = text; }

form1.SetText(aString);

Use data binding?

txtField.DataBindings.Add(new Binding("Text", someObject, "SomeProperty");

It's hard to answer without knowing more details.

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