Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnStartDownload_Click(object sender, EventArgs e) { WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); // Starts the download client.DownloadFileAsync(new Uri("SomeURLToFile"), "SomePlaceOnLocalHardDrive"); btnStartDownload.Text = "Download In Process"; btnStartDownload.Enabled = false; } void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = bytesIn / totalBytes * 100; progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("Download Completed"); btnStartDownload.Text = "Start Download"; btnStartDownload.Enabled = true; } private void Form1_Load(object sender, EventArgs e) { } private void progressBar1_Click(object sender, EventArgs e) { } private void progressBar1_Click_1(object sender, EventArgs e) { } } }
Erreur 2 Le nom 'btnStartDownload' n'existe pas dans le contexte actuel C:\Users\Felix-Etienne\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 30 13 WindowsFormsApplication1
sender.Text = "Start Download"; sender.Enabled = true;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } WebClient client = null; private void selectBouton_Click_1(object sender, EventArgs e) { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { saveTo.Text = folderBrowserDialog.SelectedPath; } } private void downloadBouton_Click_1(object sender, EventArgs e) { if (Uri.IsWellFormedUriString("http://l2euthopia.fr/launcher/", UriKind.Absolute)) { //Création d'une nouvelle instance de WebClient client = new WebClient(); //On abonne notre instance client à l'évènement de progression du téléchargement client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); //On abonne notre instance client à l'évènement de fin du téléchargement client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); //On lance le téléchargement avec les paramètres saisies. //Le premier correspond à l'adresse du fichier à télécharger et le second à l'emplacement de sauvegarde Uri url = new Uri("http://l2euthopia.fr/launcher/"); client.DownloadFileAsync(url, saveTo.Text); downloadBouton.Enabled = false; // Rend inaccessible le bouton Télécharger downloadBouton.Text = "En cours..."; // Modifie le texte de ce même bouton //Rend accessible le bouton Annuler afin de pouvoir arrêter le téléchargement cancelButton.Enabled = true; } else { MessageBox.Show("Vous devez saisir une adresse correcte"); } } private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { // Met à jour la position de la barre de progression à partir // de l'état d'avancement contenu dans l'attribut ProgressPercentage progressBar.Value = e.ProgressPercentage; } private void reinitialisation() { //Rend accessible le bouton Télécharger downloadBouton.Enabled = true; downloadBouton.Text = "Mise a Jour"; //Rend inaccessible le bouton Annuler cancelButton.Enabled = false; //Réinitialisation de la barre de progression progressBar.Value = 0; } private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (!e.Cancelled) { //Affichage du message de confirmation MessageBox.Show("Téléchargement terminé."); } else { //Affichage du message d'annulation MessageBox.Show("Téléchargement annulé."); } reinitialisation(); } private void cancelButton_Click(object sender, EventArgs e) { client.CancelAsync(); } private void saveTo_TextChanged(object sender, EventArgs e) { } private void progressBar_Click(object sender, EventArgs e) { } } }