How can I add another subitem to a list item after it's loaded?

I have this app with a list control and a button. When I click the button, I want it to add the time and file name to the list, and then add it to the control, after it finished uploading the file, I want it to add another subitem to the same list, but by for some reason not adding it and I can't figure out why I am calling something wrong?

using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;

namespace Listviews
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ListViewItem lvi = new ListViewItem();
        WebClient webClient;


        public void DownloadFile(string urlAddress, string location)
        {
            using (webClient = new WebClient())
            {
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

                try
                {
                    // Start downloading the file
                    webClient.DownloadFileAsync(new System.Uri(urlAddress), location);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        // The event that will trigger when the WebClient is completed
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                MessageBox.Show("Download has been canceled.");
            }
            else
            {
                ListViewItem lvItems = new ListViewItem();
                lvItems.SubItems.Add("Done");
                listView1.Items.Add(lvItems);
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            ListViewItem lvItems = new ListViewItem();
            string clock = DateTime.Now.ToString("HH:mm");
            lvItems.Text = clock;
            listView1.Items.Add(lvItems);

            string requestUrl = "url";
            string fileName = "\\Name";
            string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            string combinedPaths = savePath + fileName;
            DownloadFile(requestUrl, combinedPaths);
        }
    }
}

      

0


source to share





All Articles