How to use a variable from one form of c # in another [closed]

0

They could explain how I can use a variable declared in one form could be used in another.

    
asked by Cristian Castillo 23.05.2016 в 19:10
source

3 answers

3

You can create a static class that gives access to all the variables that you want to share between forms:

public static class ClaseCompartida
{
    public static string ValorConfig;
    public static int MaxNum;
    // otras variables estáticas
}

And from the forms you call it a static and common static variables:

public void MetodoForm1()
{
    ClaseCompartida.ValorConfig = datoTextBox.Text;
}

public void MetodoForm2()
{
    MessageBox.Show(ClaseCompartida.MaxNum.ToString());
}

Static classes can only have static members, and are very useful in cases like this.

    
answered by 23.05.2016 в 19:26
0

Variable between forms

You could pass it as a parameter inside the constructor of your new form and in this way you can assign it to a variable of the same type and work with the new variable without affecting the first one.

Example in your form where you declare your variable:

//Variable que deseas pasar
ObjEjemplo variable = new ObjEjemplo();
//Llamas el constructor que recibe la variable
Formulario2 form2 = new Formulario2(variable);
form2.show();

In your second form you receive the variable and the sign if you wish it

    //Constructor principal
    public Formulario2()
    {
    //Utilizado por defecto. No recibe nada
    }
    //Constructor que recibe la variable
    public Formulario2(ObjEjemplo _variable)
    {
    //Haces lo que gustes con la variable preferentemente la asignas.
    }

Remember that you can overload methods in constructors to create more constructors with different parameters if you wish.

    
answered by 26.05.2016 в 17:33
0

You can do it directly like this. Thanks for asking, I saw it clearly when I was programming the example.

Create the two forms in the IDE. Form1 is created by default when creating a new solution. Then you give the right button to the project (in the solution explorer) and:

Add > > WindowForms

You then create the second form with the default name: Form2 . Then you create the buttons and other groups of controls that you need in each Form. And you adapt the following code and use the example variable to your program.

This is Form1.cs

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 usarVarEnOtroForm
{

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

        internal static string variableCompartida;


public void Form1_Load(object sender, EventArgs e)
        {
            Form2 Form2 = new Form2();
            Form2.Activate();
            Form2.Show();
        }

}

This is Form2.cs

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 usarVarEnOtroForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        internal void Form2_Load(object sender, EventArgs e)
        {
            this.Size = new Size(500, 250);
            this.Location = new Point(300,250);
        }


        private void getVar_Button(object sender, EventArgs e)
        {
            Form1.variableCompartida = richTextBox1.Text;
        }
    }
    
answered by 15.07.2016 в 21:23