温伟的程序生活,一步一个脚印
自己会努力,做真正独一无二的温伟!无论前方有多难,我都不会放弃... 每一篇日志,都是我的积累,全是我的原创!

窗体:父和子
值:字符等一般值,数组等值
1.父<---值---子(MDIParentin_add
父窗体接受值代码:
in_add in1 = new in_add();
if (in1.ShowDialog() == DialogResult.OK)
            {
                showIN();   子窗体成功后,返回,父窗体将执行的代码
            }
子窗体是这么发送值的:
this.DialogResult = DialogResult.OK;

2。父---值--->子(Form1Form2
父窗体中定义一子类
            Form2 form2 = new Form2();
            form2.ShowDialog(this);
            form2.Dispose();

不用专门发送值,只要存在的这个值是public的,子窗体就有权获得
在子窗体中定义一个父类,如下:
            Form1 form1 = new Form1();
            form1 = (Form1)this.Owner;

C#窗体中,数据一般用到数据控件,比如dataGridView,这个控件的值一般是一行或则一列的数的集合,所以用到数组。在开发的时候,容易设计到集合的值到数组的复制。
这里的数值一般是二维数值
父窗体中的数值声明:public string[,] strs = new string[1 ,4];
父窗体将dataGridView的集合赋予一个二维数组strs= ToStringArray(this.dataGridView1, true);
public string[,] ToStringArray(DataGridView dataGridView, bool includeColumnText)
        {
            #region 实现...
            string[,] arrReturn = null;
            int rowsCount = dataGridView.Rows.Count;
            int colsCount = dataGridView.Columns.Count;
            if (rowsCount > 0)
            {
                //最后一行是供输入的行时,不用读数据。
                if (dataGridView.Rows[rowsCount - 1].IsNewRow)
                {
                    rowsCount--;
                }
            }
            int i = 0;
            //包括列标题
            if (includeColumnText)
            {
                rowsCount++;
                arrReturn = new string[rowsCount, colsCount];
                for (i = 0; i < colsCount; i++)
                {
                    arrReturn[0, i] = dataGridView.Columns[i].HeaderText;
                }
                i = 1;
            }
            else
            {
                arrReturn = new string[rowsCount, colsCount];
            }
            //读取单元格数据
            int rowIndex = 0;
            for (; i < rowsCount; i++, rowIndex++)
            {
                for (int j = 0; j < colsCount; j++)
                {
                    arrReturn[i, j] = dataGridView.Rows[rowIndex].Cells[j].Value.ToString();
                }
            }
            return arrReturn;
            #endregion 实现
        }

子窗体接受二维数组:
string[,] str=form1.strs;   父窗体的strs
            for (int i = 0; i < str.GetLength(0); i++)
                for (int j = 0; j < str.GetLength(1);j++ )
                    this.label1.Text += str[i,j];

总结:数组在窗体间的传递!

posted on 2009-01-07 16:38  winvay  阅读(506)  评论(0编辑  收藏  举报