Boblysan

Boblysan

  • NA
  • 66
  • 0

One Method, Two Forms.. Please Help!

Dec 3 2003 4:15 AM
I must be missing something very easy here... I have to forms frmMain and frmLogging frmMain has several methods within it that will be used periodically. One of the methods is specific to applicatin logging. I have a list box created within the frmLogging class When specific chunks of code are executed within the frmMain class, I am attempting to have them write information via lstBox.add. method to the list box on within the frmLogging class. Ever time I attempt to do something like this (calling methods in one class to access objects in another) I am running in to problems. The application appears to load fine but when clicking the button on form 1 to add an item to a list box on form2 the following error occurs. *** Error *** An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe Additional information: Object reference not set to an instance of an object. ***End of Error*** The code for my sample app to demonstrate what I am doing is... ***Code*** using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication2 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public static Form2 form2; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(92, 172); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { form2.addToDisplayListBox("This is entry 1"); } } } using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace WindowsApplication2 { /// /// Summary description for Form2. /// public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox Display; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form2() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } public void addToDisplayListBox(string msg) { Display.Items.Add(msg); Display.TopIndex=Display.Items.Count-1; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Display = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // Display // this.Display.Location = new System.Drawing.Point(44, 36); this.Display.Name = "Display"; this.Display.Size = new System.Drawing.Size(184, 147); this.Display.TabIndex = 1; // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Display}); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion } } I'm sure it is something very simple I am missing. Any assistance is VERY much appreciated. Thank you all for you help. Bob

Answers (1)