How can I use the data I get from a texbox in the whole form?

0

How can I occupy the data I get from a texbox in the whole form if I have to repeat the declaration of a variable in each event of different buttons in C #, for example I have two buttons, but I do not want to declare the variable twice name, how can I do this, I'm a novice and I want to improve my code, thanks

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 ADULTOS
{
    public partial class familiar : Form
    {
      string curp = tb_curp.Text; //es el texbox

        public familiar()
        {
            InitializeComponent();

        }

        private void familiar_Load(object sender, EventArgs e)
        {
            cb_pa.DataSource = Model.ObtenerParentesco(); //llenamos el combo
            Console.WriteLine(cb_pa.DataSource);
            cb_pa.DisplayMember = "nombre_p";
            cb_pa.ValueMember = "parentesco_id";
        }

        bool validarTextBoxs()
        {
            foreach (Control item in this.Controls)
            {
                try
                {
                    if (item is TextBox)
                    {
                        //Codigo comprobacion  de textbox
                        if (item.Text == "")
                        {
                            MessageBox.Show("Hay campos vacios", "ATENCIÓN", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            item.Focus();
                            return false;
                        }
                    }
                    else if (item is RichTextBox)
                    {
                        //codigo comprobacion de richtextbox
                        if (item.Text == "")
                        {
                            MessageBox.Show("Hay campos vacios");
                            item.Focus();
                            return false;
                        }
                    }
                    else if (item is ComboBox)
                    {
                        if (item.Text == "")
                        {
                            MessageBox.Show("Debes seleccionar un item");
                            item.Focus();
                            return false;
                        }
                    }
                }
                catch { }
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openUrlImg = new OpenFileDialog();
            openUrlImg.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                                "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                                "Portable Network Graphic (*.png)|*.png";
            openUrlImg.Title = "Seleccionar una imagen";
            try
            {
                if (openUrlImg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (openUrlImg.FileName.Equals("") == false)
                    {
                        l_foto_f.Text = openUrlImg.FileName;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("No se puede cargar imagen: " + ex.ToString(), "ATENCIÓN", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        private void button2_Click(object sender, EventArgs e)
        {

            if (validarTextBoxs())
            {

            }

        }

        private void btn_con_Click(object sender, EventArgs e)
        {


        }

    }
}
    
asked by Karla Huerta 15.01.2018 в 18:29
source

2 answers

4

declare your variable outside the methods to be global

  string nombre= textBox1.Text;

        private void button2_Click(object sender, EventArgs e)
            {

               // aca ya podes usar nombre

            }

     private void button1_Click(object sender, EventArgs e)
            {

               // aca ya podes usar nombre

            }
    
answered by 15.01.2018 / 18:31
source
2

As Bruno told you, you must instantiate your variable name globally and not within the click event of the button, you must also do it inside the main class, surely you are doing it outside of this and that's why you get that error

public partial class TuClase...
    
answered by 15.01.2018 в 18:53