我知道这已经被问过,但我相信我的情况有点不同 - 或者我不明白给出的答案。我已经花了大约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