如何将项目添加到ListView

时间:2012-10-23 06:29:05

标签: c# winforms listview reference

我一直收到这个错误,我知道为什么,但我需要帮助弄清楚如何解决它。我能够添加我的项目以制作新表格的唯一方法,但这看起来很傻。

如果我将所有方法设为静态=(

我一直在,

  

“非静态字段,方法或者需要对象引用   属性'Handicap_Calculator.FormMain.listViewLog'   \ Form1.cs 74 13让分计算器“

这是我的代码:

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();
    }
}
}

1 个答案:

答案 0 :(得分:2)

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

public void AddScore()
{
    //your code
}

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

如果要使用静态方法,则应将表单实例传递给要在ListView上添加项目的方法。

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

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

<强>更新

根据您更新的代码,解决方案是在您创建FormAddScore时将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();