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).
Second Form (the one trying to set the value):
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).
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();
}
}
}
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;
}
}
}
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).
Comment