tags:

views:

2323

answers:

8

I'm trying to create my own error window for the project I am working on. When I show my error window I have no way to pass the error message, and user message to the Error window because the "ErrorMessage.Text" cannot be seen in the classes I make.

I went into the form designer generated code and tried to make the TextBox static, but that just breaks things. Can I make a TextBox public / static so I can change it from another form?

  1. How do I make a TextBox Static Public so I can manipulate it across other forms, or is there another method for doing this?

EDIT:

Okay, for more information...

I have my own Form created. It is called "formErrorWindow." I need to display the form that i've pre-designed with the message set from another form. The only way I can do this is if I create a Function in the windows designer area for the form, and I set the variables with "this.errorMsg.text = error." The only way I can see that function is if I set it to static. If I set the function to Static, when I try and put "this.errorMsg.Text = error" I get this error: An object reference is required for the non-static field, method, or property.

Here is what I've attempted:

    namespace LCR_ShepherdStaffupdater_1._0
    {
        partial class formErrorWindow
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            public System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            /// 

            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
// ********* HERE IS THE FUNCTION THAT IVE ADDED BELOW. THIS WOULD WORK BUT.... *********
     public static void showError(string errorTitle, string usrMsg, string errorMsg)
    {
        formErrorWindow errorWindow = new formErrorWindow();
        errorMsgItem.Text = errorMsg;
        errorTitleItem.Text = "Error! : " + errorTitle;
        usrMsgItem.Text = usrMsg;
        errorWindow.ShowDialog();
    }
// ********* HERE IS THE FUNCTION THAT IVE ADDED ABOVE. THIS WOULD WORK BUT.... *********
// ********* I get an error: "An object reference is required for the non-static field, method, or property." *********
            public void InitializeComponent()
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formErrorWindow));
                this.usrMsgItem = new System.Windows.Forms.TextBox();
                this.errorTitleItem = new System.Windows.Forms.Label();
                this.errorMsgItem = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.panel1 = new System.Windows.Forms.Panel();
                this.label2 = new System.Windows.Forms.Label();
                this.panel1.SuspendLayout();
                this.SuspendLayout();
                // 
                // usrMsgItem
                // 
                this.usrMsgItem.Enabled = false;
                this.usrMsgItem.Location = new System.Drawing.Point(13, 37);
                this.usrMsgItem.Multiline = true;
                this.usrMsgItem.Name = "usrMsgItem";
                this.usrMsgItem.Size = new System.Drawing.Size(334, 81);
                this.usrMsgItem.TabIndex = 0;
                this.usrMsgItem.Text = "Undefined";
                // 
                // errorTitleItem
                // 
                this.errorTitleItem.AutoSize = true;
                this.errorTitleItem.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.errorTitleItem.ForeColor = System.Drawing.Color.Red;
                this.errorTitleItem.Location = new System.Drawing.Point(12, 9);
                this.errorTitleItem.Name = "errorTitleItem";
                this.errorTitleItem.Size = new System.Drawing.Size(152, 20);
                this.errorTitleItem.TabIndex = 1;
                this.errorTitleItem.Text = "Error! : Undefined";
                // 
                // errorMsgItem
                // 
                this.errorMsgItem.Enabled = false;
                this.errorMsgItem.Location = new System.Drawing.Point(0, 21);
                this.errorMsgItem.Multiline = true;
                this.errorMsgItem.Name = "errorMsgItem";
                this.errorMsgItem.Size = new System.Drawing.Size(329, 101);
                this.errorMsgItem.TabIndex = 2;
                this.errorMsgItem.Text = "Undefined";
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(272, 256);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 3;
                this.button1.Text = "Continue";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // panel1
                // 
                this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
                this.panel1.Controls.Add(this.label2);
                this.panel1.Controls.Add(this.errorMsgItem);
                this.panel1.Location = new System.Drawing.Point(12, 124);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(335, 126);
                this.panel1.TabIndex = 4;
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.label2.Location = new System.Drawing.Point(68, 1);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(189, 17);
                this.label2.TabIndex = 3;
                this.label2.Text = "Technical Error Message";
                // 
                // formErrorWindow
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
                this.ClientSize = new System.Drawing.Size(359, 290);
                this.Controls.Add(this.panel1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.errorTitleItem);
                this.Controls.Add(this.usrMsgItem);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "formErrorWindow";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                this.Text = "Error!";
                this.VisibleChanged += new System.EventHandler(this.formErrorWindow_VisibleChanged);
                this.panel1.ResumeLayout(false);
                this.panel1.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            #endregion

            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.Label label2;
            public System.Windows.Forms.TextBox usrMsgItem;
            public System.Windows.Forms.Label errorTitleItem;
            public System.Windows.Forms.TextBox errorMsgItem;
        }
    }

Look for the function I've added above. How do I get that to compile and do what I want it to do WITHOUT that error I keep getting: An object reference is required for the non-static field, method, or property.

A: 

You can make the TextBox public first of. Second make a static form(name of your form) variable and put the form there each time its loaded... This is not a good solution in my eyes but without knowing more about your design its the best i can come up with...

Petoj
Can't make a form static and modify the contents of this.errortext.text because ill get the error: An object reference is required for the non-static field, method, or property.
OneShot
Thats not what i meant.. what i meant was make a static variable that points to the form you want to manipulate..
Petoj
How do I do that?
OneShot
+1  A: 

Yes! have a function made public that can receive this text:

pseudo: public void updateTextBox(string new_text)

and have the function update the textbox from there.

don't mix UI with logic.

Ric Tokyo
You are right, that should work right? Nope. Because in order for me to see the function I have to set the function to static...which brings me to the issue in the first place. If I set it to static I get the error: An object reference is required for the non-static field, method, or property.
OneShot
@OneShot: Use non static function as delegate to be able to call non-static method
Ric Tokyo
How do I do that?
OneShot
@OneShot http://msdn.microsoft.com/en-us/library/aa288459(VS.71).aspx
Ric Tokyo
Unbelievable...I figured it out because of you. Thank you.
OneShot
@OneShot happy to help! Good job mate :)
Ric Tokyo
+6  A: 

I would just pass the message as a constructor parameter.

MyMessageBox messageBox = new MyMessageBox("My error message");
messageBox.Show();
Gerrie Schenck
No I don't want that, I have created my own form. I don't want a messagebox I want my Form to appear with the specific messages that i've set from another form.
OneShot
A: 

Allowing public access to an object contained in your class may be a design. How about writing a property in the error window class called ErrorText and inside that property set the ErrorMessage.Text to the passed string? Or am I missing something?

Frederick
I tried that, but I can't see ErrorMessage.Text...its still private in the Windows Form Designer generated code
OneShot
A: 

If you really want to go with accessing the controls directly:

  public Form2(Form1 form1)
        {
            InitializeComponent();

            Owner = form1;
            _form1 = form1;
        }

now you can access the controls on form1 this way:

_form1.theForm1ControlName
Ric Tokyo
I like your idea. I am not sure how to get that to work though.Can you revise that with the current variables...I need formSettings to display formErrorWindow. That would be perfect if I could give the form that is displaying the message ownership over the form.
OneShot
either can get ownership over the other...
Ric Tokyo
I noticed you have the method inside the class of which you are generating an object from.. are you sure you don't want to put this method in another class?
Ric Tokyo
I don't know any other way to accomplish what I need? =/
OneShot
make two forms, a big one formsettings and a small one formerror, you have there is two instance of the same form.. make two different forms instead..
Ric Tokyo
A: 

You could create a method in the Error window's class which could be something like:

public void SetErrorText(string errorText)
{
   this.ErrorMessage.Text = errorText; // assuming the TextBox' ID is ErrorMessage.
}
The only way I can see that function outside of the specific form (aka make it "Global") is if I set it to static. If I make the class static I can't use "this.ErrorMessage.Text" because Ill get the error: An object reference is required for the non-static field, method, or property.
OneShot
A: 

Sounds like you're on the right track by adding the public function you did, but you shouldn't have to make it static. As a general rule, if you have to go into the Designer Generated Code, something isn't right, so let's get out of there - it doesn't mean you can't add code to the form, obviously, but don't do it in that section :)

The error you're getting I believe originates from having a non-instantiated object (i.e. creating the error form like:

formErrorWindow myErrorWindow;

as opposed to instantiating it / creating an instance of it like so:

formErrorWindow myErrorWindow = new formErrorWindow();

You've made the function static, so you can see it from anywhere, however that function can't actually do anything because the form that it runs on doesn't exist yet until you use " = new formErrorWindow();".

So if I'm in a form called "MainForm" and I want to show an error, using the name of the function you provided it would be done like this:

formErrorWindow myErrorWindow = new formErrorWindow();
myErrorWindow.showError("Title", "User Message Here", "Error Message Here");
myErrorWindow.ShowDialog();

The key is to make sure nothing is static and then make sure you're initializing the formErrorWindow with = new formErrorWindow();

routeNpingme
A: 

solved my problem... thanx.. i jz wnt to get item of list box control from one form to another.. thnx..