Results 1 to 3 of 3

Thread: passing variables from one form to another [* Resolved *]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    passing variables from one form to another [* Resolved *]

    Hello,

    I have my mdi form. One of the child forms displays a datagrid of employee salaries.

    On the mdi form l have a button called update. I want to pass the Employee ID number from the grid and display it in another form.

    This is what l have done so far, but does not work.
    In the salaries form (datagrid form) i have this code.
    Code:
                                    /****************************
                                    Set a variable and store the ID number in this value when the user clicks the grid row
                                    ****************************/
    		private string employeeNumber;
    		employeeNumber = grdSalaries[grdSalaries.CurrentRowIndex,0];
    
                                    /******************************************************
    		 *For sending this employeenumber to the update form
    		 * to update the correct employee details
    		 * ****************************************************/
    		public string getEmployeeNumber
    		{
    			get
    			{
    				return employeeNumber;
    			}
    			set
    			{
    				value = employeeNumber;
    			}
    		}

    In the update form l have this code in the form load.
    employeeNumber = parent.getEmployeeNumber;


    I have declared these above the constructor
    private string employeeNumber;
    frmSalaries parent;

    My constructor is listed below

    Code:
    public frmUpdateEmployee(frmSalaries form)
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    			parent = form;
    		}
    The error message is below
    An unhandled exception of type 'System.NullReferenceException' occurred in payrole.exe

    Additional information: Object reference not set to an instance of an object.

    The way the program works is the main control buttons are displayed in the mdi, when you click on show salaries it displays the form with the grid. The user will click once on the grid and then click on the update button on the mdi form. This will then display another form which is passed the ID number so l know who i have to update.

    Many thanks in advance,

    Steve
    Last edited by steve_rm; Jan 30th, 2005 at 05:45 AM.
    steve

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: passing variables from one form to another

    It seems like an unusual way to manage forms, but to each their own.
    You need to make the exposed property of the Salaries form return the "Current" value, instead of just assigning a value and always returning that, i.e.

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace WindowsApplication1
    {
    	public class frmSalaries : System.Windows.Forms.Form
    	{
          private System.Windows.Forms.DataGrid dataGrid1;
    		
    		private System.ComponentModel.Container components = null;
    
    		public frmSalaries()
    		{
    			InitializeComponent();
             DataTable table = new DataTable();
             table.Columns.Add("EmpCode", typeof(string));
             table.Columns.Add("Name", typeof(string));
    
             table.Rows.Add(new object[] { "BOB", "Bob Bloggs" });
             table.Rows.Add(new object[] { "TED", "Ted Bloggs" });
             table.Rows.Add(new object[] { "FRED", "Fred Bloggs" });
             table.Rows.Add(new object[] { "JOE", "Joe Bloggs" });
             table.Rows.Add(new object[] { "MIKE", "Mike Bloggs" });
             table.Rows.Add(new object[] { "GARY", "Gary Bloggs" });
    
             dataGrid1.DataSource = table;
    
             DataGridTableStyle style = new DataGridTableStyle();
             style.MappingName = "";
             dataGrid1.TableStyles.Add(style);
             dataGrid1.TableStyles[0].GridColumnStyles["EmpCode"].Width = 0;
    		}
    
          protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(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>
    		private void InitializeComponent()
    		{
             this.dataGrid1 = new System.Windows.Forms.DataGrid();
             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
             this.SuspendLayout();
             // 
             // dataGrid1
             // 
             this.dataGrid1.DataMember = "";
             this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
             this.dataGrid1.Location = new System.Drawing.Point(8, 8);
             this.dataGrid1.Name = "dataGrid1";
             this.dataGrid1.Size = new System.Drawing.Size(272, 240);
             this.dataGrid1.TabIndex = 0;
             // 
             // frmSalaries
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(292, 266);
             this.Controls.Add(this.dataGrid1);
             this.Name = "frmSalaries";
             this.Text = "frmSalaries";
             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
             this.ResumeLayout(false);
    
          }
    		#endregion
    
          public string EmployeeNumber
          {
             get{return dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();}
          }
    	}
    }
    Then, instead of passing a refernece to the Salaries Form to the Update form, I'd pass in the ID of the Employee to update, i.e.

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
    	public class frmUpdate : System.Windows.Forms.Form
    	{
          private System.Windows.Forms.TextBox textBox1;
    
          private System.ComponentModel.Container components = null;
    
    		public frmUpdate(string empCode)
    		{
    			InitializeComponent();
             textBox1.Text = empCode;
    		}
    
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(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>
    		private void InitializeComponent()
    		{
             this.textBox1 = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             // 
             // textBox1
             // 
             this.textBox1.Location = new System.Drawing.Point(96, 96);
             this.textBox1.Name = "textBox1";
             this.textBox1.TabIndex = 0;
             this.textBox1.Text = "textBox1";
             // 
             // frmUpdate
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(292, 266);
             this.Controls.Add(this.textBox1);
             this.Name = "frmUpdate";
             this.Text = "frmUpdate";
             this.ResumeLayout(false);
    
          }
    		#endregion
    	}
    }
    You can then implement the design you stated as follows:

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
    	/// <summary>
    	/// Summary description for frmMain.
    	/// </summary>
    	public class frmMain : System.Windows.Forms.Form
    	{
          private frmSalaries m_formSalaries = null;
    
          private System.Windows.Forms.Panel panel1;
          private System.Windows.Forms.Button btnShowSalaries;
          private System.Windows.Forms.Button btnUpdate;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public frmMain()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if(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>
    		private void InitializeComponent()
    		{
             this.panel1 = new System.Windows.Forms.Panel();
             this.btnShowSalaries = new System.Windows.Forms.Button();
             this.btnUpdate = new System.Windows.Forms.Button();
             this.panel1.SuspendLayout();
             this.SuspendLayout();
             // 
             // panel1
             // 
             this.panel1.Controls.Add(this.btnUpdate);
             this.panel1.Controls.Add(this.btnShowSalaries);
             this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
             this.panel1.Location = new System.Drawing.Point(0, 0);
             this.panel1.Name = "panel1";
             this.panel1.Size = new System.Drawing.Size(512, 48);
             this.panel1.TabIndex = 1;
             // 
             // btnShowSalaries
             // 
             this.btnShowSalaries.Location = new System.Drawing.Point(8, 8);
             this.btnShowSalaries.Name = "btnShowSalaries";
             this.btnShowSalaries.TabIndex = 0;
             this.btnShowSalaries.Text = "Show Salaries";
             this.btnShowSalaries.Click += new System.EventHandler(this.btnShowSalaries_Click);
             // 
             // btnUpdate
             // 
             this.btnUpdate.Location = new System.Drawing.Point(96, 8);
             this.btnUpdate.Name = "btnUpdate";
             this.btnUpdate.TabIndex = 1;
             this.btnUpdate.Text = "Update";
             this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
             // 
             // frmMain
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.ClientSize = new System.Drawing.Size(512, 430);
             this.Controls.Add(this.panel1);
             this.IsMdiContainer = true;
             this.Name = "frmMain";
             this.Text = "frmMain";
             this.panel1.ResumeLayout(false);
             this.ResumeLayout(false);
    
          }
    		#endregion
    
          [STAThread]
          static void Main() 
          {
             Application.Run(new frmMain());
          }
    
          private void btnShowSalaries_Click(object sender, System.EventArgs e)
          {
             m_formSalaries = new frmSalaries();
             m_formSalaries.MdiParent = this;
             m_formSalaries.Show();
          }
    
          private void btnUpdate_Click(object sender, System.EventArgs e)
          {
             frmUpdate form = new frmUpdate(m_formSalaries.EmployeeNumber);
             form.MdiParent = this;
             form.Show();
          }
    	}
    }

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: passing variables from one form to another

    Hello Aaron,

    Thanks for you help, that worked for me. Tell me is that the normal way to pass information to other forms. I was told using a reference was the best way as it is using object oriented methods.

    Thanks for you help,

    Steve
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width