Avatar billede kris914g1 Nybegynder
13. juni 2013 - 09:18 Der er 5 kommentarer

hvordan kan jeg tilføje en progress bar til mit program

hej har lavet et download program, men hvordan kan jeg add en progress bar? nogen som har en ide?
her er mit script:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        My.Computer.Network.DownloadFile _
    ("https://dl.dropboxusercontent.com/ (...), _
    "C:\Documents and Settings\All Users\Documents\minecraft.jar")

    End Sub


    Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click

    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    End Sub

End Class

ps hvis i har en løsning kan i ikke bare copy paste det ind i en kommentar

tak på forhånd
Avatar billede wordwrap Nybegynder
13. juni 2013 - 09:26 #1
Avatar billede kris914g1 Nybegynder
13. juni 2013 - 11:39 #2
har prøvet at rode med det men kan ikke få det til at virke kan du ikke prøve at sætte den ind i mit script
Avatar billede arne_v Ekspert
16. juni 2013 - 04:09 #3
Jeg tvivler paa  My.Computer.Network.DownloadFile og ProgressBar spiller godt sammen.

Et eksempel som virker:


Imports System
Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Windows.Forms
Imports System.Threading

Public Class MainForm
Inherits Form
    Private bar As ProgressBar
    Private start As Button
    Private abort As Button
    Private t As Thread
    Public Sub New()
        bar = New ProgressBar
        start = New Button
        abort = New Button
        SuspendLayout
        bar.Location = New Point(50, 50)
        bar.Size = New Size(200, 50)
        bar.Name = "Progress Bar"
        start.Location = New Point(50, 150)
        start.Size = New Size(200, 50)
        start.Name = "Start Button"
        start.Text = "Start"
        AddHandler start.Click, AddressOf StartClick
        abort.Location = New Point(50, 250)
        abort.Size = New Size(200, 50)
        abort.Name = "Abort Button"
        abort.Text = "Abort"
        AddHandler abort.Click, AddressOf AbortClick
        ClientSize = New Size(300, 350)
        Controls.Add(bar)
        Controls.Add(start)
        Controls.Add(abort)
        Name = "Main Form"
        Text = "Main Form"
        ResumeLayout(False)
    End Sub
    Sub ResetBar(ByVal n As Integer)
        bar.Minimum = 0
        bar.Maximum = n
        bar.Value = 0
    End Sub
    Sub UpdateBar(ByVal n As Integer)
        bar.Value = n
    End Sub
    Delegate Sub ResetHandler(ByVal n As Integer)
    Delegate Sub UpdateHandler(ByVal n As Integer)
    Sub Copy()
        Try
        Dim req As HttpWebRequest = CType(WebRequest.Create("http://wasd.vsm.com.au/wasd/wasd1020.zip"), HttpWebRequest)
        Dim resp As HttpWebResponse = CType(req.GetResponse, HttpWebResponse)
        Dim tot As Integer = CType(resp.ContentLength, Integer)
        If bar.InvokeRequired Then
            bar.Invoke(DirectCast(Sub() ResetBar(tot), Action))
        Else
            ResetBar(tot)
        End If
        Dim f1 As Stream = resp.GetResponseStream
        Dim f2 As Stream = New FileStream("C:\work\wasd1020.zip", FileMode.Create, FileAccess.Write)
        Dim sofar As Integer = 0
        Dim b(1000) As Byte
        Dim n As Integer
        Do
            n = f1.Read(b, 0, b.Length)
            If n < 0 Then Exit Do
            f2.Write(b, 0, n)
            sofar += n
            If bar.InvokeRequired Then
                bar.Invoke(DirectCast(Sub() UpdateBar(sofar), Action))
            Else
                UpdateBar(sofar)
            End If
        Loop
        f2.Close
        f1.Close
        resp.Close
        Catch ex As Exception
        MessageBox.Show(ex.Message)
        End Try
    End Sub
    Sub StartClick(ByVal sender As Object, ByVal e As EventArgs)
        t = New Thread(AddressOf Copy)
        t.Start
    End Sub
    Sub AbortClick(ByVal sender As Object, ByVal e As EventArgs)
        t.Abort
    End Sub
    <STAThread()> _
    Public Shared Sub Main(ByVal args As String())
        Application.Run(New MainForm)
        Application.Exit
        Environment.Exit(0)
    End Sub
End Class
Avatar billede kris914g1 Nybegynder
26. juni 2013 - 20:26 #4
undskyld jeg har været fraværende ide jeg har være på en 9 dags lang sejle tur men er komme så langt at jeg har fundet ud af at WEBCLIENT er vejen frem men kan man tilknytte det til checkboxes? fx hvis jeg havde 10 checkboxes så kunne jeg have 10 downloads? kender tror ikke jeg kender koden men har prøvet men når jeg fx har valgt to checkboxes så beder den om hvor de 2 filer skal saves men så er der kun 1 som bliver downloadet? nogen som ved hvad jeg gør galt?

kode:

Imports System.Net
Imports System.IO
Imports System.Diagnostics
Public Class Form1

    Private WithEvents WC As WebClient
#Region "Download RateVariables"
    Private ChangeInAmount As Integer
    Private PreviousAmount As Integer
    Private NextCheck As DateTime
#End Region


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setstatus("0 kb", "0kb", "0%", "0kb/s")
        WC = New WebClient
    End Sub
    Private Sub setstatus(CurDownload As String, ByVal TotalDownload As String, ByVal Percentage As String, ByVal Rate As String)
        Label1.Text = "Downloaded : " & CurDownload
        Label2.Text = "Download Size : " & TotalDownload
        Label3.Text = "Rate : " & Rate
        Label4.Text = "Percentage : " & Percentage
    End Sub
    Public Function CalculateRate(ByVal Input As Integer) As String
        Return Processbytes(Input) & "/s"
    End Function

    Public Function Processbytes(ByVal Input As Integer) As String

        'Check if the file is 1GB or more
        If Input >= 1073741824 Then
            Return Math.Round(CDbl(Input / 1073741824), 2) & " GB"
        Else
            'Check for MB
            If Input >= 1048576 Then
                Return Math.Round(CDbl(Input / 1048576), 2) & " MB"
            Else
                'Can only be in KB
                Return Math.Round(CDbl(Input / 1024), 2) & " KB"
            End If

        End If

    End Function

    Private Function CDb1(p1 As Double) As Double
        Throw New NotImplementedException
    End Function

    Private Sub WC_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
        ProgressBar1.Value = 0
        ProgressBar1.Maximum = 0
        ChangeInAmount = 0
        PreviousAmount = 0
        NextCheck = Now.AddSeconds(1)
    End Sub

    Private Sub WC_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
        Try
            If Now > NextCheck Then
                ChangeInAmount = e.BytesReceived - PreviousAmount
                PreviousAmount = e.BytesReceived
                NextCheck.AddSeconds(1)
            End If
            Dim Rate As String = CalculateRate(ChangeInAmount)
            ProgressBar1.Maximum = e.TotalBytesToReceive
            ProgressBar1.Value = e.BytesReceived
            setstatus(Processbytes(e.BytesReceived), Processbytes(e.TotalBytesToReceive), e.ProgressPercentage, Rate)
        Catch
        End Try
    End Sub
   
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then




            Dim webAddress As String = "url"
            Dim SFD As New SaveFileDialog
            If SFD.ShowDialog() = DialogResult.OK Then
                WC.DownloadFileAsync(New Uri("url"), SFD.FileName)
            End If

        End If

        If CheckBox2.Checked = True Then


            Dim webAddress As String = "url"
            Dim SFD As New SaveFileDialog
            If SFD.ShowDialog() = DialogResult.OK Then
                WC.DownloadFileAsync(New Uri("url"), SFD.FileName)
            End If

        End If

        If CheckBox3.Checked = True Then


            Dim webAddress As String = "url"
            Dim SFD As New SaveFileDialog
            If SFD.ShowDialog() = DialogResult.OK Then
                WC.DownloadFileAsync(New Uri("url"), SFD.FileName)
            End If

        End If
    End Sub
End Class
Avatar billede MadsHaupt Juniormester
06. juli 2013 - 08:21 #5
Du skal bare køre downloadingen i baggrunden.

Code:

Public Class Form1

    Dim downloader As New System.Net.WebClient
    Dim backgroundworker As New System.Threading.Thread(AddressOf Download)

    Private Sub Download()
        Try
            downloader.DownloadFile("address", "filename")
        Catch ex As Exception

        End Try
        backgroundworker = New System.Threading.Thread(AddressOf Download)
    End Sub

    Private Sub Cancel()
        downloader.CancelAsync()
    End Sub

    Private Sub StartDownload()
        backgroundworker.Start()
    End Sub
End Class
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester