Stop user from selecting another item in ListView

Is there anyone that knows how to stop the ListView from changing the current selection state.

On the Form class you can stop the form from closing by using the FormClosing event. See the code below:

privatevoid SupportCallMonitorForm_FormClosing(object sender,FormClosingEventArgs e)

{
e.Cancel = true;
}

Is there something similar on the ListView for item selection changes?

Sad

[704 byte] By [HermanduPlessis] at [2007-12-16]
# 1
No, there isn't. You would have to extend the ListView and it's selection change related events and event args to implement this. An easier approach is to ignore mouse clicks and navigation characters, by overriding WndProc. In this case, I have defined a ReadOnly property to enable/disable this behaviour:



class ListViewEx : ListView

{

private bool _readOnly;

public bool ReadOnly

{

get { return _readOnly; }

set { _readOnly = value; }

}

public ListViewEx()

: base()

{

ReadOnly = false;

}

protected override void WndProc(ref Message m)

{

if (this.ReadOnly)

{

// Ignore mouse clicks

if (m.Msg == 0x201 //WM_LBUTTONDOWN

|| m.Msg == 0x203) //WM_LBUTTONDBLCLK

return;

// Ignore navigation characters

if (m.Msg == 0x0100 //WM_KEYDOWN

|| m.Msg == 0x0101 //WM_KEYUP

|| m.Msg == 0x0102) //WM_CHAR

{

switch ((Keys)(int)m.WParam)

{

case Keys.Left:

case Keys.Right:

case Keys.Up:

case Keys.Down:

case Keys.PageDown:

case Keys.PageUp:

case Keys.Home:

case Keys.End:

return;

break;

default:

// Not a navigation key...allow keystroke

break;

}

}

}

base.WndProc(ref m);

}

}


durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...