簡體   English   中英

如果從子窗體調用方法,如何設置值?

[英]How do I set a value if a method is called from a child form?

我的申請目前有2種形式。 它創建一個子窗體Form2,它以以下代碼結尾:

public partial class Form2 : Form
{ ...
    Form1 frm = new Form1();
        frm.rglu = glu;
        frm.rdate = fulldate;
        frm.sort();
    Close();
}

請注意,form1目前僅是幾個按鈕。 一個從Form2開始,如下所示:

    private void button2_Click(object sender, EventArgs e)
    {
        using (Form2 AcqForm = new Form2())
        {
            AcqForm.ShowDialog(this);
        }
    }

除按鈕test();外,沒有其他代碼運行test(); 稍后顯示)。 這個frm.sort(); 運行在Form1中找到的以下代碼:

public partial class Form1 : Form
{
    public void sort() 
    {           
        datelist = new List<DateTime>(rdate);
        datelist.Sort((a, b) => a.CompareTo(b));

        var result = rdate
        .Select((d, i) => new { Date = d, Int = rglu[i] }) 
        .OrderBy(o => o.Date) 
        .ToArray();

        this.rdate = result.Select(o => o.Date).ToArray();
        this.rglu = result.Select(o => o.Int).ToArray();  //all works fine

        for (int i = 7; i+7 <= rglu.Length; i++)
        {
    Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
        } //This shows values as expected
    }
}

但是,當我設置一個按鈕以使用rglurdate運行更多代碼時,出現空指針錯誤:

public partial class Form1 : Form
{ 
    private void test(object sender, EventArgs e)
    {
        for (int i = 7; i < rglu.Length; i++){} //rglu is null! The values are lost.
    }
}

我相信解決方案需要int[] rglu {get; set;} int[] rglu {get; set;}方法。 但是到目前為止,我根本沒有使用這些東西。 有沒有人遇到這個問題?

編輯:rglu是這樣定義的:

public int[] rglu { get; set; } //I don't get how this works though

純粹從防御性編碼風格的角度來看,我認為在對rglu調用方法之前測試rglu是否為null是一個好習慣。

例如

public void test() 
{
    if(rglu == null)
    {
        throw new InvalidOperationException("rglu is null!");
    } 

    for (int i = 7; i < rglu.Length; i++){} //rglu is not null!
}

我還要質疑,如果從按鈕單擊事件中調用test(),為什么需要將其公開。

Form1 frm = new Form1();
frm.rglu = glu;
frm.rdate = fulldate;
frm.sort();
Close();

您不會在其他任何地方使用frm變量,也不顯示表單,因此,在這種情況下,垃圾收集器是唯一將使用rdaterglu變量接收該數據的人。

似乎您想對已經存在的表單進行操作。 在這種情況下,必須將對現有Form1的引用傳遞給Form2的方法。

更新:

它可能看起來像這樣:

public partial class Form2 : Form
{
    // ...
    private readonly Form1 _parent;

    public Form2(Form1 parent) : this()
    {
        _parent = parent;
    }

    // ... somewhere in a method which closes Form2:

        Form1 frm = _parent;
        frm.rglu = glu;
        frm.rdate = fulldate;
        frm.sort();
        Close();

    // ...
}

要顯示來自Form1 Form2 ,請使用

using(var form2 = new Form2(this))
{
    form2.ShowDialog(this);
}

問題出在form2中,您不能寫“ Form1 frm = new Form1();” 此代碼。

在Form1中,這樣的代碼:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public int rglu;
        public DateTime rdate;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            Form2 frm2=new Form2();
            frm2.Form1Instance = this;
            frm2.Show();
        }
        public void sort()
        {
            datelist = new List<DateTime>(rdate);
            datelist.Sort((a, b) => a.CompareTo(b));

            var result = rdate
            .Select((d, i) => new { Date = d, Int = rglu[i] })
            .OrderBy(o => o.Date) // Sort by whatever field you want
            .ToArray();

            this.rdate = result.Select(o => o.Date).ToArray();
            this.rglu = result.Select(o => o.Int).ToArray();  //all works fine

            for (int i = 7; i + 7 <= rglu.Length; i++)
            {
                Console.WriteLine(Convert.ToString(rdate[i]) + Convert.ToString(rglu[i]));
            } //This returns values as expected
        }
    }
}

在Form2中編寫如下代碼:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form1 Form1Instance;
        public Form2()
        {
            InitializeComponent();
        }

        private void btnSortValues_Click(object sender, EventArgs e)
        {
            Form1Instance.rglu = glu;
            Form1Instance.rdate = fulldate;
            Form1Instance.sort();
            Close();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM