Reputation: 128
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.
Upvotes: 0
Views: 1134
Reputation: 833
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.
Upvotes: 1