Public Strings

cpgames

Member
Joined
Sep 14, 2011
Messages
14
Programming Experience
Beginner
Hello I have this code
VB.NET:
Public Class Frm_Download1
    Private myURL As String = "http://www.cpgames.co.uk/test.zip"
    Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & myURL.Substring(myURL.LastIndexOf("/"))
    Private WithEvents myWebClient As New Net.WebClient()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myWebClient = New Net.WebClient
        ProgressBar1.Value = 0
        Label1.Text = "Downloading..."
        myWebClient.DownloadFileAsync(New Uri(myURL), myFile)
    End Sub
    Private Sub myWebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles myWebClient.DownloadFileCompleted
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            MsgBox("Download Completed", MsgBoxStyle.Information)
        Else
            IO.File.Delete(myFile)
        End If
    End Sub
    Public Sub myWebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles myWebClient.DownloadProgressChanged
        Try
            Dim dDownloadProgress As Decimal = CStr(e.BytesReceived / e.TotalBytesToReceive * 100)
            ProgressBar1.Value = CInt(dDownloadProgress)
            Dim dDownloadedMB As Decimal = Format((e.BytesReceived / 1024) / 1024, "###,###,##0.00")
            Dim dTotalToDownloadMB As Decimal = Format((e.TotalBytesToReceive / 1024) / 1024, "###,###,##0.00")
            Label1.Text = "Downloaded: " & dDownloadedMB & "MB out of " & dTotalToDownloadMB & "MB - Progress: " _
                & Format(dDownloadProgress, "###.00") & "%"
        Catch ex As Exception
        End Try
    End Sub
End Class

That works Fine but I need to change
VB.NET:
http://www.cpgames.co.uk/test.zip

To

VB.NET:
textbox1.text

I keep getting errors and i cant seem to fix it can you help and btw the code download a item and tells you the percent and the size it has download sorry for the bad spelling i am only 15 :D
 
Please don't tell us that you get errors and then not tell us what the errors are and where they occur. You will always be given an error message and pretty much always told exactly where the error occurred. That information is to help you diagnose the problem. If you want us to help you diagnose the problem then it follows that you pass that information, and anything else that might be relevant, to us.
 
ok sorry what I am trying to do is when a user clicks on a different buttons a diffrent url goes in to textbox1 so when they click download it downloads the item they click on it all works apart form changing the string url to whast in textbox1 i now get no errors but when i try to click on the button that opens this forum i get this error
VB.NET:
An unhandled exception of type 'System.InvalidOperationException' occurred in Mods and More.exe
Additional information: An error occurred creating the form. See Exception.InnerException for details.  The error is: Object reference not set to an instance of an object.

Please help charlie
 
Right, now that you have provide all the relevant information it is quite obvious what the issue is. You have presumably done this:
VB.NET:
Private myURL As String = TextBox1.Text
The problem is that that code is executed before the form constructor, which means before the controls on your form have been created. If the TextBox1 variable is Nothing because the TextBox hasn't been created, there is no Text property to get.

The thing is, event if the TextBox did exist at that point, what purpose would it serve to get its Text then anyway? Obviously you want to get the value entered by the user so you have to get the Text AFTER the user enters it. If you want to perform the download when the user clicks a Button then wouldn't it make sense to get the Text of the TextBox in the Click event handler of that Button?
 
You will do it exactly how I said to do it: do not initialise the variable where you declare it and then assign the Text of the TextBox to the variable in the Click event handler of the Button. Presumably you know how to create a Click event handler for a Button and you obviously already know how to get the Text from the TextBox and assign it to the variable, so what's the issue?
 
Back
Top