none
Getting string from aboutBox to main Form? RRS feed

  • Question

  • I have a simple app example. A button and a label on the mainForm and button on the aboutBox form.

    The button on the main form shows the aboutBox. The button on the aboutBox calls a method on the main form which should change the label on the main form. But the label does not change. What am I doing wrong? Here is my Code:

    MainForm:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication7
    {
        public partial class mainForm : Form
        {
            public mainForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                AboutBox1 box = new AboutBox1();
                box.Show();
            }
            public void setLabel(string x)
            {
                label1.Text = x;
            }
        }
    }
    
    aboutBox form:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication7
    {
        partial class AboutBox1 : Form
        {
            public AboutBox1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                mainForm formMain = new mainForm();
                formMain.setLabel("some new text");
            }
        }
    }
    

    Friday, February 20, 2015 11:26 PM

Answers

  • Your AboutBox1.button1_Click creates a new mainForm instance and sets _its_ label value. Then it goes out of scope immediately, subjecting the Form to garbage collection. You also never 'show' this newly created Form.

    You'll need to get the instance of the original mainForm to call setLabel on it. Here's one way to do it:

            private void button1_Click(object sender, EventArgs e)
            {
                mainForm form = (mainForm) Application.OpenForms[0];
                //mainForm formMain = new mainForm();
                //formMain.setLabel("some new text");
                form.setLabel("some new text");
            }

    Another way -- expose the main Form from the Program object:

        static class Program
        {
            public static mainForm formMain = null;
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new mainForm());
                formMain = new mainForm();
                Application.Run(formMain);
            }
        }

    And call it like this:

            private void button1_Click(object sender, EventArgs e)
            {
                //mainForm formMain = new mainForm();
                //formMain.setLabel("some new text");
                Program.formMain.setLabel("some new text");            
                this.Close();
            }




    Friday, February 20, 2015 11:42 PM

All replies

  • Your AboutBox1.button1_Click creates a new mainForm instance and sets _its_ label value. Then it goes out of scope immediately, subjecting the Form to garbage collection. You also never 'show' this newly created Form.

    You'll need to get the instance of the original mainForm to call setLabel on it. Here's one way to do it:

            private void button1_Click(object sender, EventArgs e)
            {
                mainForm form = (mainForm) Application.OpenForms[0];
                //mainForm formMain = new mainForm();
                //formMain.setLabel("some new text");
                form.setLabel("some new text");
            }

    Another way -- expose the main Form from the Program object:

        static class Program
        {
            public static mainForm formMain = null;
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new mainForm());
                formMain = new mainForm();
                Application.Run(formMain);
            }
        }

    And call it like this:

            private void button1_Click(object sender, EventArgs e)
            {
                //mainForm formMain = new mainForm();
                //formMain.setLabel("some new text");
                Program.formMain.setLabel("some new text");            
                this.Close();
            }




    Friday, February 20, 2015 11:42 PM
  • Thank you sir!

    Also,

    partial class settingsBox : Form { public settingsBox() { InitializeComponent(); cityBox.Text = mainForm.cityLabel.Text; }

    }

    I have this on a secondary form.

    The textbox in this secondary form is reading the value from a label in the first form.

    Error 1 An object reference is required for the non-static field, method, or property 'TestWeather.mainForm.cityLabel'

    How do I fix this error?


    • Edited by SnowTauren Saturday, February 21, 2015 12:14 AM changed code
    Saturday, February 21, 2015 12:06 AM
  • Assuming you went with the public static version (2nd resolution) then you need to add the object name: Program.mainForm.cityLabel.Text. Statics always must be qualified with their containing object.

    Otherwise, there's not enough context to tell you why you're getting the compiler error. Please post the definition of mainForm and cityLabel.

    Saturday, February 21, 2015 12:27 AM
  • mainForm is a parent form and cityLabel is a label in that form.
    Saturday, February 21, 2015 1:08 AM