如何从 C# 中的另一个 cs 文件访问表单对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/717074/
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
How to access form objects from another cs file in C#
提问by Ivan Prodanov
In the form.cs file I have two buttons,a memo and a timer.My question is: How do I access the timer or the memo from another cs file?
在 form.cs 文件中,我有两个按钮,一个备忘录和一个计时器。我的问题是:如何从另一个 cs 文件访问计时器或备忘录?
I've tried to make the objects public,but it didn't work,please give me a source or a project,so I can see where I'm mistaken.
我试图将对象公开,但没有奏效,请给我一个来源或一个项目,这样我就可以看到我错在哪里了。
Thanks!
谢谢!
采纳答案by Konstantin Tarkus
Select your button in designer, go to it's properties and change "Modifiers" property from Private to Public.
在设计器中选择您的按钮,转到它的属性并将“修饰符”属性从私有更改为公共。
Then you can get access to it from another class, something like this:
然后你可以从另一个类访问它,如下所示:
public static class Test
{
public static void DisalbeMyButton()
{
var form = Form.ActiveForm as Form1;
if (form != null)
{
form.MyButton.Enabled = false;
}
}
}
Note: it's just an example and definitely not a pattern for good design :-)
注意:这只是一个例子,绝对不是好的设计模式:-)
回答by John Saunders
I worry whenever I hear someone talking about "another .cs file" or "another .vb file". It often (though not always) indicates a lack of understanding of programming, at least of OO programming. What's inthe files? One class? Two?
每当我听到有人谈论“另一个 .cs 文件”或“另一个 .vb 文件”时,我就会担心。它通常(尽管不总是)表明缺乏对编程的理解,至少对 OO 编程缺乏理解。什么是在文件?一节课?二?
You're not trying to access these things from another file, you're trying to access them from a method of a class, or possibly of a module in VB.
您不是试图从另一个文件访问这些东西,而是试图从类的方法或 VB 中的模块的方法访问它们。
The answer to your question will depend on the nature of the class and method from which you're trying to access these things, and the reason why you want to access them.
您的问题的答案将取决于您尝试从中访问这些内容的类和方法的性质,以及您想要访问它们的原因。
Once you edit your question to include this information, the answers you receive will probably show you that you shouldn't be accessing these private pieces of the form in classes other than the form class itself.
一旦您编辑了您的问题以包含此信息,您收到的答案可能会告诉您,除了表单类本身之外,您不应该在其他类中访问表单的这些私有部分。
回答by Aaron Daniels
Although I agree with John Saunders, one thing you may be doing wrong, assuming that you have everything accessible through public modifiers, is that you don't have the instance of that form.
尽管我同意 John Saunders 的观点,但假设您可以通过公共修饰符访问所有内容,您可能做错的一件事是您没有该表单的实例。
For example, this is how you would do it:
例如,这是您的操作方式:
Form1 myForm = new Form1;
string theButtonTextIAmLookingFor = myForm.MyButton.Text;
I am assuming that you may be trying to access it like it's static, like this:
我假设您可能试图像静态一样访问它,如下所示:
string theButtonTextIAmLookingFor = Form1.MyButton.Text;
Just something you might want to check.
只是您可能想要检查的内容。
回答by riandp
The intuitive option is simply to make the control field public Here is a very good solution to solve this issue..
直观的选择是简单地将控制字段公开这里有一个很好的解决方案来解决这个问题。
回答by user2091150
There is Form object already instanced in Program.cs, except it have no reference. With simple editing you can turn
在 Program.cs 中已经实例化了 Form 对象,只是它没有引用。通过简单的编辑,您可以将
Application.Run(new Form1());
to
到
Application.Run(formInstance = new Form1());
declare it like
声明它喜欢
public static Form1 formInstance;
and use
并使用
Program.formInstance.MyFunction(params);