Thread: Need help passing a variable between forms

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    26

    Need help passing a variable between forms

    I know I have been posting quite a few questions these few days....all I have found the solution to on my own after playing around more....i'll be sure to update thoses so I dont start spaming the forum with un-answered posts which I appoligise about for now.

    Im having a little problem trying to get a variable passed between two forms. I have set a variable which is a string on form2 and need it shared, or passed, to form1. I have included my code for both my form1 and form2.

    on compile, I keep getting the error:

    Error 1 An object reference is required for the nonstatic field, method, or property 'Form1.cs'


    HERE IS MY FORM1

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Jesup_Administrator
    {
        public partial class form1 : Form
        {
            public form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form myForm = new form2();
                myForm.Show();
    }
    
            private void button2_Click(object sender, EventArgs e)
            {
               textBox1.Text = form2.Password;
            }
        }
    }
    HERE IS MY FORM2
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Jesup_Administrator
    {
        public partial class form2 : Form
        {
    
            public string Password = "";
    
    
            public form2()
            {
                InitializeComponent();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Password = textBox1.Text;
                this.Close();
    
            }
        }
    }
    I know I should really be learning more before jumping into creating a GUI: I am learning form an O'reilly C# 2005 book, but I really would like some help at this point with this windows application, passing my string variable to form1.

    Thanks a lot.

  2. #2
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    form2 is a class, not an object, you cannot access non static members without an object, Password is not static, so what you need to do is access Password via your form2 object, which in the case of your code is called "myForm", try something like this

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Jesup_Administrator
    {
        public partial class form1 : Form
        {
            Form myForm = new form2();
    
            public form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                myForm.Show();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
               textBox1.Text = myForm.Password;
            }
        }
    }
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Thanks. That got me a bit further. I believe I understand what your saying...Although now I get this error:

    Error 1 'System.Windows.Forms.Form' does not contain a definition for 'Password'
    Last edited by GUIPenguin; 07-09-2006 at 12:29 AM.

  4. #4
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    use
    Code:
     form2 myForm = new form2();
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    To the OP , keep in mind that variables only exist in their own scope, and that changes to them will only be visible in this scope ( unless you are using the keywords ref or out ).

    So if you want to access a variable or an object troughout a class in different methods then you should make these a field of this class.

    What is also a good practice is to have fields like your Password field like this.

    Code:
    private string password;
    
    public string Password{
         get{
    	return this.password;
         }
         set{
    	this.password = value;
         }
    }
    Its imo much cleaner code, and you can specify wether password should be read-only or can also be modified from out of the class where it exists.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Thank you so much, both of you.

    EDIT: I noticed that I cant open form2 more then once... if I push the button to open it again, I get an error:

    Cannot Access a disposed object.

    Object name: 'passwordForm'.
    Last edited by GUIPenguin; 07-09-2006 at 03:46 PM.

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1 {
    	public partial class Form1 : Form {
    
    		private Form secondForm;
    
    		public Form1() {
    			InitializeComponent();
    		}
    
    		private void button1_Click(object sender, EventArgs e) {
    			if (this.secondForm == null || this.secondForm.IsDisposed) {
    				this.secondForm = new Form();
    			}
    			this.secondForm.Show();
    		}
    	}
    }
    Should do the trick. ( you could also add an eventhandler to catch the disposing of the second form and create a new form immediately but I would not advise that method ).

    Also note that in the if structure I first check for null and afterwards check for IsDisposed, suppose you switch them you will get a NullReferenceException.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    lol..im such a newb, so thanks for baring with me:

    your above code just creates a new form, it doesnt open my form2..how would I do that so I can open up the password window and close it without getting the error?
    Last edited by GUIPenguin; 07-09-2006 at 06:59 PM.

  9. #9
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Well as soon as you close a form, it can be disposed, so instead of closing it and creating a new one, you could do something like this.

    Parent form:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1 {
    	public partial class Form1 : Form {
    
    		private SecondForm mySecondForm;
    
    		public Form1() {
    			InitializeComponent();
    			this.mySecondForm = new SecondForm();
    		}
    
    		private void button1_Click(object sender, EventArgs e) {
    			this.mySecondForm.Show();
    		}
    
    		private void button2_Click(object sender, EventArgs e) {
    			this.mySecondForm.Password = "testing";
    		}
    	}
    }
    Second form:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1 {
    	public partial class SecondForm : Form {
    
    		private string password;
    
    		public SecondForm() {
    			InitializeComponent();
    		}
    		//this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SecondForm_FormClosing);
    		private void SecondForm_FormClosing(object sender, FormClosingEventArgs e) {
    			this.Hide();
    			e.Cancel = true;
    		}
    
    		public string Password {
    			set {
    				this.textBox1.Text = value;
    				this.password = value;
    			}
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make a variable available everywhere without passing?
    By Austin in forum C++ Programming
    Replies: 9
    Last Post: 03-03-2005, 11:38 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. variable passing problem
    By bsimbeck in forum C Programming
    Replies: 3
    Last Post: 02-09-2003, 06:43 PM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 08:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM