从另一个表单的数据设置主表单数据 [英] Setting mainform data from another form's data

查看:86
本文介绍了从另一个表单的数据设置主表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要形式和另一个独立形式.我想通过单击其他窗体上的按钮在主窗体上将picturebox.visible值设置为true.

I have a main form and another independent form. I want to set the picturebox.visible value to true on my mainform by clicking a button on the other form. How can I do this?

推荐答案

一种实现方法是在一种形式上启用属性,然后从另一种形式调用它.
One way to do it would be enable a property on one form and then call it from the other.


假设"mainform"实例化为"otherform",那么您应该引发一个事件.

此提示中有一个示例. [ ^ ].它使用字符串值,但是您可以轻松地对其进行更改以传递bool来设置PictureBoxVisible属性.

好-为什么1票呢?遵循OOP最佳实践的准确信息,以及指向示例代码的链接,以及以下有关以前给出的建议为何效果不佳的解释.我还能做些什么来帮助OP? [edit]
Assuming that ''mainform'' instanciates the ''otherform'' then you should raise an event.

There is an example in this tip.[^]. It uses a string value but you can easily alter it to pass a bool to set the Visible property of the PictureBox.

[edit] OK - so why the 1 vote? Accurate information that follows OOP best practices along with a link to example code, along with an explanation below on why the advice given previously was not great. What more could I do to help the OP? [edit]


不确定您如何创建表单,但是假设首先创建主表单,然后从主表单创建独立表单.因此,在您独立的窗体声明中,传递主窗体,以便您可以访问其属性

Not sure how you''re creating the forms, but let''s say that the main form is created first and the independent form is created from the main form. So, in your independent form declaration, pass in the main form so that you can access it''s properties

IndependentForm form2 = New IndependentForm(this);
form2.Show();



IndependentForm看起来像



The IndependentForm would look like

public partial class IndependentForm : Form
{
  MainForm _parent;
  Boolean _pictureBoxVisble;

  public IndependentForm(MainForm parent)
  {
    _parent = parent;
    _pictureBoxVisible = false;
  }

  public void Button1_Click(object sender, EventArgs e)
  {
    _pictureBoxVisble = !_pictureBoxVisible;
    _parent.SetPictureBoxVisible(_pictureBoxVisible);
  }
}



然后,当您单击独立表单上的按钮时,便具有对初始表单的引用,并且可以在主表单上调用属性或函数.



Then, when you click the button on the independent form, you have the reference to the initial form and you can call a property or function on the main form.

mainForm.SetPictureBoxVisible(TrueOrFalseValue);


这篇关于从另一个表单的数据设置主表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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