简体   繁体   English

如何将项目添加到ListView

[英]how can I add items to my ListView

I keep getting this error and I know why but I need help figuring out how I can solve it. 我一直收到这个错误,我知道为什么,但我需要帮助弄清楚如何解决它。 The only way I have been able to add my items it to make a new form but that seems silly. 我能够添加我的项目以制作新表单的唯一方法,但这看起来很愚蠢。

It wont work if I make all my methods static =( 如果我将所有方法都设为静态,它就无法工作=(

I keep getting, 我一直在,

"An object reference is required for the non-static field, method, or property 'Handicap_Calculator.FormMain.listViewLog' \\Form1.cs 74 13 Handicap Calculator" “非静态字段,方法或属性需要对象引用'Handicap_Calculator.FormMain.listViewLog'\\ Form1.cs 74 13 Handicap Calculator”

Here´s my code: 这是我的代码:

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

    }

}
}

Edit... 编辑...

Here is the class im calling it from: 这是我从中调用它的类:

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

The simple solution is to define your methods AddScore and AddToList as non-static. 简单的解决方案是将方法AddScoreAddToList定义为非静态方法。

public void AddScore()
{
    //your code
}

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

If you want to use static methods you should pass the instance of your Form to the method, on which you want to add items to the ListView. 如果要使用静态方法,则应将表单实例传递给要在ListView上添加项目的方法。

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

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

Update: 更新:

According to your updated code the solution is to pass the instance of your FormMain to your FormAddScore when you create it. 根据您更新的代码,解决方案是在您创建FormAddScore时将FormMain实例传递给FormAddScore。 In FormAddScore you store the reference to the FormMain instance to call the methods on. 在FormAddScore中,您存储对FormMain实例的引用以调用方法。

public partial class FormAddScore : Form
{
  // your code

  private FormMain _mainForm;

  public FormAddScore(){
    InitializeComponent();

  }

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

In your FormMain when you create the instance of FormAddScore you should use the constructor that expects an instance of FormMain 在FormMain中创建FormAddScore实例时,您应该使用需要FormMain实例的构造函数

 _FormAddScore = new FormAddScore(this);

With this setup you can change your methods to non-static and you can call the methods of FormMain in your FormAddScore, by using the stored reference in variable _mainForm. 使用此设置,您可以将方法更改为非静态,并且可以使用变量_mainForm中存储的引用在FormAddScore中调用FormMain的方法。

 _mainForm.AddScore();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM