C # Problem setting label text in a different form

I am a very new C # programmer and I am trying to make my kiosk app more accessible by increasing the font size. No problem with the main shape. I have a problem replacing posts (for which I believe there is no way to increase the font size) with small forms with the same post.

This is where I ran into the problem. The main form cannot "see" the error form and its label to set the text. I tried to set a property for the private label on the error form but it still doesn't work.

I would be very grateful for any help. I tried to apply what I learned while reading multiple streams from different C # sources.

Two strange things I've noticed, but are so new to C # that I don't know what they might indicate: (1) In the MainForm, when I print the ErrorForm. the Intellisense tooltip list pops up, but the LblNotCheckedInBecause variable does not appear in the list. (2) The compiler error says something about the LBlNotCheckedInBecause.get statement, and it seems to me that it should refer to the set statement, since I am trying to set that value.

Here are the parts of the code that I believe are involved, and if any additional code is required, 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 weren't checked for the following reasons:" + sErrors); // this is what I'm trying to replace ErrorForm.LblNotCheckInBecause = "You weren't checked for the following reasons:" + sErrors ; // this line raises a compiler error

Compiler error ... Error 1 Object reference is required for non-static 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


source to share


5 answers


You cannot access the ErrorForm as if it were static. It's just a class definition, you need to set a property on your ErrorForm instance.

You have created a new ErrorForm somewhere in your application. You need to take this variable and set the LblNotCheckedInBecause property for it.

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 doesn't work because your property is not static (and cannot be made static without creating a singleton, which you probably don't want to do)

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

      

+4


source


Another thing to be wary of: you mentioned that you edited the code in: ErrorForm.Designer.cs.



I would suggest adding your added code to ErrorForm.cs instead. The compiler likes to think that it has exclusive rights to XXXXXX.Designer.cs and is known to flush changes when it does auto-allocation of a file.

+2


source


You need to specify the ErrorForm class before use. You cannot use your form as if it were static.

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

      

+1


source


Thanks everyone for the help. After creating the form, I was able to get the code to work. Interestingly, Intellisense took a few minutes to catch up.

+1


source


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

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

it is very readable. Passing args in the constructor doesn't show intent until we go over what's going on in the constructor. Plus not in all cases where you want to do this, and if you choose the constructor path then you are bound (not responsive).

0


source







All Articles