tags:

views:

5001

answers:

5

Hi, I am a very new C# programmer and 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 but am so new to C# that I don't know what they may indicate: (1) In the MainForm when I type ErrorForm. the 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 and if any additional code is needed, just let me know:

From the bottom of ErrorForm.Designer.cs...

    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label lblNotCheckedInBecause;
    public string LblNotCheckedInBecause  // property I created to try to be able
                                          // to change the label
    {
        get { return this.lblNotCheckedInBecause.Text; }
        set { this.lblNotCheckedInBecause.Text = value; }

From MainForm.cs... MessageBox.Show("You were not checked in because of the following reasons:" + sErrors); // this is what I'm trying to replace ErrorForm.LblNotCheckInBecause = "You were not checked in because of the following reasons:" + sErrors; // this line is causing a compiler error

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

+4  A: 

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";
Rob Prouse
Hi Rob,Thank you for your response and especially so quickly. It enabled me to get the code working.Rick
Rick Mays
+1  A: 

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();
Daok
+1  A: 

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.

Rick Mays
A: 

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

Sheraz
+2  A: 

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.

GWLlosa