Question Help for an Event to detect a change in ListView subitem/cell

es2058

New member
Joined
Jul 28, 2022
Messages
4
Programming Experience
Beginner
Is there a way to detect when a column cell in the Listview changes and triggers an event to do something else?
ie.
ListView has 3 columns: item,qty,desc
1,1,item_1
2,2,item_2
3,,3item_3

When any of the of the values in qty column changes, it trigger an event with the row subitem. When row 2 qty change to 0, the event with the row 2 as argument and the eventhandler would check the qty it needs to order more.
 
The documentation is very helpful...

AfterLabelEdit looks promising.
 
This thread is obviously in the wrong form because the question is not about VS. Are you talking about a WinForms ListView? Presumably so, in which case I will move this thread to the Windows Forms forum.

There's also the issue that you can't actually edit the text of a subitem directly anyway. You can edit the text of an item in place if LabelEdit is set to True but you can only change the text of subitem programmatically, in which case you wouldn't need an event. anyway. If you want the user to be able to edit tabular data then use a DataGridView rather than a ListView. ListView controls are vastly over-used by beginners. If you're not using a view other than Details and not using groups then don't use a ListView.
 
This thread is obviously in the wrong form because the question is not about VS. Are you talking about a WinForms ListView? Presumably so, in which case I will move this thread to the Windows Forms forum.

There's also the issue that you can't actually edit the text of a subitem directly anyway. You can edit the text of an item in place if LabelEdit is set to True but you can only change the text of subitem programmatically, in which case you wouldn't need an event. anyway. If you want the user to be able to edit tabular data then use a DataGridView rather than a ListView. ListView controls are vastly over-used by beginners. If you're not using a view other than Details and not using groups then don't use a ListView.
The ListView is linked to dataset from a different program, I'm just using it as a read-only, just need to know when the data changes and what the cell value changed to. I was thinking an event is the best way to do this, but I really don't have a clue, so any guidance would be much appreciated.
 
The ListView is linked to dataset from a different program, I'm just using it as a read-only, just need to know when the data changes and what the cell value changed to.
How is the list view data updated? You'll want to hook into that process. You don't want to react to the updates on the view. You want to react to changes in the data model.

Remember that the bad old days of Win31/Win95 are long gone when people were using the UI as a data store. Modern programming practices dictates that you have your data in your data model, and that the UI is merely a view into that data model.
 
How is the list view data updated? You'll want to hook into that process. You don't want to react to the updates on the view. You want to react to changes in the data model.

Remember that the bad old days of Win31/Win95 are long gone when people were using the UI as a data store. Modern programming practices dictates that you have your data in your data model, and that the UI is merely a view into that data model.
ListView is updated thru a API which creates a bunch of threads listening for updates. I completely agree but unfortunately I don't know programming or C# well enough to really know what is happening in the API classes to access the data directly nor the time to figure it out, hence using ListView.
 
API's can't update ListViews. C# code can call APIs and then put that data into the ListView. You'll want to add code to that existing code that puts the data into the ListView.
 
API's can't update ListViews. C# code can call APIs and then put that data into the ListView. You'll want to add code to that existing code that puts the data into the ListView.
I don't have a good understanding of event and handlers, there is an OPC UA connection to a server to handle when the value changes, it creates an event and calls ClientAPI_ValueChanged and this is what updates the ListView. So I don't need access the ListView since I can access the values off the MonitoredItem and MonitoredItemNotificationEventArgs, but how do I create an event within ClientApi_ValueChanged and use it outside of this class? Once ClientAPI_ValueChanged is updated I like to have an Event created and a EventHandler outside of this class to create messages off this changed values.
C#:
m_Server.ItemChangedNotification += new MonitoredItemNotificationEventHandler(ClientApi_ValueChanged);

private void ClientApi_ValueChanged(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            // We have to call an invoke method.
            if (this.InvokeRequired)
            {
                // Asynchronous execution of the valueChanged delegate.               
                this.BeginInvoke(new MonitoredItemNotificationEventHandler(ClientApi_ValueChanged), monitoredItem, e);
                return;
            }


            // Extract notification from event
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null)
            {
                return;
            }
                       
            Object value = notification.Value.WrappedValue.Value;

            // Get the according item.
            ListViewItem item = (ListViewItem)monitoredItem.Handle;

            //add old value to ListView
            if (item.SubItems[2].Text != notification.Value.Value.ToString())
            {               
                item.SubItems[5].Text = item.SubItems[2].Text;
            }

            // Set current value, status code and timestamp.
            item.SubItems[2].Text = Utils.Format("{0}", notification.Value.Value);
            item.SubItems[3].Text = Utils.Format("{0}", notification.Value.StatusCode);
            item.SubItems[4].Text = Utils.Format("{0:HH:mm:ss.fff}", notification.Value.SourceTimestamp.ToLocalTime());
        }
 
Last edited by a moderator:
how do I create an event within ClientApi_ValueChanged and use it outside of this class? Once ClientAPI_ValueChanged is updated I like to have an Event created and a EventHandler outside of this class to create messages off this changed values.
See:
 
Back
Top Bottom