gpt4 book ai didi

c# - 在带有按钮和文本框的表单中运行几个错误

转载 作者:行者123 更新时间:2023-11-30 22:13:59 25 4
gpt4 key购买 nike

这是我的代码:

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 FJBch09ex10
{


public partial class Form1 : Form
{
Random r = new Random();
int num = r.Next(0, 100);
int counter = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btn1_Click(object sender, EventArgs e)
{
counter++;

if (int.Parse(txt1.Text) > num)
{
lbl1.Text = "the number is too high";
Form1.BackColor = HotTrack;


}
else {
lbl1.Text = "the number is too low";
Form1.BackColor = MenuHighlight;
}

}


}
}

目前r.Next中的r运行出错。 Form1.BackColor 尝试也都是运行错误。知道为什么这些运行错误吗? r 表示“字段初始值设定项不能引用非静态字段...”Form1 表示“非静态字段需要对象引用...”

最佳答案

public partial class Form1 : Form
{
Random r = new Random();
int num; // you cannot use r there
int counter = 0;

public Form1()
{
InitializeComponent();
num = r.Next(0, 100); // you can use r there
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btn1_Click(object sender, EventArgs e)
{
counter++;

if (int.Parse(txt1.Text) > num)
{
lbl1.Text = "the number is too high";
BackColor = SystemColors.HotTrack; // Form1 is the class


}
else {
lbl1.Text = "the number is too low";
BackColor = SystemColors.MenuHighlight; // Form1 is the class
}

}
}

关于c# - 在带有按钮和文本框的表单中运行几个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18726257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com