简体   繁体   中英

Insert data in RichTextBox C#

My code:

private void timer4_Tick(object sender, EventArgs e)
{
    for (int a = 0; a < 10; a++)
    {
        var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
        richTextBox1.Text = infos;
    }
    timer4.Stop();
}

I want to insert all of 10 src values in RichTextBox, while my code do it only once.

You can use AppendText

Replace

richTextBox1.Text = infos;

with

richTextBox1.AppendText(infos);

OR

richTextBox1.Text += infos + Environment.NewLine;

This line is wrong.

richTextBox1.Text = infos; 

This is right.

richTextBox1.AppendText= infos;

What your code is doing is setting the text to equal each infos , 10 times over.

So I'm guessing your output would be the last infos variable? What you might want to do instead is this:

private void timer4_Tick(object sender, EventArgs e)
{
    for (int a = 0; a < 10; a++)
    {
        var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
        richTextBox1.Text += infos; // the "+=" will add each infos to the textbox
    }
    timer4.Stop();
}

As you can see, if you use the += instead of just = , it will add each iteration to the whole, instead of just overriding the whole value each time.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM