我正在编写一个程序来访问表单2中的表单1的数组列表。在以下程序中我可以访问它但在表单2中我正在访问的数组列表(表单1)显示为空白。可能是这个原因? 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();
}
}
表格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");
}
}
答案 0 :(得分:1)
将Z=[{A{1,1} B{1,1} C{1,1}};...
{A{2,1} B{2,1} C{2,1}};...
{A{3,1} B{3,1} C{3,1}};...
{A1{1,1} B1{1,1} C1{1,1}};...
{A1{2,1} B1{2,1} C1{2,1}};...
{A1{3,1} B1{3,1} C1{3,1}};...
{A2{1,1} B2{1,1} C2{1,1}};...
{A2{2,1} B2{2,1} C2{2,1}};...
{A2{3,1} B2{3,1} C2{3,1}}];
Tstat = cell2table(VV);
Tstat.Properties.VariableNames = {'ok1' 'ok2' 'ok3'};
Tstat.Properties.RowNames = {'one' 'two' 'three' 'four' 'five' ...
'six' 'seven' 'eight' 'nine'};
writetable(Tstat, 'TstatOverview.xlsx')
传递给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();
}
}
中获取Form1
的实例:
Form2
注意:请注意,没有空值检查或其他任何内容。
答案 1 :(得分:1)
使用constructer参数将值传递给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代码
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");
}
}
答案 2 :(得分:0)
您应该在Form1
加载时创建新的Form2
。使用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");
}
答案 3 :(得分:-2)
ArrayList hop2 = new ArrayList();
应该是
public ArrayList hop2 = new ArrayList();