在您的文本框中,您每次都會覆蓋該ID。您需要連接文本框中的字符串。所以...
private void button5_Click(object sender, EventArgs e)
{
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement link in links)
{
if (link.GetAttribute("className")== "input-style1 psgn-name")
{
textBox10.Text += link.GetAttribute("id") + ",";
}
}
// Remove last comma
if(!string.IsNullOrWhiteSpace(textBox10.Text)){
textBox10.Text = textBox10.Text.Substring(0, textBox10.Text.Length - 1);
}
}
現在在您的文本框中,您可以看到以逗號分隔的元素的ID列表。
如果你想設置不同的文本框:
private void button5_Click(object sender, EventArgs e)
{
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("input");
int i = 10;
foreach (HtmlElement link in links)
{
if (link.GetAttribute("className")== "input-style1 psgn-name")
{
foreach(Control ctrl in Controls)
{
if (ctrl is TextBox){
TextBox tb = (CheckBox)c;
if(tb.Name == "textBox" + i) {
i++;
tb.Text = link.GetAttribute("id");
}
}
}
}
}
}
或者
private void button5_Click(object sender, EventArgs e)
{
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("input");
int i = 10;
foreach (HtmlElement link in links)
{
if (link.GetAttribute("className")== "input-style1 psgn-name")
{
TextBox tb = Controls.Find("textBox" + i) as TextBox;
i++;
if(tb != null) {
tb.Text = link.GetAttribute("id");
}
}
}
}
你得到它們全部,但textBox10.Text屬性的值總是用新值替換。嘗試調試您的代碼,你會看到 – Alekstim