简体   繁体   English

如果从子窗体调用方法,如何设置值?

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

My application currently has 2 forms. 我的申请目前有2种形式。 It creates a sub form, Form2, which ends with the following code: 它创建一个子窗体Form2,它以以下代码结尾:

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

Note that form1 is just a couple of buttons at the moment. 请注意,form1目前仅是几个按钮。 One starts off Form2 as follows: 一个从Form2开始,如下所示:

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

No other code runs except a button test(); 除按钮test();外,没有其他代码运行test(); shown later). 稍后显示)。 This frm.sort(); 这个frm.sort(); runs the following code found in Form1: 运行在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
    }
}

However when I set a button to run some more code using rglu and rdate the I get null pointer errors: 但是,当我设置一个按钮以使用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.
    }
}

I believe the solution requires a int[] rglu {get; set;} 我相信解决方案需要int[] rglu {get; set;} int[] rglu {get; set;} method. int[] rglu {get; set;}方法。 However so far I have been unsuccessful in using these things at all. 但是到目前为止,我根本没有使用这些东西。 Has anyone encountered this problem? 有没有人遇到这个问题?

Edit: rglu is defined like this: 编辑:rglu是这样定义的:

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

Purely from a defensive coding style point of view I think it would be good practice to test for rglu being null before calling methods on it. 纯粹从防御性编码风格的角度来看,我认为在对rglu调用方法之前测试rglu是否为null是一个好习惯。

eg 例如

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

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

I'd also question why test() needs to be public if it's called from a button click event. 我还要质疑,如果从按钮单击事件中调用test(),为什么需要将其公开。

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

You don't use frm variable anywhere else and don't display the form, so garbage collectior is the only one who is going to receive that data in rdate and rglu variables in this case. 您不会在其他任何地方使用frm变量,也不显示表单,因此,在这种情况下,垃圾收集器是唯一将使用rdaterglu变量接收该数据的人。

It seems like you wanted to operate on already existing form. 似乎您想对已经存在的表单进行操作。 In this case you must pass a reference to existing Form1 to your method in Form2 . 在这种情况下,必须将对现有Form1的引用传递给Form2的方法。

Update: 更新:

It may look like this: 它可能看起来像这样:

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();

    // ...
}

To show Form2 from Form1 , use 要显示来自Form1 Form2 ,请使用

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

Problem is in form2, You mustn't write 'Form1 frm = new Form1();' 问题出在form2中,您不能写“ Form1 frm = new Form1();” this code. 此代码。

in Form1 wite code like this: 在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
        }
    }
}

in Form2 write codes like this: 在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.

相关问题 如何将值从子表单传递回父表单? - How do I pass a value from a child back to the parent form? 如何从另一个子表单更新一个子表单 - How do I update a child form from another child form 如何在从构造函数调用的initialize方法中设置只读字段? - How do I set a readonly field in an initialize method that gets called from the constructor? 如何从MDI父级C#调用子窗体的方法 - How do I call a method of a child form from mdi parent c# 从另一个子窗体打开子窗体并将 MDI 设置为父窗体 - 怎么办? - Opening a child form from another child form and set MDI to parent form - how to do? 如何使用Startup表单中的方法在通过子表单调用时启用其控件? - How to use a method from the Startup form to enable its controls when called through a child form? 如何关闭子表单和子表单的子表单? - How do I close a child form of a child form AND the child form? 当一组子字符串连接起来形成结果字符串时,如何防止字符串出现在结果字符串中? - How do I prevent a string from appearing in a result string when a set of child strings are concatenated to form the result string? 如何从模态调用的子窗体中控制父窗体 - How to control parent form from modally called child form 如何在C#中将数据从子窗体的子窗体传递到父窗体? - How do I pass data from child of child form to parent form in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM