Good day,
I'm new to whole programming thing and can't figure out / find a solution. I've been trying to populate my ListView with Items that come from another Class. But the ListView doesn't seem to do anything when the Class2
returns an Array
.
Here is how i make it :
1 ) I search for something and send the value to Class2
( Main Class in Form1 ):
private void tbSearch_KeyDown(object sender, KeyEventArgs e)
{
Class2 cl2 = new Class2();
if (e.KeyCode == Keys.Enter)
{
if (string.IsNullOrEmpty(tbSearch.Text))
{
MessageBox.Show("The Textbox is Empty.....");
}
else
{
listV.Items.Clear();
cl2.Search(tbSearch.Text); // Give a value to Method in Class2
}
}
}
2 ) Then in Class2
i do some internet search and return arrays
with the Results :
public async void Search(string search_value)
{
/*** Read the XML Data that i've got from Response ****/
foreach (XmlNode node in XMLlist)
{
/****** Get the values that i need and write them into an array *******/
string[] result = new string[10];
result[0] = series_title;
result[1] = series_type;
result[2] = series_episodes;
result[3] = series_score;
result[4] = series_id;
result[5] = series_title_english;
result[6] = series_status;
result[7] = series_start_date;
result[8] = series_end_date;
result[9] = series_image;
Form1 fm1 = new Form1();
fm1.Update_SearchList(result); // Return an array to Form1
}
}
3) And at the end i try to populate the ListView with the returned array
( Form1 again ) :
public void Update_SearchList(string [] array)
{
ListViewItem item = new ListViewItem(array);
this.listV.BeginUpdate();
this.listV.Items.Add(item); // Here the Item.Add() Method doesn't add new Items to the ListView or they just aren't being shown.
this.listV.EndUpdate();
}
The listView.View
is set to Details
and the columns are being generated on Form.Load
.
Is there a problem with how i execute the listView.Items.Add();
Method or is there another problem ?
When i try to do this whole operation in one single private void tbSearch_KeyDown(object sender, KeyEventArgs e)
everythings works.
Thank you for your time and have a good day !
You created a new instance of Form1
in Form1 fm1 = new Form1();
, which is not the instance of your caller.
I would suggest you return the result from Search()
and process in Form1
, or better, a callback function.
I haven't tried but a quick bypass solution could passing Form1
as a reference to the Search()
function, but TBH it looks nasty to me.
private void tbSearch_KeyDown(object sender, KeyEventArgs e) {
//...
cl2.Search(tbSearch.Text, ref this); // pass the ref of current instance to cl2
}
}
}
public async void Search(string search_value, ref Form1 frm) {
//...
foreach (XmlNode node in XMLlist) {
// ...
frm.Update_SearchList(result); // use the frm reference instead
}
}
public void Update_SearchList(string [] array) {
if (InvokeRequired)
BeginInvoke(new MethodInvoker(() => Update_SearchList(array)));
else {
ListViewItem item = new ListViewItem(array);
this.listV.BeginUpdate();
this.listV.Items.Add(item);
this.listV.EndUpdate();
}
}
Note: code not tested, just a quick idea!!!
Also, I don't think if your async will work as expected if called in a non-async function.
Edit:
You can also try to return results and proceed in main form. Find the draft code as follows, but it's synced and may block your UI thread. Again, code untested !
private void tbSearch_KeyDown(object sender, KeyEventArgs e) {
//...
var results = cl2.Search(tbSearch.Text);
foreach (var item in results) Update_SearchList(item);
}
}
}
public List<xxx> Search(string search_value) {
//...
var results = new List<xxx>(); // change type to the correct type
foreach (XmlNode node in XMLlist) {
// ...
results.Add(result);
}
return results;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.