我正在尝试在listView1中添加一行。如何在其他功能中执行此操作?
我正在阅读this帖子。他们希望我直接将其添加到按钮功能中。我不想那样。
private void button1_Click(object sender, EventArgs e)
{
keyword();
}
public static void keyword()
{
string country = "";
string key = "1070";
//Goto GetHtmlAsync
GetHtmlAsync(key, country);
}
public static async void GetHtmlAsync(string key, string country)
{
//GetHtmlAsync
var url = "https://www.test.com/search?county=" + country + "&q=" + key;
var httpClient = new HttpClient();
var html = await httpClient.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//This is grabbed from HtmlDocument (list)
var id = "58756";
var seller = "Test";
var product = "GTX 1070";
var betTime = "10:10";
var price = "100";
var shipping = "4";
string[] row = { id, seller, product, betTime, price + shipping, url };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
我希望它在listView1.Items.Add(listViewItem);
的listView1中添加一行,但是我收到一条错误消息,说
非静态字段,方法或属性'Form1.listView1'需要对象引用
答案 0 :(得分:0)
所以根据您的要求尝试一下。
private void button1_Click(object sender, EventArgs e)
{
keyword();
}
// Non static so that you can access ListView1
public void keyword()
{
string country = "";
string key = "1070";
//Goto GetHtmlAsync
GetHtmlAsync(key, country);
}
// Non static
public async void GetHtmlAsync(string key, string country)
{
//GetHtmlAsync
var url = "https://www.test.com/search?county=" + country + "&q=" + key;
var httpClient = new HttpClient();
var html = await httpClient.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//This is grabbed from HtmlDocument (list)
var id = "58756";
var seller = "Test";
var product = "GTX 1070";
var betTime = "10:10";
var price = "100";
var shipping = "4";
string[] row = { id, seller, product, betTime, price + shipping, url };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}