Click here to Skip to main content
15,799,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Excuse me, I have a program and I got some error. there Is error in "Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)" from my program, please help me for my studying. thank you, sorry for my bad English.

What I have tried:

C#
Private Sub namafiledeskrip()
        Dim posisi As Integer = 0
        Dim t As Integer = 0

        While TextBox2.Text.IndexOf("\"c, t) <> -1
            posisi = TextBox2.Text.IndexOf("\"c, t)
            t = posisi + 1
        End While

        Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)
        fileoutput = fileoutput.Substring((posisi + 1))
        RichTextBox2.Text = fileoutput.Replace("_"c, "."c)


    End Sub
Posted
Updated 26-Nov-16 20:33pm
Comments
Afzaal Ahmad Zeeshan 26-Nov-16 14:54pm    
Index must be zero or greater, that is why the problem.
Member 11905957 27-Nov-16 9:23am    
Thank You Sir

The error message is pretty explicit: "Length cannot be less than zero"
Since you are passing the length based on the Textbox content:
VB
Dim fileoutput As String = TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)

The problem is almost certainly that the text box contains three characters or less...
 
Share this answer
 
Comments
Member 11905957 26-Nov-16 12:31pm    
yes sir, my text box is contains more 3 characters, so what have I change Sir from my code? thank for helping me Sir..
OriginalGriff 26-Nov-16 12:36pm    
How are you sure? What does the debugger say? The error message say it doesn't...
Are you sure you are using the right textbox?
Member 11905957 27-Nov-16 8:41am    
oh yeah, thank you, i get it sir, its work, thank you
OriginalGriff 27-Nov-16 8:51am    
You're welcome!
Member 11905957 27-Nov-16 9:09am    
Sir, can you help me again?
Based on the error message, apparently your TextBox2.Text.Length is less than 4 which resulted in negative value from their subtraction. You can't have a negative length for the second argument of Substring() method.
Look like you are trying to remove the dot extension from your file path, if so, you should first ascertain that that dot extension exists, e.g.
C#
Dim value As String = "peterleow/haha.html"

If value.LastIndexOf("."c) <> -1 Then
    Console.WriteLine(value.Substring(0, value.LastIndexOf("."c)))
End If


Learn LastIndexOf[^]
 
Share this answer
 
v5
Comments
Member 11905957 27-Nov-16 8:42am    
Thank you Sir
VB
TextBox2.Text.Substring(0, TextBox2.Text.Length - 4)

This extract the contain of TextBox2 minus the last 4 chars, this imply that it will fail if there is not at least 4 chars.
To proof your code, you gave to check if length is more than 4 and tell what to do otherwise.
 
Share this answer
 
Comments
Member 11905957 27-Nov-16 8:42am    
Thank you Sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900