Jump to content

[solved] Accessing variables from other forms (or something)

-TesseracT-

I have two forms, form1 and form2. I have a variable (ushort) on each form. You click a button on form1 and form2 pops up. There, you choose settings that alter the value of the variable. When you presws a button on form2 it closes again.

How do I make it so that I can apply the value of the variable on form2 to the variable on form1?

How do I make it so that the value changes immedietly when you close form2?'

EDIT: C#. WOuld like code examples.

Link to comment
Share on other sites

Link to post
Share on other sites

I know in Unity (using C#) it would be easy and looks something like this:

form2.ushort = form1.ushort

Just be sure at least form1's ushort is a public variable. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

I know in Unity (using C#) it would be easy and looks something like this:

form2.ushort = form1.ushort
Just be sure at least form1's ushort is a public variable.
It doesn't work. Ushort is the datatype of my two variables, not their names. Names are "strings" and "stringNumber"
Link to comment
Share on other sites

Link to post
Share on other sites

There are a few ways you can approach this. Note that you are interested in value types and not reference types.

  1. Expose the values as public properties on Form1 and pass Form1 in itself:
    1. You can do this specifically as a constructor argument, you then simply store it as a field in Form2 and set the properties when needed...
    2. You could set the Parent property of Form2 but you'd then have to cast it to your Form1 type when you want to set the properties (never good to be doing that).
  2. Show Form2 as a modal dialog from Form1 using ShowDialog() then when you are done in Form2 set ModalResult to OK and call Close(). If you expose your values as public properties in Form2 you can simply access them in Form1 once ShowDialog() returns (it's optional whether you treat it as dialog or not).
  3. Pass a lambda into Form2 from Form1. When done in Form2 call the lambda with the values; in the lambda body you set the values back in Form1.
  4. Use static.... no just don't ever do that.

I'd give you some code examples but sadly I'm suffering with a broken finger  :( Maybe someone else would be kind and flesh these out/provide example code for you.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Another way to do this is create a new class, set the variables in form1 to the class and then pass the instance into the form2 constructor.

 

Function class:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public class Account    {        public string Username, Password;        public Account(string user="", string pass="")        {            this.Username = user;            this.Password = pass;        }    }} 

 

Form1:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Account acc = new Account("Jamie", "F");            using (Form2 main = new Form2(acc))            {                this.Hide();                main.ShowDialog();                this.Close();            }        }    }} 

 

Form2:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form2 : Form    {        public Account Account;        public Form2(Account acc)        {            InitializeComponent();            this.Account = acc;        }        private void Form2_Load(object sender, EventArgs e)        {            MessageBox.Show(Account.Username + Account.Password);        }    }} 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×