我需要一种在Windows窗体之间分配和检索值的方法(C#)

时间:2015-06-23 17:28:15

标签: c# forms global

所以,我正在做一个学校项目。应用程序需要能够计算正方形,圆形等区域。

我为每个数字都有一个表单来计算关闭区域。现在我有一个“主菜单”和三个表单(每个图形一个),我希望能够在一个表单中分配变量LatestResult并从主菜单表单访问它。

注意:我希望能够在没有“将变量加载到新表单中”的情况下执行此操作,如下所示:Form1 FormMainMenu = new Form1(LatestResult)

我一直在努力与Get&设置在我的Variables.cs课程中,但我似乎无法使其正常工作,如果可以这样做的话。

编辑:将我的代码放在帖子中

我的Variables.cs看起来像这样:

public static string latestresult2
{
    get
    {
        return latestresult2;
    }

     set
     {
          latestresult2 = value;
     }
 }

然后我在一个表单中点击按钮时分配值:

Variables.latestresult2 = breddeR + " * " + længdeR + " = " + resultat;

breddeR和længdeR是我计算的int变量,resultat就是结果。

最后,我尝试以另一种形式执行此操作:

label1.Text = Variables.latestresult2;

编辑2:

来自我的MainView表格

private void button1_Click(object sender, EventArgs e)
{
    Form2 FormTrekant = new Form2();
    FormTrekant.Show();
}

2 个答案:

答案 0 :(得分:1)

您可以使用INotifyPropertyChanged interface来促进此操作。此接口适用于WinForms和WPF。

public class Form2 : Form, INotifyPropertyChanged
{
    public string latestresult2;
    public event PropertyChangedEventHandler PropertyChanged;

    public string LatestResult2
    {
        get
        {
            return latestresult2;
        }

        set
        {
            latestresult2 = value;
            this.OnPropertyChanged("LatestResult2");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler == null)
        {
            return;
        }

        handler(this, new PropertyChangedEventArgs(propertyName));
   }
}

public class Form3 : Form, INotifyPropertyChanged
{
    public string latestResult3;
    public event PropertyChangedEventHandler PropertyChanged;

    public string LatestResult3
    {
        get
        {
            return latestresult3;
        }

        set
        {
            latestresult3 = value;
            this.OnPropertyChanged("LatestResult3");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler == null)
        {
            return;
        }

        handler(this, new PropertyChangedEventArgs(propertyName));
   }
}

INotifyPropertyChanged接口允许您订阅其他对象属性更改。在上面的代码中,第二个表单将在其LatestResult2属性的值发生更改时引发事件。

现在你只需要你的Form1订阅它。

public class MainForm : Form
{
    private Form2 secondaryForm;
    private Form3 thirdForm;

    public string LatestValue {get; set;}

    public void Form2ButtonClick() // Assume this happens in a button click event on MainWindow.
    {
        this.secondaryForm = new Form2();
        this.secondaryForm.PropertyChanged += this.LatestValueChanged;
        this.secondaryForm.Closing += this.ChildWindowClosing;
    }

    public void Form3ButtonClick() // Assume this happens in a button click event on MainWindow.
    {
        this.thirdForm = new Form3();
        this.thirdForm.PropertyChanged += this.LatestValueChanged;
        this.thirdForm.Closing += this.ChildWindowClosing;
    }

    private void LatestValueChanged(object sender, PropertyChangedEventArgs e)
    {
        // Do your update here.
        if (sender == this.secondaryForm)
        {
            this.LatestValue = this.secondaryForm.LatestValue2;
        }
        else if (sender == this.thirdForm)
        {
            this.LatestValue = this.thirdForm.LatestValue3;
        }
    }

    // Clean up our event handlers when either of the children forms close.
    private void ChildWindowClosing(object sender, ClosingWindowEventHandlerArgs args)
    {
        if (sender == this.secondaryForm)
        {
            this.secondaryForm.Closing -= this.ChildWindowClosing;
            this.secondaryForm.PropertyChanged -= this.LatestValueChanged;
        }
        else if (sender == this.thirdForm)
        {
            this.thirdForm.Closing -= this.ChildWindowClosing;
            this.thirdForm.PropertyChanged -= this.LatestValueChanged;
        }
    }
}

您的MainWindow现在可以对Form2内的更改做出反应,而无需传递值。需要注意的一点是,当Form2关闭时,您需要取消订阅活动。否则你会泄漏。

答案 1 :(得分:0)

您可以将主视图的实例指定给另一个视图。这样,您就可以访问主视图的属性。

解释一些代码;

public class MainView
{
    public string LatestResult { get; set; }
}

public class ChildView
{
    private readonly MainView MainView;
    public ChildView(MainView mainView)
    {
        this.MainView = mainView;
    }
    public void Calculation()
    {
        //Some calculation
        this.MainView.LatestResult = "Some result";
    }
}

现在这段代码可以像这样使用:

var mainView = new MainView();

var childView = new ChildView(mainView);
childView.Calculation();

//mainView.LatestResult == "Some result"