C# 在另一个表单上设置标签文本时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/334770/
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-03 23:39:01  来源:igfitidea点击:

Problem setting Label text on another Form

c#winforms

提问by Rick Mays

I am trying to make a Kiosk application more accessible by increasing the size of fonts.
No problem on the main Form.
I'm having a problem replacing MessageBoxes (for which I believe there is no way to increase the font size) with small forms with the same message.

我试图通过增加字体大小来使 Kiosk 应用程序更易于访问。
主窗体上没有问题。
我在用具有相同消息的小表单替换 MessageBoxes(我认为无法增加字体大小)时遇到问题。

This is where I'm running into the problem. The main Form can't seethe error form and its Label to set the text. I have tried setting a property for the private Label on the error form, but it's still not working.

这就是我遇到问题的地方。主窗体看不到错误窗体及其标签来设置文本。我已尝试为错误表单上的私有标签设置属性,但仍然无法正常工作。

I would be very grateful for any assistance. I have been trying to apply what I've learned in reading several threads from various C# sources.

如有任何帮助,我将不胜感激。我一直在尝试应用我在阅读来自各种 C# 源的几个线程中学到的知识。

Two strange things I have noticed:

我注意到两件奇怪的事情:

  1. In the MainForm, when I type ErrorForm, Intellisense list of suggested code pops up but the variable LblNotCheckedInBecausedoes not appear on the list.
  2. The compiler error says something about the LBlNotCheckedInBecause.get statement and it seems like to me it should be refering to the set statement since I'm trying to set that value.
  1. 在 MainForm 中,当我键入 ErrorForm 时,会弹出建议代码的 Intellisense 列表,但该变量LblNotCheckedInBecause未出现在列表中。
  2. 编译器错误说明了一些关于LBlNotCheckedInBecause.get 语句的内容,在我看来它应该指的是 set 语句,因为我正在尝试设置该值。

Here are the parts of the code that I believe are involved:

以下是我认为涉及的代码部分:

From ErrorForm.Designer.cs:

来自ErrorForm.Designer.cs

private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label lblNotCheckedInBecause;

// property I created to try to be able to change the label
public string LblNotCheckedInBecause  
{
    get { return this.lblNotCheckedInBecause.Text; }
    set { this.lblNotCheckedInBecause.Text = value; }
}

From MainForm.cs:

来自MainForm.cs

// this is what I'm trying to replace
MessageBox.Show("You were not checked in because of the following reasons:" + sErrors);

// this line is causing a compiler error
ErrorForm.LblNotCheckInBecause = "You were not checked in because of the following reasons:" + sErrors; 

Compiler error:

编译器错误:

Error 1 An object reference is required for the nonstatic field, method, or property 'LogisticsKiosk.ErrorForm.LblNotCheckInBecause.get' C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\LogisticsKiosk\Forms\MainForm.cs 107 17 LogisticsKiosk

错误 1 ​​非静态字段、方法或属性“LogisticsKiosk.ErrorForm.LblNotCheckInBecause.get”需要对象引用 C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\LogisticsKiosk\Forms\MainForm.cs 107 17物流亭

回答by Rob Prouse

You cannot access the ErrorForm as if it was static. That is just the class definition, you need to set the property on an instance of the ErrorForm.

您不能像访问静态一样访问 ErrorForm。这只是类定义,您需要在 ErrorForm 的实例上设置属性。

Somewhere in your app, you created a new ErrorForm. You need to take that variable and set your LblNotCheckedInBecause property on that.

在您的应用程序的某个地方,您创建了一个新的 ErrorForm。您需要获取该变量并在其上设置 LblNotCheckedInBecause 属性。

Look for code like this;

寻找这样的代码;

ErrorForm errorFrm = new ErrorForm();
errorFrm.Show();

Then you can do this if you have a reference to that variable;

然后,如果您有对该变量的引用,则可以执行此操作;

errorFrm.LblNotCheckedInBecause = "Some Reason";

The following does not work because your Property isn't static (and can't be made static without creating a singleton which you probably don't want to do)

以下不起作用,因为您的属性不是静态的(并且不能在不创建您可能不想做的单例的情况下将其设为静态)

// Doesn't work
ErrorForm.LblNotCheckedInBecause = "Some Reason";

回答by Patrick Desjardins

You need to instance ErrorForm class before using it. You cannot use your form like if it was static.

您需要在使用它之前实例 ErrorForm 类。你不能像静态一样使用你的表单。

ErrorForm ef = new ErrorForm();
ef.LblNotCheckedInBecause   = "Your error text";
ef.Show();

回答by Rick Mays

Thank you to everyone for their help. After instantiating the form I was able to get the code working. Interestingly, it took Intellisense a few minutes to catch up.

感谢大家的帮助。实例化表单后,我能够使代码正常工作。有趣的是,Intellisense 花了几分钟才赶上。

回答by Sheraz

One thing to always keep in mind is how easy it is for another developer to read your code and understand. The best option I see is this

要始终牢记的一件事是,其他开发人员阅读和理解您的代码是多么容易。我看到的最好的选择是这个

ErrorForm form = new ErrorForm(); form.SetErrorLableMessageTo("Error Text"); form.Show();

ErrorForm 表单 = new ErrorForm(); form.SetErrorLableMessageTo("错误文本"); 表单.Show();

this is very readable. Passing the args in constructor doesn't show the intention until we go to see what's going on in constructor. Plus not in all the cases you'd want to do that and if you choose constructor way then you are bound (not a flexible design).

这是非常可读的。在构造函数中传递 args 不会显示意图,直到我们去查看构造函数中发生了什么。另外,并非在所有情况下您都想这样做,如果您选择构造函数方式,那么您将受到约束(不是灵活的设计)。

回答by GWLlosa

One other quick thing to beware of: You mentioned you edited the code in: ErrorForm.Designer.cs.

另一件需要注意的快速事项:您提到您在 ErrorForm.Designer.cs 中编辑了代码。

I'd suggest putting your added code in ErrorForm.cs instead. The compiler likes to think that it has exclusive rights to XXXXXX.Designer.cs, and has been known to blow away changes when it makes an autoedit to the file.

我建议将您添加的代码放在 ErrorForm.cs 中。编译器喜欢认为它对 XXXXXX.Designer.cs 拥有专有权,并且在对文件进行自动编辑时会破坏更改。