7 Replies - 21668 Views - Last Post: 27 December 2012 - 04:00 PM Rate Topic: -----

#1 Guest_NIXZ*


Reputation:

Download File With Progress Bar

Post icon  Posted 06 September 2009 - 04:56 AM

There's an example on how to do that here
but it has too many events, variables that
is confusing me can someone please write
an example ? no cancel buttons and stuff
just to fill this code:
Function DownloadFile(ByRef URL As String, ByRef DestinationPath As String)

End Function

The name of the progressbar is "ProgressBar"
How i think that this should work:
1. Getting the size of the file that we want to download and updating the progressbar max value to that
2. Downloading the file and updating the progressbar based on how much data its downloaded.

Is This A Good Question/Topic? 0

Replies To: Download File With Progress Bar

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Download File With Progress Bar

Posted 06 September 2009 - 05:30 AM

Take a look at this tutorial. It is targeting C#, but it is going to be fairly easy to port it to VB.NET.
Was This Post Helpful? 1
  • +
  • -

#3 Spiralz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-March 09

Re: Download File With Progress Bar

Posted 06 September 2009 - 07:02 AM

You could just use the My.Computer.Network.DownloadFile method and set ShowUI to true, which will show a window with a ProgressBar while the file downloads.


Function DownloadFile(ByRef URL As String, ByRef DestinationPath As String)

My.Computer.Network.DownloadFile(URL, DestinationPath, vbNullString, vbNullString, True, 5000, True)

End Function




It's not exactly the best looking UI, however.

This post has been edited by Spiralz: 06 September 2009 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_NIXZ*


Reputation:

Re: Download File With Progress Bar

Posted 06 September 2009 - 07:07 AM

View PostCore, on 6 Sep, 2009 - 04:30 AM, said:

Take a look at this tutorial. It is targeting C#, but it is going to be fairly easy to port it to VB.NET.



It seems like im having a problem with this i dont know how to transform it...
		Dim WebClient As New WebClient
		WebClient.DownloadProgressChanged = New DownloadProgressChangedEventHandler(client_DownloadProgressChanged)
		WebClient.DownloadFileCompleted += New AsyncCompletedEventHandler(client_DownloadFileCompleted)
		WebClient.DownloadFileAsync(New Uri(URL), DestinationPath)
		double bytesIn = double.Parse(e.BytesReceived.ToString())
	double totalBytes = double.Parse(e.TotalBytesToReceive.ToString())
	double percentage = bytesIn / totalBytes * 100;

		ProgressBar.Value = Int.Parse(Math.Truncate(percentage).ToString())



This post has been edited by NIXZ: 06 September 2009 - 07:11 AM

Was This Post Helpful? 0

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Download File With Progress Bar

Posted 06 September 2009 - 07:14 AM

There is an open source IDE called SharpDevelop which can convert code directly from C# to VB.NET. It's not fool proof but it may help you with this.

http://www.icsharpco.../OpenSource/SD/
Was This Post Helpful? 0
  • +
  • -

#6 Guest_NIXZ*


Reputation:

Re: Download File With Progress Bar

Posted 06 September 2009 - 07:21 AM

I dont think that i need to learn how to use another program right now i only need this 10 lines of code to be transformed from C# to VB.NET cause im lost ...
Was This Post Helpful? 0

#7 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Download File With Progress Bar

Posted 06 September 2009 - 09:54 AM

This is how the event declaration looks like:

		Dim webClient As New WebClient()
		AddHandler webClient.DownloadProgressChanged, AddressOf client_ProgressChanged
		AddHandler webClient.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
		webClient.DownloadFileAsync(New Uri(URL), DestinationPath)



The actual event handlers look like this:

Public Sub client_ProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
		Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
		Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
		Dim percentage As Double = bytesIn / totalBytes * 100
		ProgressBar.Value = Integer.Parse(Math.Truncate(percentage).ToString())
	End Sub

	Public Sub client_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs)

	End Sub



Don't forget to declare the URL and DestinationPath variables.
Was This Post Helpful? 1

#8 Globin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-December 12

Re: Download File With Progress Bar

Posted 27 December 2012 - 04:00 PM

I've did a small download manager with the C# targeted code adapted to VBNET in #Develop.
It has a listbox to list downloads
2 labels(Source URL, Destiny file)
2 Textboxes(Enter source & destiny)
2 buttons(Start, Browse File)
And ProgressBar, obviosly.
Imports System.Net
Imports System.ComponentModel

Partial Public Class MainForm

    Public Sub New()
        ' The Me.InitializeComponent call is required for Windows Forms designer support.
        Me.InitializeComponent()

        '
        ' TODO : Add constructor code after InitializeComponents
        '
    End Sub

    Sub Button2Click(ByVal sender As Object, ByVal e As EventArgs)
        textBox1.Enabled = False
        textBox2.Enabled = False
        button1.Enabled = False
        button2.Enabled = False
        Dim s As New WebClient
        AddHandler s.DownloadProgressChanged, AddressOf pC
        AddHandler s.DownloadFileCompleted, AddressOf pC2
        s.DownloadFileAsync(New Uri(textBox1.Text), textBox2.Text)
    End Sub
    Sub pC(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        progressBar1.Value = e.ProgressPercentage
    End Sub
    Sub pC2(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        MsgBox("Complete")
        textBox1.Enabled = True
        textBox2.Enabled = True
        button1.Enabled = True
        button2.Enabled = True
        listBox1.Items.Add(textBox2.Text)
    End Sub
	
	Sub Button1Click(sender As Object, e As EventArgs)
		Dim s As New SaveFileDialog
		If s.ShowDialog = vbOK Then
			textBox2.Text = s.FileName
		End If
	End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1