I have a ListBox
in my WinForms
application and I want to disable some items on that list,
for example if I right click on an item , it gets disabled and if I left click on a disabled item it should be enabled.
How can I do this?
Thanks very much
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If for whatever reason a custom control is not appropriate you can achieve the same result from a 'visual' point of view by splitting your results into two list boxes.
1st Control: ListBoxA (Active).
2nd Control: ListBoxB (Inactive).
Add any items which are active / can be selected to ListBoxA and any items which need to be disabled to ListBoxB.
For the next part bear in mind that the default item height for a standard ListBox entry is something like 18px.
You can have the two controls aligned vertically - dynamically setting the height of the first control to 18px multiplied by the number of Items in ListBoxA
See example below: (https://snag.gy/4GHYiz.jpg)
Of course! This only helps if you don't need the disabled items to display in the given order.
I found a way. We must create a custom ListBox Control to do this. :)
With it, you can enable or disable items with item index.
To use :
listBox1.DisableItem(index); listBox1.EnableItem(index);
Download Source
There is no native Disable/Enable items in ListBox. However you can do some tricks.
First you need to define you own class for item which will have
Enabled
property.Then you need to subscribe to MouseDown event and check which (righ or left) button was clicked. And based on x,y position get the item that was clicked. Then you will set the
Enabled
property to True/False. Below are some examples :Your custom class
The MouseDown event