保持两种形式同步

保持两种形式同步

本文介绍了保持两种形式同步...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两种形式. form1有8个标签和一个音频元素. Form2有8个带有保存按钮的文本框. .在Form2中填充文本框后.然后单击保存按钮.在form1中的标签文本应替换为文本框的内容..

My application has a two forms. form1 has 8 labels and an audio element. Form2 has 8 textboxes with a save button. . After filling Textboxes in form2. And on the click of save button. The text of labels in form1 should be replaced with Content of the textboxes..

Form1 f1= new Form1();
f1.Label1.Text=textbox1.Text;
f1.Label2.Text=textbox2.Text;
f1.Label3.Text=textbox3.Text;
.....
..
...
..
f1.show();


我无法在Form2中使用此代码,因为从一开始就将使用新的音频元素来创建form1的新对象...我不希望从头开始播放新的音频元素.从form1到form2,并创建form的新对象,以及如何防止每次将数据从(FORM1)发送到(FORM2)时都重新启动音频...


I can''t use this code in Form2 because new object of form1 will be created With new audio element from the beginning... I don''t want new audio element playing from the first.. So How do i send data from from1 to form2 with creating new object of form and How do i keep audio from restarting every time i send the data from (FORM1) to (FORM2)...

推荐答案

public static class UnderlyingData
{
    // Here, the data is really stored during application lifetime
    private static string _someText = "Initial Value";

    // A publically accessible property to read and change the data
    public static string SomeText
    {
        get { return (_someText); }
        set { _someText = value; }
    }

    // Inform GUI of changes. Use an event for that.
    public static delegate void StringDelegate( string stringTypedParameter);
    public static event StringDelegate SomeTextChanged;

    // Change publically accessible property to fire the event
    public static string SomeText
    {
        get { return (_someText); }
        set
        {
            _someText = value;

            // Fire the event
            StringDelegate eventHandler = SomeTextChanged;
            if( eventHandler != null)
            {
                eventHandler(_someText);
            }
        }
    }
}

public class Form1 : Form
{
    private void TextBox1.TextChanged(object sender, EventArgs e)
    {
        // Pass text change to business layer
        UnderlyingData.SomeText = TextBox1.Text;
    }
}

public class Form2 : Form
{
    private void Form2_Load(object sender, EventArgs e)
    {
        // Subscribe to business layer's data-changed-event
        UnderlyingData.SomeTextChanged += new UnderlyingData.StringDelegate(UnderlyingData_SomeTextChanged);
    }

    private void UnderlyingData_SomeTextChanged(string someText)
    {
        // Do whatever your UI needs to do with the changed data
        Label42.Text = someText;
    }
}



那是最小的例子.当然,如果还有其他事情取决于更改后的数据,则也可以在Form1中使用该事件.或者,您可以将验证检查合并到属性的set访问器中.
[/Edit]



That''s the minimal example. Of course, you can use the event in Form1 as well, if there is some other thing that depends on the changed data. Or you can incorporate validation checks in the property''s set accessor.
[/Edit]



这篇关于保持两种形式同步...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:18