I’ve developed a Visual WebGui 6.4 RC1 form that uses a ListView control with a button in each row. The button opens a custom control when pressed. It’s very similar to the “Custom ListView with control” demo in the CompanionKit.
At one point I need to be able to change the text of the button from “outside” the ListView, i.e. with no reference to the button. It took me a while to figure out how to get to the control. Here’s what I found.
When you create a ListView row, you first add a ListViewItem, then, using a reference to that ListViewItem, you add SubItems one by one. In my case, the fifth column contains the button and was created as follows:
// Edit button Gizmox.WebGUI.Forms.Button objButton = new Gizmox.WebGUI.Forms.Button(); objButton.Click += new EventHandler(OnEditButtonClick); objButton.Tag = objListViewItem; objButton.Text = "Edit"; objListViewItem.SubItems.Add(objButton);
Here is the code that finally gave me access to the button control:
// Need to change button text to "Edit" // Can't directly convert Subitem to Control, so convert to Object first Object objSubItem =
this.listView1.Items[mintListViewSavedIndex].SubItems[4]; ListViewItem.ListViewSubControlItem objControl = objSubItem as ListViewItem.ListViewSubControlItem; if (objControl != null) { objControl.Text = "Edit"; }
The non-obvious part was to figure out the “ListViewItem.ListViewSubControlItem” type so I could gain access to its properties (like .Text). I realized I could use .GetType() to find out what kind of object it is.
This returned Gizmox.WebGUI.Forms.ListViewItem+ListViewSubControlItem. The +ListViewSubControlItem indicates that it is a nested class under ListViewItem. Replacing the plus sign with a period, I was able to access the control as ListViewItem.ListViewSubControlItem.