更新打开(活动)表单之间的值 [英] Updating value between open(live) forms

查看:53
本文介绍了更新打开(活动)表单之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要表格,可以打开其他表格(目前最多可以打开3个表格).

I have a main form which allows opening another forms (at this moment up to 3 form).

我正在使用以下代码从主表单中打开表单:

I am using following code to open a form from main form:

public partial class network : Form
{
    p1 _p1 = new p1();
    p2 _p2 = new p2();
    public network()
    {
        InitializeComponent();
    }

    private void Phone1_Click(object sender, EventArgs e)
    {
        _p1.Show();
        //Phone1off.Visible = true;
        //networklog.Items.Add("Phone 1 added");
    }

上面的代码目前可以正常工作.

above code working fine at the moment.

现在,问题是当我从主窗体中打开了两个窗体,并在子窗体1中键入了某些内容,然后想要在子窗体2中显示它时.我做不到.

Now, the problem is when let's say I have opened two forms from main form and have type something in the child form1 then want to display it in child form2. I am unable to do it.

此刻,我已按照以下代码进行编码:

at this moment I have coded as below to achieve this:

public string TextBoxValue
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }  
private void button2_Click(object sender, EventArgs e)
    {
        p2 _p2 = new p2();
        textBox1.Text = "";
        textBox1.Text = "Calling Phone 2";

        _p2.TextBoxValue = "Phone 1 Calling";

    }

我所有的子表格都将具有相同的布局,以供您参考.所以我要继承1种设计的全部内容(例如form1:form2)

also for your information all my child forms will have same layout. so I am inheriting all from 1 design (say form1: form2)

感谢您的答复

推荐答案

您正在寻找

You are looking for DataBinding to have a Model (a simple class, List or Datatable) being watched by multiple controls.

首先有一个将充当模型的类:

First have a class that will act as the model:

public class PhoneModel
{
    public string SomePhone { get; set; }
} 

编译此代码,以便设计人员可以在添加...时找到该类.

Compile this so the designer can find that class when you add ...

...给您的网络表单的设计者:

... to the designer of your Network Form:

DataSource属性设置为对象,然后选择PhoneModel类.
Modifiers属性设置为Protected

Set the DataSource property to an object and choose the PhoneModel class.
Set the Modifiers property to Protected

对称为P的基类执行相同的操作.

Do the same for your base class called P.

TextBox的属性中选择DataBinsdings settings:

我不确定为什么要使用两个不同的类,但让我们将其保留为事实.

I'm not sure why have two different classes but let's keep that as a fact.

向您的每个类添加一个构造函数,该构造函数将接受来自调用方的BindingSource实例.我们使用该实例来更新每个表单的BindingSource.

Add an constructor to each of your classes that will accept the BindingSource instance from the caller. We use that instance to update the per form BindingSource.

public class P1 : P
{
    public P1(BindingSource bs):base()
    {
        base.bindingSource1.DataSource = bs.DataSource;
    }
}

对您需要同步其值的每种表单执行此操作

Do this for every form you have that needs to synchronize its values

在网络"表单上按钮的单击事件中,通过在构造函数中提供BindingSource来启动P1或P2:

In the click events of your buttons on the Network form start P1 or P2 by providing the BindingSource in the constructor:

private void button1_Click(object sender, EventArgs e)
{
    new P1(this.bindingSource1).Show();
}

实例化您的模型,我使用了Form_Load事件来完成该操作.

And have your model instantiated, I used the Form_Load event to do that.

private void Network_Load(object sender, EventArgs e)
{
    this.bindingSource1.DataSource = new PhoneModel { SomePhone = "foo" };
}

仅此而已.当您在文本框之一中输入值时,由于BindindingSource会更新绑定到的所有控件,因此所有打开的窗体上的所有值都会被更新,如本演示所示:

That is all there is. When you enter values in one of the TextBoxes all values on all open forms will get updated due to the BindindingSource that updates all controls that it is bounded to as can be seen in this demo:

这篇关于更新打开(活动)表单之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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