1

我有一个平均计算器 Windows 窗体应用程序,它询问用户他们想输入多少分数。一个文本框接受输入,一旦他们单击“确定”,他们输入的数字就会成为他们输入分数的文本框。填充文本框后,他们可以单击计算来计算显示给他们的平均值。

我的问题是,当我在文本框中输入一个数字并单击“确定”时,什么也没有发生。

请帮忙,这是我的代码。

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();
    }

}

}

4

3 回答 3

1

你确定你的事件绑定到按钮的正确事件(在属性/事件中验证)它有时会发生在我身上,白痴错误

于 2013-04-18T17:30:02.653 回答
0

将此代码添加到表单的构造函数中(在 InitializeComponent 调用之后):

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

现在,当您运行应用程序并按 btnOK 时,应该会弹出消息“Foo”,将消息片段替换为您的代码,您应该已经准备好了。

于 2013-04-18T17:56:55.843 回答
0

有过这样的案子。结果发现,当我按 F5 运行时,“重建”失败了,而我没有看到它。由于我创建它时,该按钮显示在表单上。当我添加第二个按钮来帮助找出问题时,它没有显示。当我尝试重建项目时,我看到了错误。没有询问“构建失败,运行最后一次成功构建”的问题。

于 2014-01-20T16:35:22.343 回答