从Form2中的值更新Form1中的变量 [英] Updating variable in Form1 from value in Form2

查看:95
本文介绍了从Form2中的值更新Form1中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表单,名字是HomeForm,第二个名字是cfgForm



我有变量,在HomeForm中命名strCNF,在cfgForm中从变量CNF得到strCNF的值。



这里来自HomeForm的代码:



  private   void  cnfRulesMenu_Click( object  sender,EventArgs e)
{
尝试
{
cfgForm cfgForm = cfgForm ( this );
cfgForm.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),< span class =code-string> 错误);
}
}

// 其他部分....
public cfgForm cfg;
string strCNF = ;
strCNF =((cfgForm) this .cfg).CNF;





然后,在另一种形式是cfgForm,这里代码:



  public   string  CNF =   S  - > NP VP + System.Environment.NewLine + 
NP - > DT NN | QT NN | NP PP | NP RC | n | p + System.Environment.NewLine +
DT - > d + System.Environment.NewLine +
NN - > JJ NN | n + System.Environment.NewLine +
JJ - > JJ JJ | j + System.Environment.NewLi ne +
QT - > q + System.Environment.NewLine +
PP - > PT NP | GG PP + System.Environment.NewLine +
PT - >我 + System.Environment.NewLine +
GG - > g + System.Environment.NewLine +
RC - > WH NP | WH VP + System.Environment.NewLine +
WH - > w + System.Environment.NewLine;

public cfgForm(HomeForm homeForm)
{
// TODO:完成成员初始化
InitializeComponent();
.homeForm = homeForm;
}

private void btnDefaultCFG_Click( object sender,EventArgs e)
{
tbCNF.Text = CNF;
}

private void cfgForm_Load( object sender,EventArgs e)
{
tbCNF.Text = CNF;
btnApplyCNF.Enabled = false ;
}

private void btnApplyCNF_Click( object sender,EventArgs e)
{
// CNF =;
CNF = tbCNF.Text;
HomeForm homeForm = new HomeForm();
homeForm.cfg = ;
// homeForm.ShowDialog();
this .Hide(); // 不起作用
}





到目前为止,这些代码运行良好,如果我使用homeForm.ShowDialog()。我的意思是,在cnfRulesMenu_Click处于活动状态并且显示cfgForm支持HomeForm之后,我更新cfgForm中的CNF变量,然后返回HomeForm和cfgForm.Hide。但它给出了空值。



任何建议先生..

解决方案

不可能有这样的东西表单变量,但它们可以是表单类的实例字段实例属性



这个关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


代码注释:

1)

你想要在主表单和其他表单之间显示/隐藏表单。请阅读:

如何:显示模态和无模式Windows窗体 [ ^ ]

如何:在Windows应用程序中选择启动表单 [ ^ ]

如何:更改应用程序的启动对象 [ ^ ]

如何:启动Windows窗体隐形 [ ^ ]



2)

如果你使用长字符串作为变量,请使用 StringBuilderClass [ ^ ]构建这个长字符串。使用StringBuilder类 [ ^ ]最简单:

  public   string  CNF =   S  - > NP VP + System.Environment.NewLine + 
NP - > DT NN | QT NN | NP PP | NP RC | n | p + System.Environment.NewLine +
DT - > d + System.Environment.NewLine +
NN - > JJ NN | n + System.Environment.NewLine +
JJ - > JJ JJ | j + System.Environment.NewLine +
QT - > q + System.Environment.NewLine +
PP - > PT NP | GG PP + System.Environment.NewLine +
PT - >我 + System.Environment.NewLine +
GG - > g + System.Environment.NewLine +
RC - > WH NP | WH VP + System.Environment.NewLine +
WH - > w + System.Environment.NewLine;



你可以简单地追加 [ ^ ] text an d比使用ToString()方法。

 StringBuilder MyStringBuilder =  new  StringBuilder(  Hello Kitty!); 
MyStringBuilder.Append( 多么美好的一天。);
MyStringBuilder.Append( 多么美好的世界。);
MyStringBuilder.Append( 多么美丽的女人。);
string s = MyStringBuil der.ToString();





3)

如果你想在表格之间传递数据,请参阅这些:

在Windows窗体之间传递数据 [ ^ ]

使用委托在两者之间传递数据两种形式 [ ^ ]

此网站上有很多例子。请使用右上角的搜索框。 ;)


I have 2 form, first name HomeForm and the second name cfgForm

I have variables, name strCNF in HomeForm, value of strCNF from variable CNF in cfgForm.

Here the code from HomeForm :

private void cnfRulesMenu_Click(object sender, EventArgs e)
{
    try
    {
        cfgForm cfgForm = new cfgForm(this);
        cfgForm.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), "Error");
    }
}

// other parts....
public cfgForm cfg;
string strCNF = "";
strCNF = ((cfgForm)this.cfg).CNF;



Then, in another form that is cfgForm, here the code :

public string CNF = "S -> NP VP" + System.Environment.NewLine +
                        "NP -> DT NN | QT NN | NP PP | NP RC | n | p" + System.Environment.NewLine +
                        "DT -> d" + System.Environment.NewLine +
                        "NN -> JJ NN | n" + System.Environment.NewLine +
                        "JJ -> JJ JJ | j" + System.Environment.NewLine +
                        "QT -> q" + System.Environment.NewLine +
                        "PP -> PT NP | GG PP" + System.Environment.NewLine +
                        "PT -> i" + System.Environment.NewLine +
                        "GG -> g" + System.Environment.NewLine +
                        "RC -> WH NP | WH VP" + System.Environment.NewLine +
                        "WH -> w" + System.Environment.NewLine;

    public cfgForm(HomeForm homeForm)
    {
        // TODO: Complete member initialization
        InitializeComponent();
        this.homeForm = homeForm;
    }

    private void btnDefaultCFG_Click(object sender, EventArgs e)
    {
        tbCNF.Text = CNF;
    }

    private void cfgForm_Load(object sender, EventArgs e)
    {
        tbCNF.Text = CNF;
        btnApplyCNF.Enabled = false;
    }

    private void btnApplyCNF_Click(object sender, EventArgs e)
    {
        //CNF = "";
        CNF = tbCNF.Text;
        HomeForm homeForm = new HomeForm();
        homeForm.cfg = this;
        //homeForm.ShowDialog(); 
        this.Hide();// doesn't work
    }



So far, those code running well, if I use homeForm.ShowDialog(). I mean that, after cnfRulesMenu_Click is active and show cfgForm stood by HomeForm , then i update CNF variable in cfgForm, then back to HomeForm, and cfgForm.Hide. But it give null value.

Any suggestion Sir..

解决方案

There cannot be such things as "form variables", but they can be instance fields or instance properties of a form class.

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


Notes to your code:
1)
You want to show/hide forms between main form and other forms. Please, read this:
How to: Display Modal and Modeless Windows Forms[^]
How to: Choose the Startup Form in Windows Application[^]
How to: Change the Startup Object for an Application[^]
How to: Make a Startup Windows Form Invisible[^]

2)
If you use long string as a variable, please use StringBuilderClass[^] to build this long string.Using StringBuilder Class[^] is simplest than do that:

public string CNF = "S -> NP VP" + System.Environment.NewLine +
                        "NP -> DT NN | QT NN | NP PP | NP RC | n | p" + System.Environment.NewLine +
                        "DT -> d" + System.Environment.NewLine +
                        "NN -> JJ NN | n" + System.Environment.NewLine +
                        "JJ -> JJ JJ | j" + System.Environment.NewLine +
                        "QT -> q" + System.Environment.NewLine +
                        "PP -> PT NP | GG PP" + System.Environment.NewLine +
                        "PT -> i" + System.Environment.NewLine +
                        "GG -> g" + System.Environment.NewLine +
                        "RC -> WH NP | WH VP" + System.Environment.NewLine +
                        "WH -> w" + System.Environment.NewLine;


You can simply Append[^] text and than use ToString() method.

StringBuilder MyStringBuilder = new StringBuilder("Hello Kitty!");
MyStringBuilder.Append(" What a beautiful day.");
MyStringBuilder.Append(" What a wonderful world.");
MyStringBuilder.Append(" What a beautiful women.");
string s = MyStringBuilder.ToString();



3)
If you want to pass data between forms, please see these:
Passing Data between Windows Forms[^]
Using a delegate to pass data between two forms[^]
There is a lot of examples on this site. Please use "Search" box on the right-top corner. ;)


这篇关于从Form2中的值更新Form1中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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