2010-12-07 54 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....] 
    } 
} 

所以你不需要复制任何相应的设置代码和所有其他形式的类型。

相关问题