Rick Mays
Rick Mays

Reputation: 31

Problem setting Label text on another Form

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.

This is where I'm running into the problem. The main Form can't see the 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.

Two strange things I have noticed:

  1. In the MainForm, when I type ErrorForm, Intellisense list of suggested code pops up but the variable LblNotCheckedInBecause does 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.

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

From 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:

// 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

Upvotes: 2

Views: 12201

Answers (5)

GWLlosa
GWLlosa

Reputation: 24403

One other quick thing to beware of: You mentioned you edited the code in: 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.

Upvotes: 2

Sheraz
Sheraz

Reputation: 2217

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();

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).

Upvotes: 0

Rick Mays
Rick Mays

Reputation: 31

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.

Upvotes: 1

Patrick Desjardins
Patrick Desjardins

Reputation: 140803

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

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

Upvotes: 1

Rob Prouse
Rob Prouse

Reputation: 22647

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.

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

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";

Upvotes: 4

Related Questions