WinForms ListBox with readonly / disabled files

Is there a way to make some items in a ListBox readonly / disabled so that they cannot be selected? Or are there any similar ListBox controls to provide this function?

+4
source share
5 answers

There is no support for ListBox for this. You can pin something, you can unselect the selected item. Here is a silly example that prevents the selection of even-numbered elements:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { for (int ix = listBox1.SelectedIndices.Count - 1; ix >= 0; ix--) { if (listBox1.SelectedIndices[ix] % 2 != 0) listBox1.SelectedIndices.Remove(listBox1.SelectedIndices[ix]); } } 

But the flicker is quite noticeable, and this confuses keyboard navigation. You can get better results using CheckedListBox, you can prevent the user from checking the box for the item:

 private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.Index % 2 != 0) e.NewValue = CheckState.Unchecked; } 

But now you cannot redefine the drawing to make it obvious to the user that the item is not selectable. There are no big solutions, it’s much easier not to display the items in the box that should not be selected.

+4
source

@ Solves the problem, as a result of which the identifier of the element is selected for a short time, and then the selection disappears. I don't like this - it can be confusing for the end user.

I prefer to hide some of the edit options buttons for the item that should be disabled:

  if (lbSystemUsers.Items.Count > 0 && lbSystemUsers.SelectedIndices.Count > 0) if (((RemoteSystemUserListEntity)lbSystemUsers.SelectedItem).Value == appLogin) { bSystemUsersDelete.Visible = false; bSystemUsersEdit.Visible = false; } else { bSystemUsersDelete.Visible = true; bSystemUsersEdit.Visible = true; } 

Here is a list of the users listed, and prohibit editing the user who actually registered in the edit panel.

+1
source

ListBox does not have a ReadOnly (or similar) property, but you can create your own ListBox control. Here is a solution that worked very well for me:

https://ajeethtechnotes.blogspot.com/2009/02/readonly-listbox.html

 public class ReadOnlyListBox : ListBox { private bool _readOnly = false; public bool ReadOnly { get { return _readOnly; } set { _readOnly = value; } } protected override void DefWndProc(ref Message m) { // If ReadOnly is set to true, then block any messages // to the selection area from the mouse or keyboard. // Let all other messages pass through to the // Windows default implementation of DefWndProc. if (!_readOnly || ((m.Msg <= 0x0200 || m.Msg >= 0x020E) && (m.Msg <= 0x0100 || m.Msg >= 0x0109) && m.Msg != 0x2111 && m.Msg != 0x87)) { base.DefWndProc(ref m); } } } 
+1
source

I know this is an old thread, but I will post a workaround for other readers in the future.

 listBox.Enabled = false; listBox.BackColor = Color.LightGray; 

This will change the background color of the list to Light Gray. Thus, it is not built into the “native path” for this, but at least gives the user some feedback that he should not / cannot edit this field.

0
source

To get read-only behavior, I have MyCBLLocked, a boolean associated with the MyCBL checklist control, and for the CheckItem event that I am doing:

 private void MyCBL_ItemCheck(object sender, ItemCheckEventArgs e) { if (MyCBLLocked) e.NewValue = e.CurrentValue; } 

So instead

 MyCBL.Enabled = false; 

I use

 MyCBLLocked = true; 

and the user can scroll through many options, but not mess up the changes.

0
source

Source: https://habr.com/ru/post/1304015/


All Articles