0

I'm new in C# programming.

I want to write a simple Form application where on a ListView after each action, I will be updated what is going on.

I wrote some code which should do the job.

The application is working good but the update on the ListView not.

    namespace ReportingTool
{
    public partial class Form1 : Form
    {
        public static Form1 form;
        public Form1()
        {
            InitializeComponent();
            form = this;
            WriteToList("1", "Program started");
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            WriteToList("2", "Opening Chrome Browser");
            PageNavigation driver = new PageNavigation();
            await driver.BrowserActions();
            WriteToList("3", "Opening TalentLink page");
            await driver.GoToTalentLinkPageAsync();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public void WriteToList(string id, string action)
        {
            form.Activate();
            ListViewItem x = new ListViewItem(id);
            x.SubItems.Add(action);
            form.listView1.Items.Add(x);
            form.listView1.Refresh();
        }
    }
}

Thank you for your help.

6
  • What is the problem? There's no asynchronous updating in this code, WriteToList() runs on the UI thread and should work just fine whether it's called from a synchronous or asynchronous event handler Commented Sep 3, 2018 at 11:11
  • Does the UI freeze perhaps? What do the BroweserActions() and GoToTalentLinkPageAsync() methods do? Commented Sep 3, 2018 at 11:11
  • Yes but on the Form there is no update visible. Like it wouldnt exist? Commented Sep 3, 2018 at 11:12
  • driver.BrowserActions() sets driver for Selenium and driver.GoToTalentLinkPageAsync() runs other methods how to navigate on page, and what to do etc.. Commented Sep 3, 2018 at 11:13
  • Could you please be a little more specific about your problem? Does it give you any error message or is just silently refusing to update the ListView? Commented Sep 3, 2018 at 11:14

1 Answer 1

1

Unfortunately there is a bug (or lack of implementation) on ListView. Basically if you make modification to any item on your existing list, this update will not be visible on UI despite being updated in code. Somehow you have to force the refresh on ItemSource in ListView. The only way I've managed to do it is save existing List of items set ItemSource to null and add updated list to the ItemSource, so:

    public void WriteToList(string id, string action)
    {
        form.Activate();
        ListViewItem x = new ListViewItem(id);
        x.SubItems.Add(action);
        form.listView1.Items.Add(x);
        form.listView1.Refresh();
        List<YourItems> newList = new List<YourItems>();
        newList = form.listView1;
        form.listView.ItemSource = null;
        form.listView.ItemSource = newList;

    }

Also I don't think that setting Static field of your class is a good way of approaching it.

Sign up to request clarification or add additional context in comments.

3 Comments

You didn't mention that you use data binding and an ItemSource. There's no bug. When you use data binding you need to update the data source, not the ListView directly. You can just add a new entry to your list and call Refresh on the listview. Eg, _itemsList.Add(new YourItem(id,action); listView.Refresh();. You don't need to add the ListViewItem manually or replace the ItemSource`
Possibly, I never tried it. However as u can see he is not using DataBinding here.
You are using data binding through ItemsSource. BTW ListView doesn't have an ItemsSource property in Windows Forms. Is this a WPF application? Or did you use the WPF control? In that case you could use an ObservableCollection<T> instead of a List<T>. This would notify the control to update itself each time you modified the collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.