从类中填写列表视图 [英] Fill a listview from within a class

查看:56
本文介绍了从类中填写列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,首先我想说我只是在学习c#。谷歌是我的朋友找到了我寻求的很多答案,这次我找不到答案。



我的表格中有通常的代码来加载listview。



Hi everyone, first I would like to say that I am just learning c#. Google is my friend in finding a lot of the answers that I seek, this time I cannot find the answer.

I have the usual code in my form to load a listview.

// main form
ListViewItem lView = new ListViewItem();
lView.Text = "Testing";
lView.SubItems.Add(Price);
lView.SubItems.Add(Manufacturer);
lView.SubItems.Add(InStock);
listView1.Items.Add(lView);





我想把它移到一个类,因为它会减少我的主程序中有很多代码。我的问题是我不知道如何在课堂上填写listview。我真的不知道如何指向列表视图,希望有人可以帮助我在这里。



这只是一个简化帖子的片段。 />




I want to move this to a class as it will cut down on a lot of code in my main program. My problem is I don't know how to fill the listview from within the class. I really don't know how to point to the listview, hope that someone can help me out here.

This is just a snippet to make posting simpler.

// class file
public void LoadListView(frmMain mainForm, string manufacturer, string Price, string InStock)
{
    ListViewItem lView = new ListViewItem();
    lView.Text = "Testing";
    lView.SubItems.Add(Price);
    lView.SubItems.Add(Manufacturer);
    lView.SubItems.Add(InStock);
    frmMain.listView1.Items.Add(lView);  // (right here the program doesn't know what ListView1 is)
}

// calling the class
WorkerClass workerClass = new WorkerClass();
workerClass.LoadListView(....);







感谢任何帮助。




Appreciate any help.

推荐答案

listview只是一个包含实例的变量,所以你可以像任何其他变量一样将它传递给你的类:

A listview is just a variable containing an instance, so you can pass it through to your class like any other variable:
public class MyClass
   {
   ...
   public static void FillIt(ListView lv)
      {
      lv.Items.Add(...);
      ...
      }
   }

   ...
   MyClass.FillIt(listView1);
   ...





BTW:帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得TextBox8是今天的手机号码,但是当你需要修改它的时候是三周时间,那你呢?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......



BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it is three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


这篇关于从类中填写列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆