我知道這已經被問過,但我相信我的情況有點不同 - 或者我不明白給出的答案。我已經花了大約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
Winforms,webforms或什麼? – 2010-12-07 02:35:12
看起來像Winforms,從他的代碼片段。 Webforms從Page繼承。 – 2010-12-07 02:51:58
對不起,沒有回答過,也沒有看到它 - 它的C#Windows窗體 – 2010-12-07 07:41:23