ZJY2369 2022-05-13 10:46 采纳率: 100%
浏览 78
已结题

对象引用对于非静态的字段、方法或属性是必须的所有fm2. () 都出现这个问题,form2里已经改成public了,求解


using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Outcome_2
{
    public partial class fm1 : Form
    {
        public fm1()
        {
            InitializeComponent();
        }
        private string[] arrname;
        private string[] arrscore;
        private int judgename, judgescore = 0;
        private int number = 0;
        private float[] arrnumber;
        float max = 0, min = 0,sum = 0,average = 0;
        private float over, under = 0;


        private void btnsave_Click(object sender, EventArgs e)
        {
            string result = (MessageBox.Show("Are you sure to enter the grade and name?", "Please Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)).ToString();
            if (result.Equals("OK"))
            {
                for (int i = 0; i < arrname.Length; i++)
                {
                    if (String.IsNullOrEmpty(txtname.Text))
                    {
                        lblnamehint.Text = "Name cannot be null";
                        lblscrhint.ForeColor = Color.Red;
                        judgename = 0;
                        break;
                    }
                    if (arrname[i] == txtname.Text)
                    {
                        lblnamehint.Text = "This name has already been entered";
                        lblnamehint.ForeColor = Color.Red;
                        judgename = 0;
                        break;
                    }
                    else
                    {
                        lblnamehint.Text = "√";
                        lblnamehint.ForeColor = Color.Green;
                        judgename = 1;
                    }
                }
            }
            string pattern_grade = @"^100(\.0*)?$|^\d{1,2}(\.[0-9]0*)?$|^\.[1-9]0*$";
            bool matchGrade = Regex.IsMatch(txtscore.Text, pattern_grade);
            if (matchGrade)
            {
                if (String.IsNullOrEmpty(txtscore.Text))
                {
                    lblscrhint.Text = "Score cannot be null";
                    lblscrhint.ForeColor = Color.Red;
                    judgescore = 0;
                }
                else
                {
                    lblscrhint.Text = "√";
                    lblscrhint.ForeColor = Color.Green;
                    judgescore = 1;
                }
            }
            else
            {
                lblscrhint.Text = "Please enter a score between 1 and 100";
                lblscrhint.ForeColor = Color.Red;
                judgescore = 0;
            }
            if (judgename == 1 && judgescore == 1)
            {
                arrname[number] = txtname.Text;
                arrscore[number] = txtscore.Text;
                fm2.rb.Text += txtname.Text + "     " + txtname.Text + "\n";
                number++;
                txtname.Text = "";
                txtscore.Text = "";
                for (int i = 0; i < number; i++)
                {
                    arrnumber[i] = float.Parse(arrscore[i].ToString());
                }
                max = arrnumber[0];
                min = arrnumber[0];
                sum = 0;
                for (int i = 0; i < number; i++)
                {

                    if (max < arrnumber[i])
                    {
                        max = arrnumber[i];
                    }

                    if (min > arrnumber[i])
                    {
                        min = arrnumber[i];
                    }

                    sum += arrnumber[i];

                    average = sum / number;

                }
                for (int i = 0; i < number; i++)
                {
                    if (arrnumber[i] >= average){
                        over++;
                    }
                    if (arrnumber[i] < average){
                        under++;
                    }
                }
                judgename = 0;
                judgescore = 0;
            }
            fm2.lblMax.Text = max.ToString();
            fm2.lblMin.Text = min.ToString();
            fm2.lblAvg.Text = average.ToString();
            fm2.lblSum.Text = sum.ToString();
        }

        private void fm1_Load(object sender, EventArgs e)
        {
            lbltime.Text = System.DateTime.Now.ToString();
            lbltime.Enabled = true;
            lblnamehint.Text = "Please input a name";
            lblscrhint.Text = "Please enter a score between 1~100";
            arrname = new string[200];
            arrscore = new string[200];
            arrnumber = new float[200];

        }

        private void Time_Tick(object sender, EventArgs e)
        {
            this.lbltime.Text = DateTime.Now.ToString();
        }

        private void fmdata(object sender, EventArgs e)
        {
            fm2 f2 = new fm2();
            f2.Show();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to quit?","Warning",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

       
    }
}

 

  • 写回答

2条回答 默认 最新

  • 於黾 2022-05-13 10:55
    关注

    fm2是类型
    f2才是它派生出来的实例
    你后续都应该是f2.属性,而不是fm2.属性

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月13日
  • 已采纳回答 5月13日
  • 修改了问题 5月13日
  • 赞助了问题酬金5元 5月13日
  • 展开全部

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。