user2296209
user2296209

Reputation:

The button on my Windows form isn't working. Visual studio is not showing any errors in my code

I have a average calculator Windows Form application that asks the user how many scores they would like to enter. A textbox accepts the input and once they click OK, the number they entered becomes textboxes for them to enter scores into. Once the textboxes are populated, they can click calculate to calculate the average which is displayed to them.

My problem is, when I enter a number in the textbox, and click OK, nothing happens.

Please help, here is my code.

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 AverageCalculator
{
public partial class AverageCalculator : Form
{
    //Private instance variable, Scores, which is an array of TextBox objects.
    //Do not instantiate the array.
    private TextBox[] Scores;

    // Create a Form_Load event hadler
    private void Form_Load(object sender, EventArgs e)
   {
        // Set the visible property of btnCalculate to false
        btnCalculate.Visible = false;
    } // end Form_Load event handler

    //Create the Click event handler for the OK button
    private void btnOK_Click(object sender, EventArgs e)
    {
        // Declare an integer, intNumTextBoxes
       int intNumTextBoxes;

        //Store the number of text boxes to be created into the integer
        intNumTextBoxes = Convert.ToInt32(txtNumScores.Text);

        // Set the height of the form to 500 pixels
        this.Height = 500;

        // Set the visible property of btnCalculate to true
        btnCalculate.Visible = true;

        // Set the enabled property of btnOK to false
        btnOK.Enabled = false;

        // Call CreateTextBoxes and pass it the integer, intNumTextBoxes as a parameter
        CreateTextBoxes(intNumTextBoxes);
    }

    // CreateTextBoxes method
    private void CreateTextBoxes(int number)
    {
        //Instantiate the Scores array and use the parameter "number" as the size of the array
        Scores = new TextBox[number];

        //Declare an integer, intTop, with an initial value of 150
        int intTop = 150;

        //Loop through the entire array, Scores
        for (int i = 0; i < Scores.Length; i++)
        {
            // Instantiate a new TextBox using the default
            // constructor and assign it to the current element 
            // of the Scores array
            Scores[i] = new TextBox();

            // Set the left property of the TextBox to 20.
            Scores[i].Left = 20;

            // Set the Top property of the Textbox to intTop
            Scores[i].Top = intTop;

            // Increment intTop by 50
            intTop += 50;

            // Ad the TextBox to the controls collection of the Form
            this.Controls.Add(Scores[i]);
        } // end for loop
    } // end CreateTextBoxes method

    // Create the Click event handler for the btnCalculate button
    private void btnCalculate_Click(object sender, EventArgs e)
    {

    }
    public AverageCalculator()
    {
        InitializeComponent();
    }

}

}

Upvotes: 1

Views: 6378

Answers (3)

CW1255
CW1255

Reputation: 121

Had a case like that. Turned out the 'rebuild' was failing when I was hitting F5 to run, and I wasn't seeing it. The button was showing on the form due to when I created it. When I added a second button to help figure out the problem, it wasn't displaying. When I tried just rebuilding the project, I saw the error. The 'Build failed, run last successful build' question wasn't getting asked.

Upvotes: 0

Ale Miralles
Ale Miralles

Reputation: 604

Add this code into the form's constructor (after the InitializeComponent call):

btnOK.Click +=(s,e)=>{MessageBox.Show("Foo");};

Now when you run the app and press btnOK the message "Foo" should popup, replace the message snippet with your code, and you should be pretty much set.

Upvotes: 0

NamedJohnny
NamedJohnny

Reputation: 76

are you sure your event is bind to the correct event of the button (verify in Properties/Event) it happens to me sometime, idiot mistake

Upvotes: 1

Related Questions