How can I use my UI form controls from another class? I want to work with these controls in another class, then call it into my main form. The problem is that the new class I created can't seem to access the label or the textboxes of these controls and I keep getting an error.
如何使用其他类的UI表单控件?我想在另一个类中使用这些控件,然后将其调用到我的主窗体中。问题是我创建的新类似乎无法访问这些控件的标签或文本框,我不断收到错误。
How would i solve this issue?
我该如何解决这个问题?
Error Messages:
错误消息:
FirstName_text is inaccessible due to its protection level
由于其保护级别,FirstName_text无法访问
First_Name_label is inaccessible due to its protection level
由于其保护级别,First_Name_label无法访问
Color does not exist in the current context
当前上下文中不存在颜色
public void Checker()
{
//First Name Validation
if (Regex_Static_Class.FirstNameRgex.IsMatch(FirstName_text.Text) == false)
{
First_Name_label.Text = "invalid first name";
Error_Lable.ForColor = Color.Pink;
}
}
2 个解决方案
#1
0
I guess First_Name_label is a UI label and it must be accessible form your other class too.
我猜First_Name_label是一个UI标签,它也必须可以从其他类访问。
Maybe make a setter method to fill in text into your label.
也许制作一个setter方法将文本填入你的标签。
#2
0
I would say that you have to set Modifiers property for your controls to public
or internal
in Forms Designer to access it from another class. Control's instance is protected by default.
我想说你必须在窗体设计器中将控件的Modifiers属性设置为public或internal,以便从另一个类访问它。 Control的实例默认受保护。
That's the answer to your question. Another thing is that it's not the best idea to do that. You shouldn't access form's controls outside of form directly. Form class should encapsulate its controls and expose an interface to change them, for example.
这就是你问题的答案。另一件事是这样做并不是最好的主意。您不应直接访问表单外的表单控件。例如,Form类应封装其控件并公开接口以更改它们。
Also Color
doesn't exists most probably because you don't have proper using
in your another class.
此外,Color很可能不存在,因为您在另一个类中没有正确使用。
It has nothing to do with conditions (references to conditions removed from the original question).
它与条件无关(引用从原始问题中删除的条件)。
#1
0
I guess First_Name_label is a UI label and it must be accessible form your other class too.
我猜First_Name_label是一个UI标签,它也必须可以从其他类访问。
Maybe make a setter method to fill in text into your label.
也许制作一个setter方法将文本填入你的标签。
#2
0
I would say that you have to set Modifiers property for your controls to public
or internal
in Forms Designer to access it from another class. Control's instance is protected by default.
我想说你必须在窗体设计器中将控件的Modifiers属性设置为public或internal,以便从另一个类访问它。 Control的实例默认受保护。
That's the answer to your question. Another thing is that it's not the best idea to do that. You shouldn't access form's controls outside of form directly. Form class should encapsulate its controls and expose an interface to change them, for example.
这就是你问题的答案。另一件事是这样做并不是最好的主意。您不应直接访问表单外的表单控件。例如,Form类应封装其控件并公开接口以更改它们。
Also Color
doesn't exists most probably because you don't have proper using
in your another class.
此外,Color很可能不存在,因为您在另一个类中没有正确使用。
It has nothing to do with conditions (references to conditions removed from the original question).
它与条件无关(引用从原始问题中删除的条件)。