使用从主窗体启动的子窗体上的变量更新主窗体上的文本框 [英] Updating textbox on mainform with a variable on a child form that is launched from main form

查看:30
本文介绍了使用从主窗体启动的子窗体上的变量更新主窗体上的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体应用程序.主窗体有一个文本框和一个用于启动另一个窗体的按钮.当另一个表单启动时,两个表单都在屏幕上(但启动的表单是模态的).子窗体有一个文本框和按钮,当按下按钮时,我希望主窗体(父窗体)上的文本框更新为子窗体上文本框中的值.

I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox on the main form (the parent) to be updated with the value in the textbox on the child form.

这个功能是如何实现的?

How is this functionality achieved?

推荐答案

理想情况下,您希望避免两种形式相互依赖,这可以通过接口来实现:

Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:

public interface IMainView
{
  public void UpdateValue(string val);
}

public interface IChildView
{
  public void Show(IMainView parent);
}

让主窗体实现 IMainView,子窗体实现 IChildView,父窗体调用 child.show(this),子窗体调用 parent.UpdateValue(blah);

have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);

希望这会有所帮助.

这篇关于使用从主窗体启动的子窗体上的变量更新主窗体上的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆