可以把i看作它们之间共享的变量!!
请问怎么做啊???!
晕,这样说太抽象,能不能写这个代码我看看
静态变量虽然可以 但是也得考虑资源问题 静态变量的生命周期和应用程序一样 只要应用程序不结束它就一直存在 所以应该尽量避免使用静态变量:
Form1:MDI父窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.MdiParent = this;
f2.Show();
}
}
}
Form2:子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
f1.I = 5;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f1.I.ToString());
}
}
}
[此贴子已经被作者于2007-10-19 15:19:08编辑过]
这个不错,简单好用!!!
全局静态变量!!
静态变量虽然可以 但是也得考虑资源问题 静态变量的生命周期和应用程序一样 只要应用程序不结束它就一直存在 所以应该尽量避免使用静态变量:
Form1:MDI父窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.MdiParent = this;
f2.Show();
}
}
}
Form2:子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
f1.I = 5;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f1.I.ToString());
}
}
}
你的源码没错吧?我调试不成功!要不麻烦把你调试过的程序给我看看?发上来吧