2010-12-07 56 views
1

我知道這已經被問過,但我相信我的情況有點不同 - 或者我不明白給出的答案。我已經花了大約4個小時對此進行了紮實的研究並最終實現了,我只是不知道該怎麼做。C#在沒有「公共」的情況下從外部類訪問控件

我有2個窗體(Form1,設置)和一個我創建的類叫做Themes。

我得到/設置屬性,目前的工作,但都在Form1內,我想移動儘可能多的代碼相關的主題,因爲我可以在Form1之外,並進入Themes.cs

更改主題:要更改主題,用戶打開「設置」表單並從下拉菜單中選擇一個主題並按下「設置」按鈕 - 這一切都有效,但現在我想將它移動到我自己的主題中類,我無法獲得編譯代碼。

下面是移動前的示例代碼 - 請注意,這只是我想修改的2個不同的控件,但總共大約有30個。我剝奪代碼:

表1:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnSettings_Click(object sender, EventArgs e) 
    { 
     Settings frm = new Settings(this); 
     frm.Show(); 
    } 

    private Color txtRSSURLBGProperty; 
    private Color txtRSSURLFGProperty; 

    public Color TxtRSSURLBGProperty 
    { 
     get { return txtRSSURLBGProperty; } 
     set { txtRSSURL.BackColor = value; } 
    } 

    public Color TxtRSSURLFGProperty 
    { 
     get { return txtRSSURLFGProperty; } 
     set { txtRSSURL.ForeColor = value; } 
    } 

設置形式:

public partial class Settings : Form 
{ 
    public Settings() 
    { 
     InitializeComponent(); 
    } 

    private Form1 rssReaderMain = null; 

    public Settings(Form requestingForm) 
    { 
     rssReaderMain = requestingForm as Form1; 
     InitializeComponent(); 
    } 

    private void button2_Click(object sender, EventArgs args) 
    { 
     // Appearence settings for DEFAULT THEME 

     if (cbThemeSelect.SelectedIndex == 1) 
     { 
      this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen; 
      this.rssReaderMain.TxtRSSURLFGProperty = Color.White; 
      [......about 25 more of these....] 
     } 

主題類目前是空的。再次,目標是將代碼移入主題類(特別是get/set語句,如果可能的話),並希望只要選擇正確的drowndown項目就可以在Settings窗體中使用類似於此的方法:SetTheme(Default);

我希望有人能幫忙,我希望我解釋得對!我一直在絞盡腦汁,需要儘快完成這項工作!非常感謝,我敢肯定每個人都說。如果有人想要遠程訪問,我有teamviewer或logmein--這同樣簡單。

如果需要,我也可以將我的項目作爲zip文件發送。

非常感謝,

庫爾特

審查修改後的代碼:

Form1窗體:

public partial class Form1 : ThemeableForm 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

ThemeableForm形式:

internal abstract class ThemeableForm : Form 
{ 
    private Color rssLabelBGProperty; 
    private Color rssLabelFGProperty; 

    public Color RssLabelBGProperty 
    { 
     get { return rssLabelBGProperty; } 
     set { lRSS.BackColor = value; } 
    } 

    public Color RssLabelFGProperty 
    { 
     get { return rssLabelFGProperty; } 
     set { lRSS.ForeColor = value; } 
    } 

設置形成:

public Settings(ThemeableForm requestingForm) 
    { 
     rssReaderMain = requestingForm as ThemeableForm; 
     InitializeComponent(); 
    } 

    private ThemeableForm rssReaderMain = null; 

    private void button2_Click(object sender, EventArgs args) 
    { 

     // Appearence settings for DEFAULT THEME 

     if (cbThemeSelect.SelectedIndex == 1) 
     { 
      this.rssReaderMain.LRSSBGProperty = Color.DarkSeaGreen; 
      this.rssReaderMain.LRSSFGProperty = Color.White; 
     } 

現在都在我的get/set(LRSS在上面的示例代碼)的控制出錯誤與does not exist in the current context。我也收到警告:

警告1設計者無法顯示此文件,因爲其中的類不能被設計。設計器檢查出文件中的 以下類:

Form1中---基類的RSSReader_BKRF.ThemeableForm「無法加載 。確保組件已經被引用,並且所有項目 都已經構建。 0 0

+0

Winforms,webforms或什麼? – 2010-12-07 02:35:12

+1

看起來像Winforms,從他的代碼片段。 Webforms從Page繼承。 – 2010-12-07 02:51:58

+1

對不起,沒有回答過,也沒有看到它 - 它的C#Windows窗體 – 2010-12-07 07:41:23

回答

0

讓很大程度上改變時,主題更改數據的Themes類組成:顏色,字體等

讓設置表單中選擇一個主題,寫出來作爲默認的主題。如果這是WinForms,那麼您可以只擁有Themes類的靜態CurrentTheme屬性,它返回在「設置」窗體上選擇的主題。

讓Form1中以及其他任何形式的委託它們的一些性質,以當前主題:

private Color BackgroundColor 
{ 
    get {return Themes.CurrentTheme.BackgroundColor;} 
} 

private Color TextColor 
{ 
    get {return Themes.CurrentTheme.TextColor;} 
} 

那麼你可能要推這些授權性高達基本形式類,通過多種形式共享。

0

好吧,我看到你正在試圖讓設置窗體操縱幾個(很多?)其他窗體上的屬性值。

一個解決方案是讓每一個其他形式都繼承自同一個抽象類,我們稱之爲ThemeableForm。現在你可以定義ThemeableForm來擁有所有的通用屬性。

簡單例子:

internal abstract class ThemeableForm : Form { 
    private Color txtRSSURLBGProperty; 
    private Color txtRSSURLFGProperty; 

    public Color TxtRSSURLBGProperty 
    { 
     get { return txtRSSURLBGProperty; } 
     set { txtRSSURL.BackColor = value; } 
    } 

    public Color TxtRSSURLFGProperty 
    { 
     get { return txtRSSURLFGProperty; } 
     set { txtRSSURL.ForeColor = value; } 
    } 
} 

,並聲明Form1中:

public class Form1 : ThemeableForm { 
    // custom stuff for Form1, no need to write the common properties 
} 

我宣佈爲 「內部」,因爲你可能想要控制誰/如何THemeableForm被繼承。但是,你也可以公開它。和設置可以用ThemeableForm工作:

public Settings(ThemeableForm requestingForm) 
{ 
    rssReaderMain = requestingForm as ThemeableForm; 
    InitializeComponent(); 
} 

private ThemeableForm rssReaderMain = null; 

private void button2_Click(object sender, EventArgs args) { 

    // Appearence settings for DEFAULT THEME 

    if (cbThemeSelect.SelectedIndex == 1) 
    { 
     this.rssReaderMain.TxtRSSURLBGProperty = Color.DarkSeaGreen; 
     this.rssReaderMain.TxtRSSURLFGProperty = Color.White;    
     [......about 25 more of these....] 
    } 
} 

所以你不需要複製任何相應的設置代碼和所有其他形式的類型。

相關問題