Click here to Skip to main content
6,594,432 members and growing! (16,247 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Save ListView settings

By Guy Baseke

How to save ListView settings.
C#, VC7.NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:19 May 2004
Views:46,036
Bookmarked:31 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.77 Rating: 3.95 out of 5
1 vote, 11.1%
1

2

3
3 votes, 33.3%
4
5 votes, 55.6%
5

Sample Image - ListviewSettings.jpg

Introduction

Most of the time, it is important to persist the width of columns in a list view. And this can be done easily. But sometimes you may need to persist, the order, whether a column is a required column, and whether the column should be displayed or not.

The Question

The order and the width are already provided, but whether a column is required or should be displayed is the reason of this article, and my question: "How do I persist these values?".

The Answer

The ListView in itself does not know anything about this information. So, I decided to implement a ListView Settings dialog. Columns information is stored to the registry in the order that they should be displayed.

Steps that require attention in the ListView Settings dialog are:

  1. Add columns to the dialog.
        private void AddDonorCols()
        {
            reg.ReadDonorSettings();
            foreach( MyColumns col in reg.DonorCols)
            {
                ListViewItem it =  listView1.Items.Add(col.name);
                it.Checked = col.show;
            }
            listView1.Focus();
            listView1.Items[0].Selected = true;
        }

    Read columns information from the registry and initialize list with information.

  2. Moving items up or down.
        private void SwapElements(int idx)
        {
            listView1.BeginUpdate();
            ListViewItem it1 = listView1.Items[idx];
            ListViewItem it2 = listView1.Items[idx -1];
            listView1.Items.RemoveAt(idx);
            listView1.Items.RemoveAt(idx - 1);
            listView1.Items.Insert(idx-1,it1);
            listView1.Items.Insert(idx,it2);
            SwapCols(ref reg.m_DonorCols[idx], ref reg.m_DonorCols[idx - 1]);
            // Needed to Update Width value properly.
    
            OnSelectedIndexChanged(null, null);
            listView1.Focus();
            listView1.EndUpdate();
        }

    This function is used by the Move up and Move Down buttons. When moved down, the index (idx) of the element being moved is the index of the next element in the list. When moved up, the index (idx) of the element being moved is passed to the function.

  3. Check box is clicked.
        private void OnItemChecked(object sender, 
                System.Windows.Forms.ItemCheckEventArgs e)
        {
            int idx = e.Index;
            if ( reg.m_DonorCols[idx].required == true)
            {
                // Fields that are required should remain checked
    
                e.NewValue = CheckState.Checked;
            }
            // Q: Why don't I worry about updating my (.show) state?
    
            // A: Because I enable buttons base on the ListviewItem state
    
            //    and not the state of (.show) state.
    
            //    The (.show) state will be updated once when OK is clicked.
    
        }

    When an item is required, you cannot change its checked state. So, you set the NewValue to checked.

Back to the main ListView

I started by changing the ListView property HeaderStyle to NonClickable, so I will not have to worry about dragging columns. The only worry I will have will be resizing columns, which has been taken care by the article: Persist ListView settings with serialization by dfontanesi.

Steps that need notice in the main ListView:

  1. Initialize the ListView width:
        private void InitDonors()
        {
            MyReg reg = new MyReg();
            reg.ReadDonorSettings();
            //-- Add Column Header                    
    
            listView1.BeginUpdate();
            listView1.Columns.Clear();
            foreach( MyColumns col in reg.m_DonorCols)
            {
                if ( col.show)
                    listView1.Columns.Add(col.name, 
                        col.width, HorizontalAlignment.Left);
            }
            listView1.EndUpdate();
        }
  2. Save ListView width:
        private void SaveListViewWidths()
        {
            MyReg reg = new MyReg();
            reg.ReadDonorSettings();
            foreach( ColumnHeader column in listView1.Columns )
            {
                LV_COLUMN pcol = new LV_COLUMN();
                pcol.mask = LVCF_ORDER;
                bool ret = SendMessage(listView1.Handle, 
                    LVM_GETCOLUMN, column.Index, ref pcol);
                reg.m_DonorCols[column.Index].width = column.Width;
            }
            reg.WriteDonorSettings();
        }

Well peace, I am out.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Guy Baseke


Member
Software engineer


Occupation: Software Developer (Senior)
Location: Canada Canada

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralListview Savesettings PinmemberJorge Estrada20:37 20 Dec '05  
GeneralRe: Listview Savesettings PinmemberGuy Baseke13:16 21 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 May 2004
Editor: Smitha Vijayan
Copyright 2004 by Guy Baseke
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project