C#:如何访问表单类之外的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/687090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:30:13  来源:igfitidea点击:

C#: How to access a button outside the form class

c#.netwinforms

提问by Ivan Prodanov

I want to either enable or disable a button from another file,what should I do?

我想启用或禁用另一个文件中的按钮,我该怎么办?

This is the form class declaration:

这是表单类声明:

public partial class Form1 : Form

I tried with

我试过

Form.btnName.enabled = false/true 

but there's no btnName member.

但没有 btnName 成员。

Thanks in advance!

提前致谢!

采纳答案by JaredPar

You need to expose the btnName member to other classes by making it public or using a property of sorts. For example add the following code to Form1

您需要通过将 btnName 成员设为公共或使用某种属性来将其公开给其他类。例如将以下代码添加到 Form1

public Button ButtonName { get { return btnName; } }

Now you can use form.ButtonName for any instance of Form1

现在您可以将 form.ButtonName 用于 Form1 的任何实例

回答by Reed Copsey

You'll have to specify it on your specific instance of Form1.

您必须在 Form1 的特定实例上指定它。

Ie: If you have something like Form1 myForm = new Form1(...);, then you can do myForm.btnName.Enabled = false;

即:如果你有类似的东西Form1 myForm = new Form1(...);,那么你可以做myForm.btnName.Enabled = false;

This will also require that btnName is public. It would be "better" to make a property or accessor to retrieve it than directly provide public access to the, by default, private button field member.

这也要求 btnName 是公开的。与直接提供对默认情况下的私有按钮字段成员的公共访问相比,创建属性或访问器来检索它会“更好”。

回答by JoshBerke

You need to add a public property, or method to set the button.

您需要添加公共属性或方法来设置按钮。

public void DisableBtnName()
{
  this.btnName.Enabled=false;
}

public Button BtnName
{
    get { return this.btnName;}
}

回答by casperOne

This is because by default, the controls on a form are not public (unlike in VB6 which all controls were exposed publicly).

这是因为默认情况下,表单上的控件不是公开的(不像在 VB6 中所有控件都是公开的)。

I believe you can change the visibility accessor in the designer to public, but that's generally a bad idea.

我相信您可以将设计器中的可见性访问器更改为公开,但这通常是一个坏主意。

Rather, you should expose a method on your form that will perform the action on the button, and make that method accessible to whatever code you want to call it from. This allows for greater encapsulation and will help prevent side effects from occurring in your code.

相反,您应该在表单上公开一个方法,该方法将在按钮上执行操作,并使该方法可被您想要调用它的任何代码访问。这允许更大的封装,并有助于防止在您的代码中发生副作用。

回答by SirDemon

Simply expose a public method:

只需公开一个公共方法:

public void EnableButton(bool enable)
{
    this.myButton.Enabled = enable;
}

Correction:

更正:

public void EnableButton()
{
    this.myButton.Enabled = true;
}

回答by eglasius

I really suggest to read more information on how forms fit in .net. You have a couple issues in that sample code "Form.btnName.enabled = false/true"

我真的建议阅读有关表单如何适应 .net 的更多信息。您在示例代码“Form.btnName.enabled = false/true”中有几个问题

  • Your form is called Form1, it inherits from Form.
  • Forms are instances, in fact you can have different form instances in an application belonging to the same class.
  • Because of the above, it would not make sense to access Form1.btnName. You have to do it through the specific instance.
  • Form's controls are not public by default, define a method for that.
  • Windows forms projects, usually have a main that runs the form. There you can access the form instance and hand it to something else in the app.
  • The above answers the specific question. Note that there are multiple ways to achieve different scenarios, and what you really want to do might not need the above approach.
  • 您的表单称为 Form1,它继承自 Form。
  • 表单是实例,实际上您可以在属于同一类的应用程序中拥有不同的表单实例。
  • 由于上述原因,访问 Form1.btnName 是没有意义的。您必须通过特定实例来完成。
  • 默认情况下,表单的控件不是公开的,请为此定义一个方法。
  • Windows 窗体项目,通常有一个运行窗体的主程序。在那里您可以访问表单实例并将其交给应用程序中的其他内容。
  • 以上回答了具体问题。请注意,有多种方法可以实现不同的场景,您真正想做的可能不需要上述方法。

回答by User6667769

In Form1, create a object for external class(add button name in the parameter)

在 Form1 中,为外部类创建一个对象(在参数中添加按钮名称)

Class1 obj_Class1 = new Class1(btnName);

In Class1, create a private button

在 Class1 中,创建一个私有按钮

private System.Windows.Forms.Button btnName;

In Class1 Construct

在 Class1 构造

 public Class1(System.Windows.Forms.Button btnName)
        {
            this. btnName = btnName;
        }

then you can access your button like,

然后你可以访问你的按钮,比如,

btnName.enabled = false/true;