简体   繁体   English

C#:通过form2访问form1的arraylist时出错

[英]C# : Error while accessing the arraylist of form1 by form2

i am writing a programme to access the array list of form 1 in form 2.in following programme i am able to access it but in form 2 the array list (of form 1)that i am accessing is showing blank.what can be the reason for this? 我正在编写一个程序以访问窗体2中的窗体1的数组列表。在以下程序中,我可以访问它,但是在窗体2中,我正在访问的数组列表(窗体1的)显示为空白。原因是什么? The programme for form1 is as follows: form1的程序如下:

public partial class Form1 : Form
    {
        public  ArrayList hop = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            hop.Add("2016");
            hop.Add("2015");
            Form2 f = new Form2();
            f.checkedListBox2.Text = this.textBox1.Text;
            f.Show();
        }
    }

for form 2 as follows: 对于表格2如下:

 public partial class Form2 : Form
    {
        ArrayList hop2 = new ArrayList();
        public Form2()
        {
            InitializeComponent();        
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            hop2.Add("2016");
            Form1 fp = new Form1();
           // fp.hop.Add("kite");
            if (hop2[1] == fp.hop[1])
                MessageBox.Show("equal");
            else
                MessageBox.Show("not equal");
        }
    }

Pass your Form1 to the Form2 constructor: 将您的Form1传递给Form2构造函数:

public partial class Form1 : Form
{
    public ArrayList hop = new ArrayList();

    public Form1()
    {
        InitializeComponent();
    }      

    private void button1_Click(object sender, EventArgs e)
    {
        hop.Add("2016");
        hop.Add("2015");
        Form2 f = new Form2(this);
        f.checkedListBox2.Text = this.textBox1.Text;
        f.Show();
    }
}

Get an instance of your Form1 in Form2 : Form2获取Form1的实例:

public partial class Form2 : Form
{
    ArrayList hop2 = new ArrayList();
    private readonly Form1 m_parentForm;
    public Form2(Form1 parentForm)
    {
        InitializeComponent();        

        m_parentForm = parentForm;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        hop2.Add("2016");

        if (hop2[1] == m_parentForm.hop[1])
            MessageBox.Show("equal");
        else
            MessageBox.Show("not equal");
    }
}

NOTE: Be careful, there is no null-checking or anything. 注意:请小心,没有null-checking或其他任何东西。

Use constructer parameter to pass the value to form2. 使用构造函数参数将值传递给form2。

public partial class Form1 : Form
    {
        public  ArrayList hop = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            hop.Add("2016");
            hop.Add("2015");
            Form2 f = new Form2(hop);
            f.checkedListBox2.Text = this.textBox1.Text;
            f.Show();
        }
    }

Form2 code Form2代码

public partial class Form2 : Form
    {
        private ArrayList _hopForm1;
        ArrayList hop2 = new ArrayList();
        public Form2(ArrayList hopForm1)
        {
            InitializeComponent();  
            _hopForm1 = hopForm1
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            hop2.Add("2016");
            Form1 fp = new Form1();
           // fp.hop.Add("kite");
            if (hop2[1] == _hopForm1[1])
                MessageBox.Show("equal");
            else
                MessageBox.Show("not equal");
        }
    }

You should.t create a new Form1 on Form2 load. 您应该在Form2加载时创建一个新的Form1 Use Application.OpenForms as: 使用Application.OpenForms作为:

private void Form2_Load(object sender, EventArgs e)
 {
        hop2.Add("2016");   
         var f1 = (Form1)Application.OpenForms[0];//<--this references Form1  current instance   
        if (hop2[1] == f1.hop[1])
            MessageBox.Show("equal");
        else
            MessageBox.Show("not equal");
}
ArrayList hop2 = new ArrayList();

应该

public ArrayList hop2 = new ArrayList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM