0

我不断收到此错误,我知道为什么,但我需要帮助弄清楚如何解决它。我能够添加我的项目以制作新表格的唯一方法,但这似乎很愚蠢。

如果我将所有方法都设为静态,它将无法工作 =(

我不断得到,

“非静态字段、方法或属性‘Handicap_Calculator.FormMain.listViewLog’\Form1.cs 74 13 Handicap Calculator 需要对象引用”

这是我的代码:

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

public partial class FormMain : Form
{
    //FormAddScore FormAddNewScore = new FormAddScore();
    public static bool addScoreIsShown = false;
    public static FormAddScore _FormAddScore;
    public static ListViewItem Item;
    //public static List<string> ScoreInfo = new List<string>();

    public FormMain()
    {
        InitializeComponent();

    }

    public void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (_FormAddScore == null || _FormAddScore.IsDisposed)
            {
                _FormAddScore = new FormAddScore();
            }
            _FormAddScore.Show();
            if (_FormAddScore.WindowState == FormWindowState.Minimized)
            {
                _FormAddScore.WindowState = FormWindowState.Normal;
            }
            _FormAddScore.BringToFront();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    public static void AddScore()
    {
        int Round = 1;
        DateTime date = _FormAddScore.date;
        string course = _FormAddScore.course;
        int holes = _FormAddScore.holes;
        int score = _FormAddScore.score;
        float courseRating = _FormAddScore.courseRating;
        float slopeRating = _FormAddScore.slopeRating;

        string[] ScoreInfo = new string[7];
        ScoreInfo[0] = Round.ToString();
        ScoreInfo[1] = date.ToString();
        ScoreInfo[2] = course;
        ScoreInfo[3] = holes.ToString();
        ScoreInfo[4] = score.ToString();
        ScoreInfo[5] = courseRating.ToString();
        ScoreInfo[6] = slopeRating.ToString();
        AddToList(ScoreInfo);

    }

    public static void AddToList(string[] ScoreInfo)
    {
        Item = new ListViewItem(ScoreInfo);            
        //listViewLog.Items.Add(Item);      

    }

}
}

编辑...

这是我调用它的类:

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

public partial class FormAddScore : Form
{
    public DateTime date;
    public string course;
    public int holes;
    public int score;
    public float courseRating;
    public float slopeRating;

    public FormAddScore()
    {
        InitializeComponent();

    }

    private void FormAddScore_FormClosing(object sender, FormClosingEventArgs e)
    {
        FormMain.addScoreIsShown = false;
    }

    public void  getscore()
    {
        try
        {
            date = dateTimePicker1.Value;
            course = textBoxCourse.Text;
            holes = Convert.ToInt16(textBoxHoles.Text);
            score = Convert.ToInt16(textBoxScore.Text);
            courseRating = Convert.ToSingle(textBoxCourseRating.Text);
            slopeRating = Convert.ToSingle(textBoxSlopeRating.Text);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        getscore();            
        FormMain.AddScore();
    }
}
}
4

1 回答 1

2

AddScore简单的解决方案是将您的方法定义AddToList为非静态的。

public void AddScore()
{
    //your code
}

public void AddToList(string[] ScoreInfo)
{
    // your code
}

如果你想使用静态方法,你应该将你的 Form 实例传递给你想要在 ListView 上添加项目的方法。

public static void AddScore(FormMain mainForm)
{
    //your code
    AddToList(mainForm, ScoreInfo);
}

public static void AddToList(FormMain mainForm, string[] ScoreInfo)
{
    // your code
}

更新:

根据您更新的代码,解决方案是在创建时将 FormMain 的实例传递给您的 FormAddScore。在 FormAddScore 中,您存储对 FormMain 实例的引用以调用方法。

public partial class FormAddScore : Form
{
  // your code

  private FormMain _mainForm;

  public FormAddScore(){
    InitializeComponent();

  }

  public FormAddScore(FormMain mainForm) : this(){
    _mainForm = mainForm;
  }

在您的 FormMain 中,当您创建 FormAddScore 的实例时,您应该使用需要 FormMain 实例的构造函数

 _FormAddScore = new FormAddScore(this);

通过此设置,您可以将方法更改为非静态方法,并且可以使用存储在变量 _mainForm 中的引用在 FormAddScore 中调用 FormMain 的方法。

 _mainForm.AddScore();
于 2012-10-23T06:32:03.427 回答