Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: passing variables bitween two form in C#

  1. #1
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Default passing variables bitween two form in C#

    Ok, trying not to go nuts about this. i have two forms A=mainform and B=secondFrom(not Dialog).

    i show the second form using a menu on the A. how do i get the values entered on B back to A. note it dont wish to to this

    Code:
    public some_method(){
      B b=new B();
     b.show();
    some_variable_on_b = textbox1.text;
    }
    if i have to createte the B every place i have to use it,ill go nuts.

    in short is there no way in c# for me to do

    Code:
    public some_method_on_A(){
      B b=new B();
     b.show();
    }
    
    
    public some_method_On_B(){
      A.myvariable_on_A=B.textbox1.text;
    }
    i want to use one method to show the formB and use a button on formB to assign the vaules it gathered to variable on A.
    Thanks in advance.
    Last edited by Artificial_Intelligence; Aug 6, 2006 at 01:31 PM.
    Anything or Anyone that fails to grow will eventually die. {AI}
    -------------------------------------------------
    Tomorrow is the future!
    Today Is the Tomorrow you made Yesterday!{AI}

  2. #2
    Join Date
    Aug 2002
    Posts
    6,223
    Rep Power
    0

    Default

    In VB I know a Public Variable can be created in a separate .bas Module. Don't know if that can apply to C#, though......
    .
    PC - Ubuntu 15.04 64bit Desktop
    HP Pav G60-236US 3GB RAM Laptop, Ubuntu 15.04 64bit and Win7 Home

    "So Daddy, how come you telling me stealing not right when YOU copying DVDs? How come? How Come?"


    RIP Ramesh ...

  3. #3
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    If you have two forms opened at the same time, then you may have to send a message that is peeked at. The subform sends a message when it vriable changes. The mainform would peek at the messages. This means that both form will have independent message loops/threads.

    If the subform closes before the main form updates then that is much easier to do. Just set subform data member to public, or write a public get property, or method.
    Quote Originally Posted by Artificial_Intelligence
    note it dont wish to to this

    Code:
    public some_method(){
      B b=new B();
     b.show();
    some_variable_on_b = textbox1.text;
    }
    if i have to createte the B every place i have to use it,ill go nuts.
    That's your problem rite there.
    How can you not want to call some_method() alone in a menu command event trigger. Actually some_variable_on_a = b.textbox1.text;

    Quote Originally Posted by Artificial_Intelligence
    in short is there no way in c# for me to do

    Code:
    public some_method_on_A(){
      B b=new B();
     b.show();
    }
    
    
    public some_method_On_B(){
      A.myvariable_on_A=B.textbox1.text;
    }
    i want to use one method to show the formB and use a button on formB to assign the vaules it gathered to variable on A.
    Thanks in advance.
    The only thing I can think about is that some_method_On_B() sends a message to the message processing queue of A
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #4
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Angry

    let me see if i get what your saying:

    is i want to use a method_A on a formA to show a formB and then use
    a methodG_on_FormB to send data back to FormA i would have to A:Threading or B:IPC (The forms will have to be seperate apps) ?
    Anything or Anyone that fails to grow will eventually die. {AI}
    -------------------------------------------------
    Tomorrow is the future!
    Today Is the Tomorrow you made Yesterday!{AI}

  5. #5
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Quote Originally Posted by Artificial_Intelligence
    let me see if i get what your saying:

    is i want to use a method_A on a formA to show a formB and then use
    a methodG_on_FormB to send data back to FormA i would have to A:Threading or B:IPC (The forms will have to be seperate apps) ?
    In theory, separate message loops, no modal blocking, but not necessarily different apps, and your right threads would be different.

    I am sure why the 'public' implementation is not what you want, but for me, that implementation would be the one of choice.

    Anyway, I tried a win proc messaging app, but then I found a better way to do it with threading while looking at an example of how to make cross thread call on a Control.

    Here is the code in .Net 2.0 C#. I commented out the win proc because that was not needed anymore. I also used some global variables which may not be accepted in your particular case, I hope not.

    Program.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WindowsApplication1
    {
        static class Program
        {
            static Form1 A;
            static Form2 B;
            [STAThread]
            static void Main()
            {
                A = (new Form1());
                B = (new Form2());
                B.Tag = A;
    
                Thread t1 = new Thread(new ThreadStart(RunA));
                Thread t2 = new Thread(new ThreadStart(RunB));
                t1.Start();
                t2.Start();
            }
            static void RunA()
            {
                A.ShowDialog();
            }
            static void RunB()
            {
                B.ShowDialog();
            }
        }
    }
    Form1.cs
    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
        {
            delegate void UpdateTextCallback(string strNew);
    
            public Form1()
            {
                InitializeComponent();
            }
            /*
            protected override void WndProc(ref Message m)
            {
                // Listen for operating system messages.
                switch (m.Msg)
                {
                    case 777:
                        MessageBox.Show("A recieved");
                        break;
                }
                base.WndProc(ref m);
            }
            */
            public void UpdateText(string strNew)
            {
                if (this.txtBoxA.InvokeRequired)
                {
                    this.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { strNew });
                }
                else
                {
                    this.txtBoxA.Text = strNew;
                }
            }
        }
    }
    Form2.cs
    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 Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void txtBoxB_TextChanged(object sender, EventArgs e)
            {
                /*
                Message m = Message.Create(this.Handle, (777), new IntPtr(0), new IntPtr(0));
                this.WndProc(ref m);
                */
                /*
                Message m = Message.Create(((Form)(this.Tag)).Handle, (777), new IntPtr(0), new IntPtr(0));
                ((Form)(this.Tag)).WndProc(ref m);
                */
                ((Form1)(this.Tag)).UpdateText("blah");
            }
    
            /*
            protected override void WndProc(ref Message m)
            {
                // Listen for operating system messages.
                switch (m.Msg)
                {
                    case 777:
                        MessageBox.Show("B recieved");
                        break;                
                }
                base.WndProc(ref m);
            }
            */
        }
    }
    Form1.Designer.cs
    Code:
    namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (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.txtBoxA = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // txtBoxA
                // 
                this.txtBoxA.Location = new System.Drawing.Point(113, 126);
                this.txtBoxA.Name = "txtBoxA";
                this.txtBoxA.Size = new System.Drawing.Size(67, 20);
                this.txtBoxA.TabIndex = 0;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.txtBoxA);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox txtBoxA;
        }
    }
    Form2.Designer.cs
    Code:
    namespace WindowsApplication1
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (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.txtBoxB = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // txtBoxB
                // 
                this.txtBoxB.Location = new System.Drawing.Point(113, 126);
                this.txtBoxB.Name = "txtBoxB";
                this.txtBoxB.Size = new System.Drawing.Size(67, 20);
                this.txtBoxB.TabIndex = 1;
                this.txtBoxB.TextChanged += new System.EventHandler(this.txtBoxB_TextChanged);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.txtBoxB);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox txtBoxB;
        }
    }
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #6
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Angry

    Hey thanks man but i am quite new to C#. and that is a bit too much for me at the moment. ill still give it a try. seems like a over kill for such a simple task though.
    Last edited by Artificial_Intelligence; Aug 8, 2006 at 09:13 AM.
    Anything or Anyone that fails to grow will eventually die. {AI}
    -------------------------------------------------
    Tomorrow is the future!
    Today Is the Tomorrow you made Yesterday!{AI}

  7. #7
    Join Date
    Jul 2006
    Posts
    20
    Rep Power
    0

    Default

    crosswire, why did you use threading to do this? In C++, notification is implemented with observer design pattern. The observer pattern is a built in pattern in C#. In my opinion we can use event and delegate to do this.

    I will try to post a example later.

    Rakoun
    /&#176;\
    Pa konèt mové, dèyè do sé on péyi.

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Your right! C# has good support for events delegates as one of its basic design pattern enhancements.

    This should work without creating threads.

    The purpose of the threading was to have the two windows open simulataneously, otherwise I would not use it.

    It would be nice to see your example. I am just looking at in in your light. Registering the delegate that updates the second textbox with the event that the first textbox has changed its text.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Here is a modal code. Not so much of an overkill, for real.

    I had problems in getting the delegate to run and I commented the line out. At the time UpdateText was void UpdateText(object sender, EventArgs e) and now it is void UpdateText(string strNew). I can't explain why but the delegate was not called even though it was registered under the even and the event was fired. Anyone knows why?

    Form1.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace FormsShareVar
    {
        public partial class Form1 : Form
        {
            Form2 form2;
            public Form1()
            {
                InitializeComponent();
                form2 = new Form2();
                form2.Tag = this;
                //form2.TextChanged += new System.EventHandler(UpdateText);
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                form2.ShowDialog();
            }
    
            public void UpdateText(string strNew)
            {
                this.txtBoxA.Text = strNew;
            }
    
        }
    }
    Form2.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace FormsShareVar
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                ((Form1)this.Tag).UpdateText(this.txtBoxB.Text);
            }
        }
    }
    Program.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace FormsShareVar
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    Form1.Designer.cs
    Code:
    namespace FormsShareVar
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (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.button1 = new System.Windows.Forms.Button();
                this.txtBoxA = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(101, 124);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(91, 24);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // txtBoxA
                // 
                this.txtBoxA.Location = new System.Drawing.Point(96, 57);
                this.txtBoxA.Name = "txtBoxA";
                this.txtBoxA.Size = new System.Drawing.Size(100, 20);
                this.txtBoxA.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.txtBoxA);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox txtBoxA;
        }
    }
    Form2.Designer.cs
    Code:
    namespace FormsShareVar
    {
        partial class Form2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (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.txtBoxB = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                // 
                // txtBoxB
                // 
                this.txtBoxB.Location = new System.Drawing.Point(96, 126);
                this.txtBoxB.Name = "txtBoxB";
                this.txtBoxB.Size = new System.Drawing.Size(100, 20);
                this.txtBoxB.TabIndex = 2;
                this.txtBoxB.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.txtBoxB);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox txtBoxB;
        }
    }
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  10. #10
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Rakoun++...
    simple.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

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