0

My program consists of 3 static buttons created using WinForms: button1, button2 and button3. Buttons 2 and 3 are set to enabled=False. What I want to do is enable these 2 buttons in turn by clicking on button 1 by placing them in an array. This is my code so far but does not work. Can anyone see what I'm doing wrong?

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Button[] btns = new Button[2];
        //Button[] btns = { button2}
        public Form1()
        {
            InitializeComponent();
            Button[] btns = { button2, button3};
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 2; i++)
            {
               // btns[i] = new Button();
               //btns[i].Enabled = true;

            }

        }
    }
}
1
  • 2
    please clarify "doesn't work". is there any error message? have you debugged your code? What have you tried? Commented Oct 25, 2012 at 18:34

2 Answers 2

4

The main problem in the code in the question is here:

public Form1()
{
    InitializeComponent();
    Button[] btns = { button2, button3};
}

The problem is that inside the Form1 constructor, btns is a local variable. Your code clearly assumes that you are referring to the member variable of the same name.

So you initialize the local variable, and then it immediately vanishes out of scope. In the rest of the code you refer to the member variable btns which has not initialised.

Solve the problem by initialising that member variable. You could do it like this:

public partial class Form1 : Form
{
    private Button[] btns;

    public Form1()
    {
        InitializeComponent();
        btns = new Button[] { button2, button3 };
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (var btn in btns)
        {
            btn.Enabled = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (var btn in btns)
        {
            if (!btn.Enabled)
            {
                btn.Enabled = true;
                return;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

this does not enable them one by one
Read your question again and look and see if it's obvious that you want them enabled one by one. No matter, I'll post an edit in two minutes that does that.
enable these 2 buttons in turn
@Tacit What does that comment mean? Did you read my update yet? You click on button1 and button2 becomes enabled. Then click on button1 again and this time button3 is enabled. I think you should edit the question to make it clear what you want. The "one by one" part is missing from your question. My code enables the buttons one by one.
3

Indexes are 0-based by default, not 1-based. This should work:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 2; i++)
    {
       btns[i].Enabled = true;
    } 
}

or use foreach:

private void button1_Click(object sender, EventArgs e)
{
    foreach(Button btn in btns)
    {
       btn.Enabled = true;
    } 
}

Also your array initialization should be:

Button[] btns = new [] { button2, button3};

2 Comments

i keep getting getting NullReferenceException was unhandled Object reference not set to an instance of an object.
@Tacit That's because you are initializing a local variable rather than the member variable, as I explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.