与另一个 C# 类的 Windows 控件交互

发布于 2024-12-23 17:14:29 字数 790 浏览 1 评论 0原文

我是一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

翻身的咸鱼 2024-12-30 17:14:30

继承不会在所涉及的类的实例(对象)之间创建链接。

public class Form1 : Form
{
    public TextBox ErrorTextBox;
}

public class Staff : Form1
{ 
    public void PlayAll() { }
}

让我们创建两个实例

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff();
staff.Show();

这将打开两个表单。现在存在两个不同的 ErrorTextBox:一个位于表单 form1 上,另一个位于表单 staff 上。

我建议对此问题采用两种不同的解决方案:

  1. 不要打开 Form1,而是打开 Staff

  2. form1form1.ErrorTextBox 的引用传递给 staff

您可以通过构造函数注入执行#2。将 Staff 的构造函数更改为:

private TextBox _form1ErrorTextBox;

public Staff (Form1 form1)
{
   InitializeComponent();
    _form1ErrorTextBox = form1.ErrorTextBox;
}

public void PlayAll()
{
    _form1ErrorTextBox.Text = "";
    if (Pressed_Notes.Count == 0) {
        _form1ErrorTextBox.Text = "There are no music notes to play!";
    }  else {
        //Play the music notes
    }
}

现在您可以将 Form1 的实例传递给 Staff,如下所示:

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff(form1);

Inheritance does not created a link between instances (objects) of the involved classes.

public class Form1 : Form
{
    public TextBox ErrorTextBox;
}

public class Staff : Form1
{ 
    public void PlayAll() { }
}

Let's create two instances

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff();
staff.Show();

This open two forms. Now two different ErrorTextBoxes exist: One on form form1 and one on form staff.

I suggest two different solutions to this problem:

  1. Instead of opening Form1 open Staff.

  2. Pass a reference of form1 or of form1.ErrorTextBox to staff.

You can do #2 through constructor injection. Change the constructor of Staff to:

private TextBox _form1ErrorTextBox;

public Staff (Form1 form1)
{
   InitializeComponent();
    _form1ErrorTextBox = form1.ErrorTextBox;
}

public void PlayAll()
{
    _form1ErrorTextBox.Text = "";
    if (Pressed_Notes.Count == 0) {
        _form1ErrorTextBox.Text = "There are no music notes to play!";
    }  else {
        //Play the music notes
    }
}

Now you can pass an instance of Form1 to Staff like this:

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff(form1);
流绪微梦 2024-12-30 17:14:30

您无法从另一个类访问任何表单控件。访问它们的简单方法是不安全的方法,这就是..
假设你有一个类 form1 ,它有一个控件 Textbox1
你还有另一个类myClass。只需使用 ref 将所需的控件作为参数传递即可。
例如

public Class myClass
{
 TextBox tb;
 public myClass(ref TextBox mtb)
    {
     tb = mtb;
    }
 //...Now you can use tb as your textbox and the value in it will be 
 //...displayed on form1 control
}

public Class form1
{
 myClass mc = new myClass(ref textBox1);
 // ...
}

,但请记住,这样做是不安全的操作。这段代码在调试模式下会抛出错误。所以无需调试即可运行..

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 Textbox1
and u have another class myClass. just pass the desired control as argument with ref.
e.g.

public Class myClass
{
 TextBox tb;
 public myClass(ref TextBox mtb)
    {
     tb = mtb;
    }
 //...Now you can use tb as your textbox and the value in it will be 
 //...displayed on form1 control
}

public Class form1
{
 myClass mc = new myClass(ref textBox1);
 // ...
}

But remember, its an unsafe operation to do so. This code will throw error in debugging mode. So run it without debugging..

自在安然 2024-12-30 17:14:30

这里的主要问题是当您在 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.

爱格式化 2024-12-30 17:14:30

此链接将向您展示解决该问题的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文