-5

I'd like to know how to pass, let's say an integer, from form1 to form2.

I tried to do that through a button that would open form2, but the event button click didn't recognize the integer... What should I do?

In form1 I have integer x, and I want that when I click on button1, form2 would open up with x value in a label.

If there's a way to pass the info without the button (then I could use the button just to open form2) that would be great as well.

0

3 Answers 3

2

you can use second form constructor.

    private int input;
    public Form2(int input)
    {
        this.input = input;
        InitializeComponent();
    }

when you create an object , you can pass your var(int in here):

        int myvar=911;
        Form2 tmp = new Form2(myvar);
        tmp.Show();

now you can use that private variable in form2:

lbl.Text=input.toString();

in Form1:

private void button1_Click(object sender, EventArgs e)
    {
        Form2 tmp = new Form2(911);
        tmp.Show();
    }

and in Form2:

    public Form2(int input)
    {
        InitializeComponent();
        label1.Text = input.ToString();
    }

send your code for solve this problem.i can't find out why it doesn't recognize your vars without your codes!

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I tried to do, but I want it to open frm2 from a button click.and I cannot pass my variables from frm1 to that event( it doesn't rexognize them)
1

In your code have a variable accessible by both forms. For example create a new Namespace and add a public static class FormData with a public static int Value inside.

namespace GlobalVariables
{
  public static class FormData
  {
    public static int Value { get; set; }
  }
}

Then from both of your forms you can access the said variable (and modify it) with GlobalVariables.FormData.Value. Here I made it a property, but you can do pretty much whatever you want with it.

1 Comment

That's a great idea, though I use my self made class team which has an array of players.. Also self made. It tells me I cannot make a sstatic array... So I guess I'll have to pass that. Thanks anyway!
1

Alternatively to passing the value by Form2 constructor, you can create a property that sets the label value, e.g.

Form2

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

    public int XValue{
        set{
            label1.Text = value.ToString(); 
        }
    }
}

Form1

    public partial class Form1 : Form
{
    private int x = 10;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.XValue = x;
        form2.Show();
    }
}

3 Comments

or simply new Form2(){XValue = x}.Show();
@L.B this isn't codegolf :P
The thing is I change the value of x in form1_load. It's weird, if I do what you did and change the value in form_load, its working. But when I do it with my "team" class, its not working. I'll check that further...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.