由于其保护级别,comboBox2(或任何特定表单项)无法访问 [英] comboBox2 (or any particular form item) is inaccessible due to its protection level

查看:44
本文介绍了由于其保护级别,comboBox2(或任何特定表单项)无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个双赢表格.目前正在研究名为 HM_Settings.cs.cs 类,它与 HM_SettingsForm 结合使用.

I am working on a win-forms. Currently working on the .cs class called HM_Settings.cs which works in conjunction with HM_SettingsForm.

我试图通过声明这些全局变量来访问表单项的变量

I am trying to access the variables of the form items, by declaring these global variables

    public Brush backgroundColor;
    public Brush textColor;
    public double timeOffset;
    public double dateOffset;
    public string title;
    public bool showTitle;
    public bool showText;

    public HM_SettingsForm hmf = new HM_SettingsForm();

我试图通过将它们分配给全局变量来使用表单的变量,如下所示:

I am trying to use the form's variables by assigning them to the global variables, like so:

title = hmf.textBox1.Text;
showTitle = hmf.checkBox1.Checked;
showText = hmf.checkBox2.Checked;

以上应该可以工作.但是,我对这个错误感到震惊,告诉我由于其保护级别而无法访问.

The above should work. However, I am struck with this error, telling me it is inaccessible due to its protection level.

因此,我尝试将某些值从 private 更改为 public,但无济于事.我能做什么?

As a result, I tried changing certain values from private to public, but to no avail. What could I do?

推荐答案

HM_SettingsForm 类中使用 WinForms 设计器定义的控件默认是私有的,因此无法从一个外部类.

The controls defined inside the HM_SettingsForm class using the WinForms Designer are private by default, so it is not possible to reach their property from an external class.

您可以使用 WinForms Designer 更改属性 Modifiers 或直接修改 HM_SettingsForm.Designer.cs 文件并将其设置为 public,但我不建议这样做以这种方式解决问题.
对表单的内部控件保持开放访问可能会导致将来出现问题.
(严重违反了 OOP 的封装规则)

You could change the property Modifiers using the WinForms Designer or directly modifying the HM_SettingsForm.Designer.cs file and set it to public, but I don't recommend to resolve the problem in this way.
Leaving open access to the internal controls of a form could lead to problems in future.
(A spectacular breach of the encapsulation rule of OOP)

相反,我建议在 HM_SettingsForm 类中创建一些返回这些控件的内部值的公共属性(只有 get 访问器).
(如果需要,您还可以添加自己的自定义代码)

Instead I suggest to create, inside the HM_SettingsForm class, some public properties (with only the get accessor) that returns the internal values of these controls.
(You have also the benefit to add your own custom code if the need arises)

例如,您可以在 HM_SettingsForm 类中编写此属性

For example, you could write this property inside the HM_SettingsForm class

public class HM_SettingsForm:Form
{
     // Of course, here a more meaningful name 
     // is a must for future readers of your code
     public string ComboBox2Text
     {
         get{return this.combobox2.Text;}
     }
}

作为旁注,实际上与当前问题无关,但是....
返回字符串并将该值分配给 Brush 对象似乎是错误的.

As a side note, not actually related to the current question but....
It seems an error to return a string and assign that value to a Brush object.

这篇关于由于其保护级别,comboBox2(或任何特定表单项)无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆