简体   繁体   中英

I want to take a variable from one form and give it to a label in another how do I do this

Ok so I have six variables in one form that all change when various methods are invoked via buttons.

In another form I have six corresponding labels. I want to take the variables from the first form and set the SomeLabel.text in the second form when the button on the first form is pressed.

I've been trying to do this in this fashion Name=Mainform.InitializeComponent.SomeLabel.Text; but that doesn't work. I know the InitializeComponent method is private. I've tried changing it to public but that didn't prove fruitful either(also not good coding). So I came here to ask how I would do this. I was thinking about using a get and set setup but I'm still unable to access the label from the first form. I didn't include my code because nothing is broken in it and all the pieces that are needed are in the post.

You can either share a variable using public/internal Method or using `public / internal properties'

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Form2 frm = new Form2();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            frm.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm.SetTextLabel1("Hello world");
            //or
            frm.Label1Text = "HEllo world again";
        }
    }
}


using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //using method to set the value of label
        public void SetTextLabel1(string value)
        {

            label1.Text = value;
        }

        //using property to set the value of label
        public string Label1Text
        {
            set { label1.Text = value; }
        }
    }
}

Set Modifier property of label to Public . So you can access to label from Form object.

Well there's the quick and dirty way which you've already been provided. Or there's write a class to hold this common data ,add a method to call from form1's button that fires an event, that form2 subscribes to, reads the data and updates it's labels. They both know about the intermediate class but neither needs to know anything about the other.

Why don't you take those six variables as Public static in form2.

public partial class Form1 : Form
{        
    private void button1_Click(object sender, EventArgs e)
    {
        Form2.VariableLable1 = "a";
        Form2.VariableLable2 = "b";
    }
}

public partial class Form2 : Form
{  
    public static string VariableLable1,VariableLable2;
    private void form_load(object sender, EventArgs e)
    {
        Lable1.Text = VariableLable1;
        Lable1.Text = VariableLable2;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM