none
Make Listview is readonly

    Question

  • Hey,

    I am trying to make ListView ReadOnly and can't setup Enable property to false because I need allow user use scroll bar. What I did is handling Mouse Click and KeyPress event and keep checkState of inside check boxes as no change. However it has a problem, If user continue clicking mouse and pressing space key at same time. In sometime, the checkstate of check is change. It is working in mouse click only or space key pressing.

    Any idea will be helpful.

    Thanks
    Monday, May 07, 2007 7:43 PM

Answers

  • Okay, here's another approach.  Add a new class to your project and paste the code below.  Build.  Drop a MyListView from the top of the toolbox onto your form and set the ReadOnly property to True.

    using System;
    using System.Windows.Forms;

    public class MyListView : ListView {
      private bool mCreating;
      private bool mReadOnly;
      protected override void OnHandleCreated(EventArgs e) {
        mCreating = true;
        base.OnHandleCreated(e);
        mCreating = false;
      }
      public bool ReadOnly {
        get { return mReadOnly; }
        set { mReadOnly = value; }
      }
      protected override void OnItemCheck(ItemCheckEventArgs e) {
        if (!mCreating && mReadOnly) e.NewValue = e.CurrentValue;
        base.OnItemCheck(e);
      }
    }


    Monday, May 07, 2007 9:10 PM

All replies

  • This worked for me:

      public partial class Form1 : Form {
        private bool mInitialized;
        public Form1() {
          InitializeComponent();
          listView1.ItemCheck += listView1_ItemCheck;
        }
        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e) {
          if (mInitialized) e.NewValue = e.CurrentValue;
        }
        private void Form1_Load(object sender, EventArgs e) {
          mInitialized = true;
        }
      }

    Note that the mInitialized flag is only necessary when you initialize one or more of the ListView's items with the Checked = True.  You could also wait with adding the event handler until the Load event.  Details, details...
    Monday, May 07, 2007 8:02 PM
  • Good point.

    However it doesn't work. For example, one tab Control including more than one tagPages. tabPage3 has listview control. The tabPage1 should be the initial one when form loaded. At this time, you can load data to listview and ItemCheck event never fired. When you switch to tabPage3, tagPage3 tries to paint listview and check necessary checkboxes. That is the itemCheck event will be raised. In this case, the e.NewValue is not equal with e.CurrentValue. If I use your code, there won't have any checkbox checked.

    Also, when tagPage3 is in the top of tagPages. If use try to retrieve data from somewhere, the state of each checkbox may be different. How can't refresh listview control?
    Monday, May 07, 2007 8:48 PM
  • Okay, here's another approach.  Add a new class to your project and paste the code below.  Build.  Drop a MyListView from the top of the toolbox onto your form and set the ReadOnly property to True.

    using System;
    using System.Windows.Forms;

    public class MyListView : ListView {
      private bool mCreating;
      private bool mReadOnly;
      protected override void OnHandleCreated(EventArgs e) {
        mCreating = true;
        base.OnHandleCreated(e);
        mCreating = false;
      }
      public bool ReadOnly {
        get { return mReadOnly; }
        set { mReadOnly = value; }
      }
      protected override void OnItemCheck(ItemCheckEventArgs e) {
        if (!mCreating && mReadOnly) e.NewValue = e.CurrentValue;
        base.OnItemCheck(e);
      }
    }


    Monday, May 07, 2007 9:10 PM
  • That is working. Thanks for your help.
    Tuesday, May 08, 2007 3:29 PM
  • This is correction. The solution nobugz provided has a problem for listview already created. In this case, the check state of each checkbox won't change. In my real work, I made some change listed here.

    Setup flag in Space key press and Mouse single click event. If the ItemCheck was triggered by above events, keep check state as no change using e.newstate=e.currentstate.

    This has a little work to do for mouse double click and user single click checkbox lable. you can add your code to handle that.

    After these change, my listview is working fine for readonly situation.


    Tuesday, May 15, 2007 7:50 PM