473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C# textbox Object reference problem

4
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).
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9.  
  10. namespace WindowsApplication3
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public static string OpenTxt;
  15.         public static string strFN;
  16.         public Form2 form = new Form2();
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  23.         {
  24.             Save();
  25.         }
  26.  
  27.         private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
  28.         {
  29.             form.Show();
  30.         }
  31.  
  32.         private void Save()
  33.         {
  34.             strFN = textBox2.Text + Form2.strExt;
  35.             FileStream file = new FileStream(strFN, FileMode.Create, FileAccess.ReadWrite);
  36.             StreamWriter sw = new StreamWriter(file);
  37.             sw.Write(textBox1.Text);
  38.             sw.Flush();
  39.             sw.Close();
  40.         }
  41.  
  42.         private void Open()
  43.         {
  44.             Open Open = new Open();
  45.             Open.Show();
  46.         }
  47.  
  48.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  49.         {
  50.             if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
  51.                     Save();
  52.         }
  53.  
  54.         private void openToolStripMenuItem_Click(object sender, EventArgs e)
  55.         {
  56.             Open();
  57.         }
  58.     }
  59. }
Second Form (the one trying to set the value):
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9.  
  10. namespace WindowsApplication3
  11. {
  12.     public partial class Open : Form
  13.     {
  14.         public string strFO;
  15.         public Open()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             strFO = textBox1.Text;
  23.             FileStream reading = new FileStream(strFO, FileMode.Open);
  24.             StreamReader sr = new StreamReader(reading);
  25.             strFO = sr.ReadToEnd();
  26.             sr.Close();
  27.             Form1.OpenTxt = strFO;
  28.             this.Close();
  29.         }
  30.  
  31.         private void Open_FormClosing(object sender, FormClosingEventArgs e)
  32.         {
  33.             Form1.textBox1.Text = Form1.OpenTxt;
  34.         }
  35.     }
  36. }
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 'WindowsApplication3.Form1.textBox1' C:\Users\robert\Documents\Visual Studio 2005\Projects\WindowsApplication3\WindowsApplicati on3\Open.cs 33 13 WindowsApplication3

Anybody know what I can do?
(textBox1 is set to public).
Feb 17 '08 #1
3 7102
khanh
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.
Feb 17 '08 #2
b0b3rt
4
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.
Feb 17 '08 #3
b0b3rt
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, FormClosedEventArgs 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
Feb 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andrea Trevisan | last post by:
That's a revival of a known thing I suppose.I hope it's useful. My problem was: I want to have a DataGrid with two Template columns: first with TextBox,second with Button.I want to fire an event...
2
by: Robbie | last post by:
I have a Web Form with some tailored logos and artwork. The web form also has a user control that has typical registration info on it (Name, Company Name, etc.) One of the fields, a TextBox, is a...
28
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
4
by: Tom McLaughlin | last post by:
I have been working on projects in vb5 and I am now trying to get started with .net as you can see I am not doing very well. I have a program that has two forms. On form1 I have a TextBox1. From...
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
7
by: david | last post by:
I try to use "for" loop to assign textbox control ID to a textbox variable in server side codebehind for a web form. But I met some problem. What I want to do is solving the following-like code by...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
1
by: Jim in Arizona | last post by:
On one page I have a datalist where a message is bound to a label. On another page I have the datalist which allows for the insert of new message as well as being able to edit those messages. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.