简体   繁体   中英

Why does the value of my variable not change what I'm trying to change in other form?

I gotta make a program with 2 forms where in the 1st one I have to insert the size that I want my array to have and then when I press the button the array gotta take that number as it size and then open the other form where I insert values to my array and they are showed in a textbox and if I exceed the number of values it shows a messagebox that says you cant insert more but idk why when I insert the size of the array in the first form and go to the second one it only let me insert 1 value no matter what number I have used in the first one this is the code I'm using:

for the first one:

namespace Arreglo
{
    public partial class frmregistro : Form
    {
        public frmregistro()
        {
            InitializeComponent();
        }

        private void btniniciar_Click(object sender, EventArgs e)
         {
            using(frmmatricula form = new frmmatricula()) 
            { 
                int s = Convert.ToInt32(txtcantidad.Text);
                frmmatricula frm = new frmmatricula();
                frm.ShowDialog();
            }
        }
    }
}

for the second form

namespace Arreglo
{
    public partial class frmmatricula : Form
    {
        int indice = 0;

        int[] matricula = new int[5];
        string[] nombre = new string[5];
        public int s { get; set; }

        public frmmatricula()
        {
            InitializeComponent();
            this.Load += frmmatricula_Load; 

        }
        private void frmmatricula_Load(object sender, EventArgs e)
        {
            int[] matricula = new int[s];
            string[] nombre = new string[s];
        }

        private void btnagregar_Click(object sender, EventArgs e)
        {
            if (indice > s)
            {
                MessageBox.Show("No se aceptan más datos");
        }
            else
            {
                matricula[indice] = Convert.ToInt32(txtmatricula.Text);
                nombre[indice] = txtnombre.Text;
                txtresultados.Text += matricula[indice];
                txtresultados.Text += nombre[indice];
                txtresultados.Text += Environment.NewLine;
                indice++;
            }
        }

        private void btnbuscar_Click(object sender, EventArgs e)
        {
            int info = Convert.ToInt32(txtnombre.Text);
            MessageBox.Show(nombre[info] +  matricula[info]);
        }
    }
}

It looks like you're not assigning s to the public property on your other form, so it's always going to have it's default value of 0 .

I haven't looked at anything else in your form or made any other corrections (there may be other issues to fix too), except to remove one of the instances of frmmatricula you were creating in the following block. (You only need one.)

using (frmmatricula form = new frmmatricula()) 
{ 
    form.s = Convert.ToInt32(txtcantidad.Text);  // send the value to the form

    form.ShowDialog();
}

You are setting indices to 0, and then checking to see if it is > s . It wont be. It is 0 (and so is s when you are checking).

You need pass the value of your array from form1 to form2 you can do so using the public property you have defined in form2 which is public int s {get;set}; .

private void btniniciar_Click(object sender, EventArgs e)
{
  using(frmmatricula form = new frmmatricula()) 
  { 
     int s = Convert.ToInt32(txtcantidad.Text);
     frmmatricula frm = new frmmatricula();
     frm.ShowDialog();
  }
}

include the following line just before frm.ShowDialog();

frm.s = s;

Try this, I transfer your s in the second form to the first form, then I included the form in the instance of the second form:

namespace Arreglo
{
    public partial class frmregistro : Form
    {
        public int S { get; set; }

        public frmregistro()
        {
            InitializeComponent();
        }

        private void btniniciar_Click(object sender, EventArgs e)
         {
            using(frmmatricula form = new frmmatricula(this)) 
            { 
                S= Convert.ToInt32(txtcantidad.Text);
                frm.ShowDialog();
            }
        }
    }
}

namespace Arreglo
{
    public partial class frmmatricula : Form
    {
        private frmregistro _parentForm;

        int indice = 0;

        int[] matricula = new int[5];
        string[] nombre = new string[5];

        public frmmatricula(frmregistro parentForm)
        {
            _parentForm = parentForm;
            InitializeComponent();
            this.Load += frmmatricula_Load; 

        }
        private void frmmatricula_Load(object sender, EventArgs e)
        {
            int[] matricula = new int[_parentForm.S];
            string[] nombre = new string[_parentForm.S];
        }

        private void btnagregar_Click(object sender, EventArgs e)
        {
            if (indice > _parentForm.S)
            {
                MessageBox.Show("No se aceptan más datos");
            }
            else
            {
                matricula[indice] = Convert.ToInt32(txtmatricula.Text);
                nombre[indice] = txtnombre.Text;
                txtresultados.Text += matricula[indice];
                txtresultados.Text += nombre[indice];
                txtresultados.Text += Environment.NewLine;
                indice++;
            }
        }

        private void btnbuscar_Click(object sender, EventArgs e)
        {
            int info = Convert.ToInt32(txtnombre.Text);
            MessageBox.Show(nombre[info] +  matricula[info]);
        }
    }
}

Initial "s" in constructor of form like this:

...
public frmmatricula(int inputS)
{
    InitializeComponent();
    this.Load += frmmatricula_Load; 
    s = inputS;
}
...

and pass your arbitrary value for "s" when creating it like this:

...
using(frmmatricula form = new frmmatricula()) 
{ 
    int s = Convert.ToInt32(txtcantidad.Text);
    frmmatricula frm = new frmmatricula(s);
    frm.ShowDialog();
}
...

Make the following changes and it WILL work, I commented where the changes need to be made...

the code for the first form

namespace Arreglo
{
    public partial class frmregistro : Form
    {
        public frmregistro()
        {
            InitializeComponent();
        }


        private void btniniciar_Click(object sender, EventArgs e)
        {
            using (frmmatricula form = new frmmatricula())
            {

                //int s = Convert.ToInt32(txtcantidad.Text);  <-- I changed this 
                frmmatricula frm = new frmmatricula();
                frm.s = Convert.ToInt32(txtcantidad.Text);  // <-- to this ... also please note you should do validation...
                frm.ShowDialog();
            }
        }

        private void frmregistro_Load(object sender, EventArgs e)
        {

        }
    }
}

Code for the second Form

namespace Arreglo
{

    public partial class frmmatricula : Form
    {
        int indice = 0;

        private int hld_s;      // <-- I added this, just to hold the value 

        int[] matricula = new int[5];
        string[] nombre = new string[5];


        //public int s { get; set; }  <-- I changed this the the next block of code
        public int s 
        {
            get 
            {
                return hld_s; 
            }
            set
            {
                hld_s = value;
            }
        }





        public frmmatricula()
        {
            InitializeComponent();
            this.Load += frmmatricula_Load;

        }
        private void frmmatricula_Load(object sender, EventArgs e)
        {
            int[] matricula = new int[s];
            string[] nombre = new string[s];
        }

        private void btnagregar_Click(object sender, EventArgs e)
        {

            if (indice > s)
            {
                MessageBox.Show("No se aceptan más datos");
            }
            else
            {
                //... do some coding here 
            }
        }

        private void btnbuscar_Click(object sender, EventArgs e)
        {
            //... do some coding here
        }
    }
}

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