Access a control of the main form

4

I have a windowsform application with 2 form and form2 windows, which is generated by default (I consider it the main one) and a 2nd form, and I wanted to access a textbox of the main form from it form2 form to update it with a button from this.

I found this solution that met the requirements:

Form created by default:

private void btnIngresar_Click(object sender, EventArgs e)
    {

        lavhs instancia = new lavhs();
        usuario = txtboxUsuario.Text;
        contra = txtboxContra.Text;

        instancia.Show(this);      // que envia? una instancia del form actual?
    }

Form 2:

private void btnActualizar_Click(object sender, EventArgs e)
    {
        Form1.usuario = txtActualizar.Text; //envio el string a un string statico perteneciente al form principal

        Form form1 = this.Owner; //Obtenemos el dueño del Form (no entiendo)

        TextBox cajaTextoForm1 =  (TextBox)form1.Controls["txtboxUsuario"]; //Obtenemos la caja de texto en Form1 (no entiendo)

        //Le pasamos el texto introducido
        cajaTextoForm1.Text = txtActualizar.Text;
    }

My question is: How do you get access to the main form created by default, since I understand when creating an instance of the class, a different form is generated and this is not the case if you want to modify a controller an existing form that is already running, or I'm wrong in how I understand things.

    
asked by Miko 04.03.2016 в 19:42
source

2 answers

1

The controls in C # are passed and assigned by reference, so that as long as we do not create a new control in the assignment (with new ..), provided that assign a control to a variable (of its type), the variable will reference to that control, to the same object. I suggest:

  • Declare a textBox_par in Form2, to which we will assign txtbox_user , so that any changes we make to textBox_par are applied directly to < em> txtbox_user .

Note: I deleted parts of the code that you put, then, adapt this example to your development.

Form1.cs ..

using System;
using System.Windows.Forms;

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

        internal static string usuario;

        private void ingresar_Click(object sender, EventArgs e)
        {
            usuario = txtbox_usuario.Text;
        }

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

Form2.cs ..

using System;
using System.Windows.Forms;

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

        internal TextBox textBox_par;

        private void actualizar_button_Click(object sender, EventArgs e)
        {
            Form1.usuario = txtActualizar.Text;
            textBox_par.Text = txtActualizar.Text;
        }
    }
}
    
answered by 17.07.2016 / 16:45
source
3

You do not directly access the controls of the other, but you do it uncoupled using interfaces.

Here

Communicate form

I explain how you could achieve it

The idea is that you define an interface like being

interface IForm{
   void Metodo1();
}

Then you have the form implement the interface

public class FormPrincipal : Form, IForm{
    //codigo del form
}

When you invoke the form it passes the instance in the constructor

private void btnIngresar_Click(object sender, EventArgs e)
{
    lavhs instancia = new lavhs(this);
    instancia.Show();      
}

Access to the data, or perform actions of the main form, is done through the methods or properties that you define in the interface, but from one to another you pass simple data that you do not control.

In form2 you would use

 public class lavhs:Form{

     private IForm _form;

     public lavhs(IForm form){ //este es el constructor
          _form = form;
     }

     //para acceder al form principal lo haces por medio de _form
 }

All this I explain better in the article that I proposed at the beginning.

    
answered by 04.03.2016 в 19:57