array of ListViewItem - how to do it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • grs

    array of ListViewItem - how to do it

    The following is a code example from the Microsoft MSDN.
    My question is on the following three lines of code:

    ListViewItem item1 = new ListViewItem("i tem1",0);
    ListViewItem item2 = new ListViewItem("i tem2",1);
    ListViewItem item3 = new ListViewItem("i tem3",0);

    You would in most cases know the ListViewItem name or the number of items to
    create (item1,item2 etc).
    How would you create this individual rows programmaticall y ?

    thanks
    grs


    =============== =============== =======
    private void CreateMyListVie w()
    {
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bound s = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.Label Edit = true;
    // Allow the user to rearrange columns.
    listView1.Allow ColumnReorder = true;
    // Display check boxes.
    listView1.Check Boxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullR owSelect = true;
    // Display grid lines.
    listView1.GridL ines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorti ng = SortOrder.Ascen ding;

    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("i tem1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems. Add("1");
    item1.SubItems. Add("2");
    item1.SubItems. Add("3");
    ListViewItem item2 = new ListViewItem("i tem2",1);
    item2.SubItems. Add("4");
    item2.SubItems. Add("5");
    item2.SubItems. Add("6");
    ListViewItem item3 = new ListViewItem("i tem3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems. Add("7");
    item3.SubItems. Add("8");
    item3.SubItems. Add("9");

    // Create columns for the items and subitems.
    listView1.Colum ns.Add("Item Column", -2, HorizontalAlign ment.Left);
    listView1.Colum ns.Add("Column 2", -2, HorizontalAlign ment.Left);
    listView1.Colum ns.Add("Column 3", -2, HorizontalAlign ment.Left);
    listView1.Colum ns.Add("Column 4", -2, HorizontalAlign ment.Center);

    //Add the items to the ListView.
    listView1.Items .AddRange(new ListViewItem[]{item1,item2,it em3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall. Images.Add(Bitm ap.FromFile("C: \\MySmallImage1 .bmp"));
    imageListSmall. Images.Add(Bitm ap.FromFile("C: \\MySmallImage2 .bmp"));
    imageListLarge. Images.Add(Bitm ap.FromFile("C: \\MyLargeImage1 .bmp"));
    imageListLarge. Images.Add(Bitm ap.FromFile("C: \\MyLargeImage2 .bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.Large ImageList = imageListLarge;
    listView1.Small ImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.A dd(listView1);
    }


  • Jeffrey Tan[MSFT]

    #2
    RE: array of ListViewItem - how to do it

    Hi grs,

    Thanks for your post.

    I do not think I understand you question. What does "How would you create
    this individual rows programmaticall y ?" mean? In your code, I see that you
    have added the listviewitems into ListView with listView1.Items .AddRange
    method.

    Can you show me your concern more concrete?

    Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    Comment

    • grs

      #3
      RE: array of ListViewItem - how to do it

      "Jeffrey Tan[MSFT]" wrote:
      Hi Jeffrey,

      The problem is the hardcoded "item1". You do not know how many
      items there will be so you can not create an ListViewItem object
      in advance. I do not think you would want to to a create on
      item1,item2, etc and restrict the number of ListViewItems
      [color=blue]
      >My question is on the following three lines of code:
      >ListViewItem item1 = new ListViewItem("i tem1",0);
      >---------------^(*) here is the problem
      >ListViewItem item2 = new ListViewItem("i tem2",1);[/color]

      So the recap of my problem is that I do not know the number of rows that
      exists how do I create the ListViewItems (which unless I am wrong
      correspond to rows)

      thanks
      grs

      grs

      --
      Sent via .NET Newsgroups

      Comment

      • Marcus Andrén

        #4
        Re: array of ListViewItem - how to do it

        On Mon, 31 Oct 2005 17:15:10 -0600, "grs" <gsmith@budgete xt.com>
        wrote:
        [color=blue]
        >You would in most cases know the ListViewItem name or the number of items to
        >create (item1,item2 etc).
        >How would you create this individual rows programmaticall y ?
        >[/color]

        First of all, it is usually best to seperate the code for creating the
        ListView from the code adding the ListViewItems.

        Here is the code I usually use to add ListViewItems. Assuming you have
        a collection MyDataClassColl ection that contains a number of
        MyDataClass instances that can be used to generate ListViewItems.:

        private void RefreshListView ()
        {
        listView.BeginU pdate();

        listView.Items. Clear();
        AddItemsToListV iew();

        listView.EndUpd ate();
        }


        private void AddItemsToListV iew()
        {
        //Add to an ArrayList first because AddRange is a lot faster
        //than Add when dealing with lots of elements.
        //Also, there is no need to turn off the sorter when adding
        //all the elements at once

        ArrayList listItems = new ArrayList();

        foreach (MyDataClass myDataItem in MyDataClassColl ection)
        {
        // The if statement can be removed if all items in
        // MyDataClassColl ection should be added to the ListView.
        if( ShouldDisplay(m yDataItem) )
        listItems.Add(C reateListViewIt em(myDataItem)) ;
        }

        // Adds the items to the ListView
        listView.Items. AddRange(
        (ListViewItem[]) listItems.ToArr ay( typeof(ListView Item)));
        }

        // Generate a listviewitem by using the myDataItem instance.
        private static ListViewItem CreateEpisodeLi stViewItem(MyDa taClass
        myDataItem)
        {
        ListViewItem item = new ListViewItem(
        new string[]
        {
        myDataItem.Stri ngProperty,
        myDataItem.IntP roperty.ToStrin g(),
        (myDataItem.Ref erenceProperty == null) ? "" :
        myDataItem.Refe renceProperty.T oString()
        });
        item.ForeColor = CalculateForeCo lor(myDataItem) ;

        // It is usually a good idea to use the Tag property to attach
        // the MyDataClass instance directly to the ListViewItem.
        item.Tag = episode;

        return item;
        }

        --
        Marcus Andrén


        Comment

        • Jeffrey Tan[MSFT]

          #5
          RE: array of ListViewItem - how to do it

          Hi grs,

          Thanks for your feedback.

          Sorry, but I still do not understand you very well. I am not sure the
          "item1" you mentioned refers to the ListViewItem reference or the first
          parameter passed to ListViewItem constructor? Can you clarify this to me?
          If you referred to the ListViewItem reference, I do not think this is a
          problem, we can reuse the same reference to create many ListViewItem
          instances, then add them into ListView.Items collection. The reference need
          not correspond to ListViewItem instance one-by-one.
          If you referred to the first parameter passed to ListViewItem, I just do
          not understand why. This parameter identifies the text to be displayed in
          ListView, if you want to add a new item, you SHOULD know which text you
          want to display.

          In normal situation, once we know the text we want to display in ListView,
          we can construct a new ListViewItem instance and add it into ListView.Items
          collection, no matter how many items you want to add, only one ListViewItem
          reference is needed.

          If I misunderstand you, please feel free to tell me, thanks

          Best regards,
          Jeffrey Tan
          Microsoft Online Partner Support
          Get Secure! - www.microsoft.com/security
          This posting is provided "as is" with no warranties and confers no rights.

          Comment

          • grs

            #6
            RE: array of ListViewItem - how to do it

            Jeffrey,

            Your statement below cleared everything up for me. I seem to have a
            problem with reusing references, I have had this mind road-block before.
            Maybe someday I will get it.

            thanks for turning me around.
            grs
            [color=blue]
            >we can reuse the same reference to create many ListViewItem
            >instances, then add them into ListView.Items collection[/color]

            grs

            --
            Sent via .NET Newsgroups

            Comment

            • Jeffrey Tan[MSFT]

              #7
              RE: array of ListViewItem - how to do it

              I am glad my reply can help you. If you need further help, please feel free
              to tell me, thanks

              Best regards,
              Jeffrey Tan
              Microsoft Online Partner Support
              Get Secure! - www.microsoft.com/security
              This posting is provided "as is" with no warranties and confers no rights.

              Comment

              Working...