我的Windows窗体上的按钮不工作.Visual studio在我的代码中没有显示任何错误
本文关键字:我的 代码 错误 任何 显示 Visual 窗体 Windows 按钮 工作 studio | 更新日期: 2023-09-27 18:09:59
我有一个平均计算器Windows窗体应用程序,询问用户他们想要输入多少分数。一个文本框接受输入,一旦他们点击OK,他们输入的数字就会变成文本框,供他们在其中输入分数。一旦文本框被填充,他们可以点击计算来计算显示给他们的平均值。
我的问题是,当我在文本框中输入一个数字,然后点击OK,没有任何反应。
请帮忙,这是我的代码。
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();
}
}
}
你确定你的事件绑定到按钮的正确事件(在属性/事件中验证)它有时发生在我身上,白痴的错误
将以下代码添加到表单的构造函数中(在InitializeComponent调用之后):
btnOK.Click +=(s,e)=>{MessageBox.Show("Foo");};
现在,当你运行应用程序并按btnOK时,消息"Foo"应该弹出,用你的代码替换消息片段,你应该差不多设置好了。
遇到过这样的情况。结果,当我按下F5运行时,"重建"失败了,我没有看到它。按钮显示在表单上,由于当我创建它。当我添加第二个按钮来帮助解决问题时,它没有显示出来。当我尝试重新构建项目时,我看到了错误。没有询问"构建失败,运行上次成功构建"问题。