Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:10:15+00:00 2026-06-14T01:10:15+00:00

My program consists of 3 static buttons created using WinForms: button1, button2 and button3.

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:10:16+00:00Added an answer on June 14, 2026 at 1:10 am

    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;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

THIS IS HOMEWORK: I have a program that consists of two winforms and three
I am trying to program a java application that consists of several windows using
I'm having trouble with the linking of my files. Basically, my program consists of:
Suppose I have a big program that consists of hundreds of methods in it.
I'm currently trying to optimize my program. I have a large database which consists
I have a program which consists of multiple projects in eclipse (working under ubuntu
I'd like to use @Grape in my groovy program but my program consists of
I'm working with C# and .NET 4.0. My program consists of a front-end and
I am working on a program which consists of two parts Service component will
I'm creating a small program which consists of a few source files on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.