ListView - potatoscript/csharp GitHub Wiki

home

Index Remark
■ Hide ListView Column Header Set the HeaderStyle member of the LlistView to None
ListView Items Add with Array
ListViewEx In-place editing of ListView subitems
ListView Cell's Height Use SmallImageList to set the height
ListView Data Passing
FindItemWidthText
ListView Image File list
ListView Multipe Select
ListView SubItems Style
Sorting ListView Item Sort Ascending and Descending

home

ListView-Array-Data

   Index.listView1.Items.Clear();
   var sql = "SELECT "+Index.fiels+" FROM TABLE";
   var = 0;
   foreach(char c in Index.fields)
   {
      if(c==',') n++;
   }
   var pims = new Get(Pims.database(), "myDatabase", sql);
   if(pims.data.HasRows)
   {
      while(pims.data.Read())
      {
         string[] s = new string[n+1];
         for(var i=0; i<=n; i++)
         {
            s[i] = pims.data[i].ToString();
         }
         Index.listView1.Items.Add(new ListViewItem(s));
      }
   }
   pims.connection.Close();

home

ListViewEx

  • Step 1: Add ListViewEx.dll to reference
  • Step 2: Add ListViewEx to toolbox
  • Step 3: Create ListViewEx and control-box and set the control-box invisible in the beginning
  • Step 4: Create SubItemClick Event Handling to show the keypad popup window
   private void listViewEx1_SubItemClicked(object render, ListViewEx.SubItemEventArgs e)
   {
      if(e.SubItem == 2 )
      {
         listViewEx1.startEditing(control-box, e.Item, e.SubItem);
         Form1 frm = (Form1)this.Owner;
         // the following is to show the keypad popup window from the keypadListView class at the control-box location
         if(frm.chk.key.Checked==true)
         {
             keypadListView board = new keypadListView("control-box");
             board.Owner = this;
             board.StartPosition = FormStartPosition.Manual;
             board.SetDesktopLocation(Cursor.Position.X-50, Cursor.Position.Y+15);
             board.ShowDialog();
         }
      }
   }
  • Step 5: Add constructor below the InitializeComponent();
   listViewEx1.SubItemClicked += new ListViewEx.SubItemEventHandler(listViewEx1_SubItemClicked);
  • Step 6: End the Editing on press [CLOSE] key button on the keypad
   private void btnCloseClick(object sender, EventArgs e)
   {
      this.Close();
      // Popup1 is the parent form of the ListViewEx1
      Popup1 frm = (Popup1)this.Owner;
      frm.listViewEx1.EndEditing(true);
   }

home

ListView-Cell-Height

  • add imageList
  • add image member tto the imageList -refer the imageList to the smallImageList

home

ListView-Data-Passing

   foreach(object item in frm2.listView1.Items)
   {
      listViewfrm1.Items.Add( 
          new ListViewItem( 
              new string[] {
                   frm2.listView1.Items[i].SubItems[0].Text,
                   frm2.listView1.Items[i].SubItems[1].Text,
                   frm2.listView1.Items[i].SubItems[2].Text 
      });     
   }

home

FindItemWIdthText

var item = listView.process.FindItemWithText(value);
if(item != null) item.Selected = true;

home

ListView-Image-File

   private void ListView1.MouseMove(object sender, MouseEventArgs e)
   {
      try
      {
         ListViewItem itemindex = new ListViewItem();
         itemindex = ListView1.GetItemAt(e.X, e.Y);
         ListViewItem item = ListView1.Items[0];
         try
         {
             item = ListView1.Items[itemindex.Index];
         }
         catch(Exception)
         { 
             item = ListView1.Items[0];
         }
         var dir = host.dir + @"c:\Images\";
         try
         {
            char[] sc = {'-'};
            var img = item.SubItems[1].Text.Split(sc, System.StringSplitOptionsRemoveEmptyEntries);
            Bitmap bitmap = (Bitmap)Image.FromFile(dir+img[0]+"-"+img[1].substring(0,1)+".jpg",true);
            
            vTooltip t =  new vTooltip();
            t.itemimage.Image = bitmap;
            t.Text = item.SubItems[1].Text.Trim();
            t.itemcode.Text = item.SubItems[1].Text.Trim();
            t.StartPosition = FormStartPosition.Manual;
            t.SetDesktopLocation(Cursor.Postion.X-300, Cursor.Position.Y-100);
            t.Show();
         }
         catch(Exception)
         {
            t.Hide();
         }
      }
      catch(Exception)
     { 
        t.Hide();
     }
   }

home

ListView-Multiple-Select

   private void listView1_SubItemClick(object sender, SubItemEventArgs e)
   {
      var indices = listView1.SelectedIndices[0];
      listViewItem item = listView1.Items[indices];
      Index frm = (Index) this.Owner;
      if(e.SubItem==0)
      {
         char[] sc = {'★'};
         if(listView1.Items[item.Index].SubItems[1].Text.Contains("★") == false)
         {
            var s = listView1.Items[item.Index].SubItem[1].Text.Split(sc,System.StringSplitOptions.RemoveEmptyEntries); 
            listView1.Items[item.Index].SubItems[1].Text = "★"+s[1].ToString();
            listView_select_item.Items.Add(
                new ListViewItem(new string[] {
                     item.SubItems[0].Text.Trim(),
                     item.SubItems[1].Text
            }));
         }
         else
         {
             for(int i =listView_select_item.Items.Count-1; i>=0;i--)
             {
                if(listView_select_item.Items[i].SubItems[0].Text == listView1.Items[item.Index].SubItems[0].Text)
                {
                    listView_select_item.Items[i].Remove();
                }
             }
             var s = listView1.Items[item.Index].SubItem[1].Text.Split(sc,System.StringSplitOptions.RemoveEmptyEntries);
             listView1.Items[item.Index].SubItems[1].Text = s[0].ToString();
         }
      }
   }

home

UseItemStyleForSubItems

  • To change the style of the listView item you have to set the default UseItemStyleForSubItems to false
   listView1.Items[i].UseItemStyleForSubItems = false;
   listView1.Items[i].SubItemss[1].ForeColor = Color.White;

home

Sorting

  • Step 1: Handling the ColumnClick Event and trigger to sort descending and ascending
   private int sortColumn = -1;
   private void ListView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
   {
      if(e.Column != sortColumn)
      {
          sortColumn = e.Column;
          ListView1.Sorting = SortOrder.Descending;
      }
      else
      {
         if(ListView1.Sorting == SortOrder.Descending)
         {
             ListView1.Sorting = SortOrder.Ascending;
         }
         else
         {
            ListView1.Sorting = SortOrder.Descending;
         }
      }
      ListView1.Sort();
      this.ListView1.ListViewItemSorter = new ListViewItemComparer(e.Column, ListView1.Sorting);
   }
  • Step 2 : Connect the event handling method to the ListView control
    by adding code to the constructor of the form
   this.ListView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.ListView1.ColumnClick);
  • Step 3 : Creating the ListViewItemComparer Class
   class ListViewItemComparer : IComparer
   {
      private int col;
      private SortOrder order;
      public ListViewItemComparer()
      {
         col = 0;
         order = SortOrder.Ascending;
      }
      public ListViewItemComparer(int column, SortOrder order)
      {
          col = column;
          this.order = order;
      }
      public int Compare(object x, object y)
      {
         int returnVal = -1;
         returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
         
         if(order==SortOrder.Descending)
         {
             returnVal *= -1;
         }
         return returnVal;
      }
   }