Custom Download

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
splitedchill
VIP - Donator
VIP - Donator
Posts: 8
Joined: Thu Apr 15, 2010 4:09 pm

Custom Download
splitedchill
Hello and welcome to my next tutorial

Today i show you how to make a custom download with a Progressbar and Labels they show you how many MB you have to download.

Now we beginning with the Form1 Design we need:


Button 1 for a SaveFileDialog
Button 2 for a Download
1 Progressbar
1 Textbox for the path
1 Label for the MB

Now we beginning with the code i give you the code in one, because you need some Events and i can not explain this in english!
Code: Select all
Imports System.Net
Public Class download
    Private WithEvents httpclient As WebClient

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim save As New SaveFileDialog
        save.Title = "Save under..."
        save.ShowDialog()
        TextBox1.Text = save.FileName & ".rar"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        httpclient = New WebClient

        Dim SourceURL As String = "http://yourwebsite/withprogram.exe"
        Dim ZielDatei As String = TextBox1.Text

        ProgressBar1.Value = 0
        ProgressBar1.Maximum = 100

        Button2.Enabled = False
        Button1.Enabled = False

        Try

            httpclient.DownloadFileAsync(New Uri(SourceURL), ZielDatei)
        Catch ex As Exception

            Button1.Enabled = True
            MsgBox("Fehler!" & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub httpclient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles httpclient.DownloadFileCompleted
        MsgBox("Download complete")
    End Sub

    Private Sub httpclient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage

        Dim totalbytes As Long = e.TotalBytesToReceive / 1024
        Dim mtotalbytes As Long = totalbytes / 1024

        Dim bytes As Long = e.BytesReceived / 1024
        Dim mbytes As Long = bytes / 1024

        If totalbytes < 1 Then totalbytes = 1
        If bytes < 1 Then bytes = 1

        If totalbytes > 1024 Then
            Label9.Text = mbytes.ToString & " MB von " & mtotalbytes.ToString & " MB"
        Else
            Label9.Text = bytes.ToString & " KB von " & totalbytes.ToString & " KB"
        End If
    End Sub
The last part is some math but the rest is very easy to understand

Have a nice day and i hope you like my tutorial give me please feedback
Last edited by splitedchill on Tue Sep 06, 2011 7:22 pm, edited 1 time in total.
User avatar
rocky4126
VIP - Donator
VIP - Donator
Posts: 258
Joined: Mon Nov 16, 2009 7:39 pm

Re: Custom Download
rocky4126
Hi could you translate the save.Title text etc to english if possible? That'd be great, I don't do good in most languages XD
Image
User avatar
lolxot
VIP - Donator
VIP - Donator
Posts: 155
Joined: Thu Apr 08, 2010 4:59 pm

Re: Custom Download
lolxot
rocky4126 wrote:
Hi could you translate the save.Title text etc to english if possible? That'd be great, I don't do good in most languages XD
Code: Select all
Imports System.Net
Public Class download
    Private WithEvents httpclient As WebClient

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim save As New SaveFileDialog
        save.Title = "Save under..."
        save.ShowDialog()
        TextBox1.Text = save.FileName & ".rar"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        httpclient = New WebClient

        Dim SourceURL As String = "http://yourwebsite.com/withprogram.exe"
        Dim DestinationFile As String = TextBox1.Text

        ProgressBar1.Value = 0
        ProgressBar1.Maximum = 100

        Button2.Enabled = False
        Button1.Enabled = False

        Try

            httpclient.DownloadFileAsync(New Uri(SourceURL), DestinationFile)
        Catch ex As Exception

            Button1.Enabled = True
            MsgBox("Error!" & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub httpclient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles httpclient.DownloadFileCompleted
        MsgBox("Download completed")
    End Sub

    Private Sub httpclient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage

        Dim totalbytes As Long = e.TotalBytesToReceive / 1024
        Dim mtotalbytes As Long = totalbytes / 1024

        Dim bytes As Long = e.BytesReceived / 1024
        Dim mbytes As Long = bytes / 1024

        If totalbytes < 1 Then totalbytes = 1
        If bytes < 1 Then bytes = 1

        If totalbytes > 1024 Then
            Label9.Text = mbytes.ToString & " MB von " & mtotalbytes.ToString & " MB"
        Else
            Label9.Text = bytes.ToString & " KB von " & totalbytes.ToString & " KB"
        End If
    End Sub
User avatar
splitedchill
VIP - Donator
VIP - Donator
Posts: 8
Joined: Thu Apr 15, 2010 4:09 pm

Re: Custom Download
splitedchill
okey i have change all things but i see lolxot is faster :)
User avatar
lolxot
VIP - Donator
VIP - Donator
Posts: 155
Joined: Thu Apr 08, 2010 4:59 pm

Re: Custom Download
lolxot
yeah :D
and i even changed "ZielDatei" to "DestinationFile"
5 posts Page 1 of 1
Return to “Tutorials”