与另一个 C# 类的 Windows 控件交互
我是一个C#新手,遇到了以下问题。
我有一个名为 Form1
的类,它在设计视图中包含许多控件。
我有另一个名为 Staff
的类,它继承自 Form1
,其中包含一个名为 PlayAll
的方法,该方法可以播放由 Form1
播放的所有音符。用户一个接一个地敲击音乐键盘。
在方法 PlayAll
中,我实现了一个条件来确定用户是否按下了任何音符。
如果用户没有按任何注释,则错误消息应显示在 ErrorTextBox
(包含在 Form1.cs 中)中。
这是 PlayAll()
的相关代码(在 Staff.cs 中)
public void PlayAll()
{
ErrorTextBox.Text = "";
if (Pressed_Notes.Count == 0) //check if the user pressed a key
{
ErrorTextBox.Text = "There are no music notes to play!";
}
else
{
//Play the music notes
}
}
我的问题是 ErrorTextBox
上没有出现任何内容(在 Form1.cs 中找到)。请问我该如何解决这个问题?谢谢。
I am a C# newbie and have encountered the following problem.
I have a class called Form1
which contains a number of controls in design view.
I have another class called Staff
which inherits from Form1
and which, amongst others, contains a method called PlayAll
that plays all the music notes played by the user on a music keyboard one after the other.
In the method PlayAll
, I have implemented a condition which determines whether a user pressed any notes or not.
If the user did not press any notes, an error message should be displayed in ErrorTextBox
(contained in Form1.cs).
This is the relevant code of PlayAll()
(in Staff.cs)
public void PlayAll()
{
ErrorTextBox.Text = "";
if (Pressed_Notes.Count == 0) //check if the user pressed a key
{
ErrorTextBox.Text = "There are no music notes to play!";
}
else
{
//Play the music notes
}
}
My problem is that nothing appears on the ErrorTextBox
(found in Form1.cs). How can I solve this problem please? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
继承不会在所涉及的类的实例(对象)之间创建链接。
让我们创建两个实例
这将打开两个表单。现在存在两个不同的 ErrorTextBox:一个位于表单
form1
上,另一个位于表单staff
上。我建议对此问题采用两种不同的解决方案:
不要打开
Form1
,而是打开Staff
。将
form1
或form1.ErrorTextBox
的引用传递给staff
。您可以通过构造函数注入执行#2。将
Staff
的构造函数更改为:现在您可以将
Form1
的实例传递给Staff
,如下所示:Inheritance does not created a link between instances (objects) of the involved classes.
Let's create two instances
This open two forms. Now two different ErrorTextBoxes exist: One on form
form1
and one on formstaff
.I suggest two different solutions to this problem:
Instead of opening
Form1
openStaff
.Pass a reference of
form1
or ofform1.ErrorTextBox
tostaff
.You can do #2 through constructor injection. Change the constructor of
Staff
to:Now you can pass an instance of
Form1
toStaff
like this:您无法从另一个类访问任何表单控件。访问它们的简单方法是不安全的方法,这就是..
假设你有一个类
form1
,它有一个控件 Textbox1你还有另一个类
myClass
。只需使用ref
将所需的控件作为参数传递即可。例如
,但请记住,这样做是不安全的操作。这段代码在调试模式下会抛出错误。所以无需调试即可运行..
You can't access any form control from another class. The simple way to access them is unsafe way, here it is..
let suppose u have a class
form1
which have a control Textbox1and u have another class
myClass
. just pass the desired control as argument withref
.e.g.
But remember, its an unsafe operation to do so. This code will throw error in debugging mode. So run it without debugging..
这里的主要问题是当您在 Staff 类中继承 form1 类时。无法通过工作人员访问form1的表单成员(标签)。
这就是您面临这个问题的原因。
如有疑问请联系。
Here main problem is when you are inheriting form1 class in staff class. It is not possible to access form members(label) of form1 through staff.
That is why you are facing this problem.
Contact if you have any doubts.
此链接将向您展示解决该问题的 3 种方法。我建议你专注于凯文的答案来解决这个问题。
1-在form1上创建句柄,使标签修饰符公开并在form2上访问它(不建议,在链接的问题中解释)
2-将变量参数传递给form2(Kevin答案中的第一个选项)
3-创建事件这将更新该值(凯文答案中的第二个选项)
如果您需要更多信息,请发表评论。
This link will show you 3 ways of getting passed that problem. I suggest you concentrate on Kevin's answer for the solution.
1- Creating a handle on form1, making the label modifier public and acessing it on form2 (not advisable, explained in the question of the link)
2- Passing the variable parameter to form2 (first option in Kevin's answer)
3- Creating an event that will update the value (second option in Kevin's answer)
Leave a comment if you need more information.