Winform中怎樣跨窗體獲取另一窗體的控件對象


場景

Winform中實現跨窗體獲取ZedGraph的ZedGraphControl控件對象:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/101375325

之前寫過使用存取全局對象的方式去跨窗體獲取控件對象。

在主窗體中有一個ZedGraphControl控件,如果要在本窗體獲取此控件對象則通過:

this.zedGraphControl1

其中zedGraphControl1是控件ZedGraphControl的name屬性。

如果在另一個窗體中獲取此控件對象並對其進行屬性設置的話,正常邏輯是

聲明主窗體對象main,然后main.zedGraphControl1去調用。

但是試過之后發現卻不能對其屬性進行更改。

因為每次new 出來是一個新的對象,並不是想獲取的控件對象

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

現在要在FrmPDFOption這個窗體中獲取MainViewContent這個窗體的zedGraph控件。

在MainViewContent窗體中,聲明一個public的靜態的當前窗體對象:

public static MainViewContent mainViewContent;

然后在MainViewContent的構造方法中將this即當前窗體對象賦值給上面的窗體對象:

public MainViewContent()
        {
            InitializeComponent();
            mainViewContent = this;
        }

然后在要調用當前MainViewContent的窗體中獲取其控件:

System.Drawing.Image image = MainViewContent.mainViewContent.zedGraphControl1.GetImage();

 

 這里是直接獲取MainViewContent中zedGraphControl1的image對象。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM