I've created a checked listview programmatically, with one of the columns being an editable textbox. I want to enable or disable the subitem on each row according to the state of the checkbox, but can't find a property or a way to access the subitem's edit state. I've tried to cast the subitem back to a control, but that didn't work as well.
Asked
Active
Viewed 747 times
3
-
You will have to overlay a Textobox to achieve that. Except if you want only one coulmn to be editable: Then you can simply change the order of the columns and display Item (ie SubItems[0]) further to the right.. - If you are already doing that use LabelEdit to allow/disallow editing; or post the code you have. If you want to change the edit state for individual rows you can try to change it in the selectionchanged event.. Or consider switching to a DataGridView – TaW Jul 22 '18 at 08:52
-
What do you mean by "overlaying a TextBox"? In my case only one column is actually editable, but the SubItem[X] doesn't have an enabled property. Furthermore, I tried to create a list of TextBoxes, that holds reference to the same controls in the ListView, to try and influence it that way, but in order to manage that I need the control's name, which I can't get as well. – Guy Jul 22 '18 at 08:52
-
Well if you only need to edit one subitem you don't need textboxes. you can try to use the `ItemCheck` event: `yourLV.LabelEdit = !e.CurrentValue.HasFlag(CheckState.Checked);` - Note that afaik the checkboxes will always sit in the item1. – TaW Jul 22 '18 at 09:02
-
Contrary to the [MSDN example](https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.itemcheck(v=vs.110).aspx) the currentstate is the one before the change, hence the `!` (or go for `ItemChecked` event!) - You still need to code the Selectionindexchanges: ` if (listView2.SelectedIndices.Count <= 0) return; int i = listView2.SelectedIndices[0]; listView2.LabelEdit = listView2.Items[0].Checked;` – TaW Jul 22 '18 at 09:09
-
I tried to implement your solution but The LabelEdit didn't give me any results. I did manage to find a somewhat ugly solution. I created a list of those same textboxes and as I added a textbox to a listviewitem, I added a tag to the subitem with the textbox's name. Now all I have to do is get that name, locate it in the list and enable/disable it from there. – Guy Jul 22 '18 at 09:31
-
I can't post any script in here, cause my workplace environment doesn't enable copy-paste to the browser. – Guy Jul 22 '18 at 09:33
1 Answers
0
This is the solution I used. I hope there's a more elegant one: I created a List of TextBoxes and as I added a textbox to a ListViewItem I also added it to the list. In addition, I added a Tag to each ListViewItem with the TextBox's name. Now, when I check or uncheck a certain row I get the name of the control from the tag via lstItemList.Items[e.Index].SubItems[3].Tag.ToString() and search for it in the list. I change the Enabled property of the located TextBox.

Guy
- 325
- 1
- 3
- 18