C# textbox Object reference problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • b0b3rt
    New Member
    • Feb 2008
    • 4

    C# textbox Object reference problem

    Hi. I've got a problem with object references in a second form in the application.
    I'll just post the code of both forms.
    The error is at the bottom of the post.

    First form (With textbox).
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            public static string OpenTxt;
            public static string strFN;
            public Form2 form = new Form2();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Save();
            }
    
            private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                form.Show();
            }
    
            private void Save()
            {
                strFN = textBox2.Text + Form2.strExt;
                FileStream file = new FileStream(strFN, FileMode.Create, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(file);
                sw.Write(textBox1.Text);
                sw.Flush();
                sw.Close();
            }
    
            private void Open()
            {
                Open Open = new Open();
                Open.Show();
            }
    
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
                        Save();
            }
    
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Open();
            }
        }
    }
    Second Form (the one trying to set the value):
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsApplication3
    {
        public partial class Open : Form
        {
            public string strFO;
            public Open()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                strFO = textBox1.Text;
                FileStream reading = new FileStream(strFO, FileMode.Open);
                StreamReader sr = new StreamReader(reading);
                strFO = sr.ReadToEnd();
                sr.Close();
                Form1.OpenTxt = strFO;
                this.Close();
            }
    
            private void Open_FormClosing(object sender, FormClosingEventArgs e)
            {
                Form1.textBox1.Text = Form1.OpenTxt;
            }
        }
    }
    Basically, I want to read a file using a textbox in the second form, and then set it to a string. When the "Open" button is pressed, it does all of that and closes the second form. While the second form is closing, it should set the text value of the textbox in the first form to a string value in the first form (which is set BY the second form). If the last part is unnecessary, I can simply set the textbox value from the second form.

    BUT it gives me this error: Error 1 An object reference is required for the nonstatic field, method, or property 'WindowsApplica tion3.Form1.tex tBox1' C:\Users\robert \Documents\Visu al Studio 2005\Projects\W indowsApplicati on3\WindowsAppl ication3\Open.c s 33 13 WindowsApplicat ion3

    Anybody know what I can do?
    (textBox1 is set to public).
  • khanh
    New Member
    • Feb 2008
    • 2

    #2
    You need to reference the instance of the Form1 object, not its class.
    So in Form2, delcare a Form1 object. Then, when you make a call to open Form2 from Form1, set the Form1 object that you declared to point to the instance of Form1.

    Comment

    • b0b3rt
      New Member
      • Feb 2008
      • 4

      #3
      Could you show me an example using my code? Or any code at all.
      I'm not quite sure I understand what you mean by declaring a form1 object in form2.

      Comment

      • b0b3rt
        New Member
        • Feb 2008
        • 4

        #4
        I figured out that if I do this in Form1:
        private void Open()
        {
        Open Open = new Open();
        Open.Show();
        this.Hide();
        }

        (And then call it)

        public void OpenFile()
        {
        textBox1.Text = OpenTxt; <-- (string value set in (form)Open)
        }

        and this in (form)Open:

        private void Open_FormClosed (object sender, FormClosedEvent Args e)
        {
        Form1 otx = new Form1();
        otx.Show();
        otx.OpenFile();
        }

        then the textbox displays the text from the file I'm reading from.
        However, I would like to find a way to do this without creating a separate instance of Form1, as this resets other variables that may have been changed on it, such as the SaveTo directory string.
        If there is any simple way to do it without using this: Form1 otx = new Form1();
        that would be nice.

        If there isn't a simple way to do that, is there any way to carry over variable values to the new instance?

        Thanks

        Comment

        Working...