Visual C#尝试在标签中显示值
我试图在Visual C#中获取某些数值,以显示在名为“lblOutput”的标签中 但是,每次我按下按钮显示数值时,程序都会冻结 我是否使用正确的语法将值显示到标签上 包括以下代码:Visual C#尝试在标签中显示值,c#,winforms,C#,Winforms,我试图在Visual C#中获取某些数值,以显示在名为“lblOutput”的标签中 但是,每次我按下按钮显示数值时,程序都会冻结 我是否使用正确的语法将值显示到标签上 包括以下代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; u
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TableOfSquaresGUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
const int MAX = 20;
double number = 1;
double numberSquared;
while (number <= MAX)
{
numberSquared = Math.Pow(number, 4);
lblOutput.Text = String.Format("{0} is there entered number
and the value squared is {1}", number, numberSquared);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
squaresgui的名称空间表
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效btnCalc_单击(对象发送方,事件参数e)
{
常数int MAX=20;
双数=1;
双数平方;
而(number嗯,number
总是小于MAX
,因此它将进入无休止的循环。因此,您的程序将冻结
像这样改变:
while (number <= MAX)
{
numberSquared = Math.Pow(number, 4);
lblOutput.Text = String.Format("{0} is there entered number and the value squared is {1}", number, numberSquared);
number++; //add this line
}
选项2(推荐):
尝试为int变量编写«ToString()»
您可以按照$替换«String.Format»
例子
嗯,数字总是小于MAX
,因此它将进入无休止的循环。因此您的程序冻结。谢谢,但现在它只显示一个值。有没有办法让它显示1-20平方的值?您需要继续将文本附加到lblOutput.text
。另一个更好的选择是将使用RichTextBox而不是label。然后使用RichTextBox1.AppendText(String.Format(..)+“\n”);
lblOutput.Text += String.Format("{0} is there entered number and the value squared is {1}", number, numberSquared);
richTextBox.AppendText(String.Format("{0} is there entered number and the value squared is {1}", number, numberSquared) + "\n");
lblOutput.Text = $"{number.ToString()} is there entered number
and the value squared is {numberSquared.ToString()}");