在您的文本框中,您每次都会覆盖该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