1. 程式人生 > WINDOWS開發 >C# 定義屬性 實現winForm視窗間傳值

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 之後將相關資訊傳遞至主視窗。