是这样的,我有如下的代码:
主窗体:
namespace DataImport
{
    public partial class MainFRM : Form
    {
        public ArrayList arlOrignal = new ArrayList();
        public ArrayList arlTblDBsvr = new ArrayList();
        public ArrayList arlTblDBName = new ArrayList();
        public ArrayList arlTblDBUser = new ArrayList();
        public ArrayList arlTblDBPsw = new ArrayList();
    
        private void butTest_Click(object sender, EventArgs e)
        {
            DataImpDetailFRM frmChild = new DataImpDetailFRM();
            frmChild.Show();
    }
}子窗体:
namespace DataImport
{
    public partial class ChildFRM : Form
    {
        private MainFRM frmMain;        private void ChildFRM_Load(object sender, EventArgs e)
        {
            frmMain = new DataImpMainFRM();
            //可以像如下这样操作吗?想做到的效果是,关闭子窗口后,主窗口的数组依然有值。
            frmMain.arlOrignal.add("aaa");
            frmMain.arlTblDBsvr.add("bbb");
            frmMain.arlTblDBName.add("ccc");
            frmMain.arlTblDBUser.add("ddd");
            frmMain.arlTblDBPsw.add("eee");
        }
    }
}到底应该怎样做呢?是否一定要做成MDI窗口才行?

解决方案 »

  1.   

    frmMain = new DataImpMainFRM(); 
    这句话应该报错吧?    public partial class Form1 : Form
        {
            public int haha = 0;
            Form2 frm;
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                frm = new Form2();
                frm.Show(this);
            }        private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show(haha.ToString());
            }
        }
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                ((Form1)this.Owner).haha = 2;
            }
        }
      

  2.   

    以上是我自己重新做的例子,你可以举一反三。
    两个FORM,第一个FORM1为启动FORM,其中有两个BUTTON,BUTTON1是打开FORM2,BUTTON2是用MESSAGEBOX显示HAHA的值。
    第二个FORM2为子FORM,其中有一个BUTTON1,是用来修改FORM1中HAHA的值。关键是这两句:
    frm.Show(this);

    ((Form1)this.Owner).haha = 2;
      

  3.   

    子窗体new了一个父窗体,那就“乱伦”了。老子肯不肯把钱给儿子用,那样看老子会不会把他的钱声明成"public"的。
      

  4.   

    bwangel 我已经把父窗体的数组声明成Public了,这样就可以操作了吗
      

  5.   

    主窗体中:
    frmModify frmModify = new frmModify(ip, wellname, tablename, columnname, id, count, dgvData, clboxParameter, cboxFilter, cmbboxFilter, txtboxMin, txtboxMax, cboxSort, cmbboxSort, rbtnAsc, rbtnDesc);
                        
    frmModify.ShowDialog();
    子窗体:
        public partial class frmModify : Form
        {
            public frmModify(string ipadd, string well, string table, string col, string[] idextent, int cnt, DataGridView dgv, CheckedListBox par, CheckBox cf, ComboBox cbf, TextBox min, TextBox max, CheckBox cs, ComboBox cbs, RadioButton asc, RadioButton desc)
            {
                InitializeComponent();            //初始化控件
                txtboxBaseValue.Clear();
                txtboxOffset.Clear();            //接收传递参数
                ip = ipadd;
                wellname = well;
                tablename = table;
                columnname = col;
                id = idextent;
                count = cnt;
                dgvData = dgv;
                clboxParameter = par;
                cboxFilter = cf;
                cmbboxFilter = cbf;
                txtboxMin = min;
                txtboxMax = max;
                cboxSort = cs;
                cmbboxSort = cbs;
                rbtnAsc = asc;
                rbtnDesc = desc;
            }        public string ip;               //数据库服务器IP
            public string wellname;             //井名
            public string tablename;                //数据表表名
            public string columnname;               //数据列名
            public string[] id;             //对应ID号
            public int count;               //选中的单元格数        //frmMain控件
            public DataGridView dgvData;
            public CheckedListBox clboxParameter;
            public CheckBox cboxFilter;
            public ComboBox cmbboxFilter;
            public TextBox txtboxMin;
            public TextBox txtboxMax;
            public CheckBox cboxSort;
            public ComboBox cmbboxSort;
            public RadioButton rbtnAsc;
            public RadioButton rbtnDesc;
      

  6.   

    如果是窗体控件,可以通过invoke用matlab实现分水岭算法 
      

  7.   

    还有 可以自己定义一个 专门存储 static 变量的类public partial Class CStatic
    {
        // 定义你要存储的
            public static ArrayList arlOrignal ; 
            public static ArrayList arlTblDBsvr ; 
            public static ArrayList arlTblDBName 
            public static ArrayList arlTblDBUser; 
            public static ArrayList arlTblDBPsw ;
    } private void ChildFRM_Load(object sender, EventArgs e) 
            {   
                  CStatic.arlOrignal.add("aaa"); 
                CStatic.arlTblDBsvr.add("bbb"); 
                CStatic.arlTblDBName.add("ccc"); 
                CStatic.arlTblDBUser.add("ddd"); 
                CStatic.arlTblDBPsw.add("eee"); 
            } 
      

  8.   

    Cherishny 不好意思,我是菜鸟,你指的是在主窗体定义一个专门存储 static 变量的类,然后在子窗体调用吗?能否给个具体一点的代码?
      

  9.   


    public partial class Form1 : Form
    {
    public static int haha;
    }public partial class Form2 : Form
    {
    private void button1_Click(object sender, EventArgs e)
    {
    Form1.haha=2;
    }
    }
    可以这样实现,不过并不建议这样使用.
      

  10.   

    public partial class FormNew: Form 
    {
            public ArrayList arlOrignal = new ArrayList(); 
            public ArrayList arlTblDBsvr = new ArrayList(); 
            public ArrayList arlTblDBName = new ArrayList(); 
            public ArrayList arlTblDBUser = new ArrayList(); 
            public ArrayList arlTblDBPsw = new ArrayList(); 
    }
    public partial class MainFRM :FormNew
    public partial class ChildFRM :FormNew
      

  11.   

    mohugomohu 请问怎样构造?给个代码例子看看吧,谢谢了。
      

  12.   

     ChildFRM frmChild = new ChildFRM (this); 
                frmChild.Show(); 
     public partial class ChildFRM : Form 
        { 
            private MainFRM frmMain;
     
    //子窗体的构造函数
    public  ChildFRM (MainFRM  main){
    frmMain=main;}......
    这是最简单的
      

  13.   

    可以参考一下面的问题
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
      

  14.   

    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx