i want to create a code using array in which i can store the month name by a button named ADD and use other to view month name . my code was following but the problem is it is holding last value only not all values

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] m;
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            m = new string[12];
            int i =int.Parse(textBox1.Text);
            m[i-1] = textBox2.Text;
            textBox1.Clear();
            textBox2.Clear();
            textBox1.Focus();
            listBox1.Items.Add(m[i-1]);
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button2_Click(object sender, EventArgs e)
        {
            int j = int.Parse(textBox1.Text);
            textBox2.Text = m[j-1];
            //int j = int.Parse(textBox1.Text);
            //{
            //    if (j <= 12 & j>0)
            //    {
            //        textBox2.Text = m[j]; 
            //    }
            //    else
            //    {
            //        MessageBox.Show("error");
            //    }
            //}
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Edited 5 Years Ago by __avd: Added [code] tags.

An array is instantiate each time a button's clicked. Remove a line #25 //m = new string[12];

Instantiate the array at line #18

string[] m=new string[12];
This article has been dead for over six months. Start a new discussion instead.