C# 定义属性 实现winForm窗口间传值

主窗口(frmEaa)

子窗口(FileOption)

 子窗口代码(设定属性,用于传递数据)

    public partial class FileOption : Form
    {
        bool newfile = false;
        bool openfile = false;
        #region 属性
        public string filePath
        {
            get
            {
                return tbFilePath.Text;
            }
            set
            {
                tbFilePath.Text = value;
            }
        }

        public bool fileNew
        {
            get
            {
                return newfile;
            }
            set
            {
                newfile = value;
            }
        }
        public bool fileOpen
        {
            get
            {
                return openfile;
            }
            set
            {
                openfile = value;
            }
        }
        #endregion
}

主窗口代码(用于接收数据)

        /// <summary>
        /// 文件相关操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFile_Click(object sender, EventArgs e)
        {
            try
            {
                bool ok = false;
                FileOption fo = new FileOption();
                fo.filePath = configPath;
                fo.ShowDialog();
                if (fo.fileOpen)
                {
                    configPath = fo.filePath;
                    // 读取文件数据
                    ok = usePathReadFile(configPath);
                }
                else if(fo.fileNew)
                {
                    configPath = fo.filePath;
                    commentDataRun = new Dictionary<string, Company>();
                    var lstCom = commentDataRun.Values.ToList();
                    // 填充olv内容
                    olvCompanyList.SetObjects(lstCom);
                    dgwCommentList.Rows.Clear();
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);  // 打印错误日志
                throw;
            }
        }

代码块作用:

子窗口 获得/收集 文件地址,在单击 new/open 之后将相关信息传递至主窗口。

猜你喜欢

转载自www.cnblogs.com/lijinying/p/12680241.html