从函数添加项目到列表视图
Add item to Listview from function
我正在尝试在我的 listView1 中添加一行。我如何在另一个函数中执行此操作?
我正在阅读 this post。他们希望我直接将其添加到按钮功能中。我不想那样做。
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 中添加一行 listView1.Items.Add(listViewItem);
,但我收到一条错误消息
An object reference is required for the non-static field, method, or property 'Form1.listView1'
所以根据你的要求试试这个。
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);
}
我正在尝试在我的 listView1 中添加一行。我如何在另一个函数中执行此操作?
我正在阅读 this post。他们希望我直接将其添加到按钮功能中。我不想那样做。
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 中添加一行 listView1.Items.Add(listViewItem);
,但我收到一条错误消息
An object reference is required for the non-static field, method, or property 'Form1.listView1'
所以根据你的要求试试这个。
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);
}