none
Can't I select png files which have letters in their names?

    Question

  • 1) An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

    2) Additional information: Illegal characters in path.

    These are the following two errors which popup during the execution of the program 

    Here is the code I have written: 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
    
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & "*.png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
            Next
    
            Sprites(1).Pic.Name = "Heavy Panzer"
            Sprites(2).Pic.Name = "Heavy Scorcher"
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
    
            Form1_Resize(1, Nothing)
        End Sub

    The specific error is in the line under .backgroundimage and the file location is correct and the file names of the selected 3 are: tigerjet.png, TANKA.png and TANKB.png


    WRA

    Saturday, March 25, 2017 8:02 PM

Answers

  • Ohhhh so write the actual file location before doing anything else further 

    WRA

    Per loop, it's a new instance:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.IO.Path
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Const baseDirectory As String = _
                "C:\Users\BusinessOffice1Room\Documents"
    
            For i As Integer = 1 To 3
                Dim fi As New  _
                    FileInfo(Combine(baseDirectory, _
                                     i.ToString) & ".png")
    
                If fi.Exists Then
                    ' Now you know you can use it
                End If
            Next
    
            Stop
    
        End Sub
    End Class
    

    See?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 9:31 PM
    Saturday, March 25, 2017 9:16 PM

All replies

  • Waliur,

    Look at using the Path class of System.IO and specifically the .Combine method.

    Let it combine things to form a complete path. Once you have that, test that it exists before you use it (or you'll get a System.IO.FileNotFoundException).

    My preference is for IO.FileInfo but you can also use IO.File.Exists.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 8:24 PM
  • Waliur,

    Take out the "*" and try it, but do look into using the .Combine method and test that it exists first.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 8:35 PM
  • Sounds like the best idea because the first option that I so called prefer is doing my head in with the fact I need to name these files "1 2 3" just for them to open up. 

    I'll report back if any problems occur but I hope not since I will be importing the files into my vb project and all I gotta do is set their positions.


    Saturday, March 25, 2017 8:36 PM
  • Sounds like the best idea because the first option that I so called prefer is doing my head in with the fact I need to name these files "1 2 3" just for them to open up. 

    I'll report back if any problems occur but I hope not since I will be importing the files into my vb project and all I gotta do is set their positions.


    For example:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.IO.Path
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim i As Integer = 1
    
            Dim imagePath As String = _
                Combine("C:\Users\BusinessOffice1Room\Documents\", _
                        i.ToString) & ".png"
    
            Dim fi As New FileInfo(imagePath)
    
            If fi.Exists Then
                ' Now do the rest...
            End If
    
            Stop
    
        End Sub
    End Class

    When it gets to "Stop", it will stop just as though you'd put a breakpoint in. When it does, hover your mouse over "imagePath" and you'll see the full path.

    Make sense?

    If you have several files to get, you might be better off with an actual iteration of the file in the file system.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 8:41 PM
  • I see so put a for loop like I did in the previous example that I shown at the start

    WRA

    Saturday, March 25, 2017 8:45 PM
  • Youv'e done the starters and you want me to do the main course for the code.

    WRA

    Saturday, March 25, 2017 8:48 PM
  • I see so put a for loop like I did in the previous example that I shown at the start

    WRA

    Well, do understand that this will only work on that computer, but at any rate, test that it exists first.

    IO.File.Exists is lighter weight than FileInfo but I like using the instance properties/methods of FileInfo rather than "repeating myself" with the path (IO.File uses shared methods).

    So you'll know -- when it stops, hover your mouse over the variable "fi" and you'll see all kinds of good information about the file (if it exists, that is).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 8:50 PM
  • Do I need to fill in the details of the If statement just to clarify and with what do I fill it in with?

    WRA

    Saturday, March 25, 2017 8:55 PM
  • Yes I checked before the stopping of the program I found the file. 

    WRA

    Saturday, March 25, 2017 8:56 PM
  • @Frank 

    Is the expected answer suppose to be 1.png

    The reason I ask is I tested by checking with the use of msgbox and it shows the file doesn't exist. 


    WRA


    Saturday, March 25, 2017 8:57 PM
  • Do I need to fill in the details of the If statement just to clarify and with what do I fill it in with?

    WRA

    Well, do understand: Here's the actual cause of the exception:

    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & "*.png")

    The asterisk is an illegal character is the problem.

    By the way, IO.Path.GetInvalidPathChars will also tell you the illegal characters if you want but IO.FileInfo will toss an exception back your way if you have an illegal path of any sort, including the length of the path.

    Path.Combine is still a better way. It knows how to take what you hand it and give you back a valid path.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Saturday, March 25, 2017 9:02 PM ...added link to MSDN documentation
    Saturday, March 25, 2017 9:00 PM
  • I see so that asterisk is just for show and pointless

    WRA

    Saturday, March 25, 2017 9:01 PM
  • Do I need to fill in the details of the If statement just to clarify and with what do I fill it in with?

    WRA

    Yes - try it.

    You won't know otherwise. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:03 PM
  • I think will just take the easiest and most understanding route possible. Thanks for sharing some of the path alternatives btw. 

    WRA

    Saturday, March 25, 2017 9:04 PM
  • Sounds good. 

    Question: why doesn't msdn ever do live chat supports like they do with xbox customer and support and stuff?I always wondered that because microsoft is a big company


    WRA

    Saturday, March 25, 2017 9:05 PM
  • I see so that asterisk is just for show and pointless

    WRA

    You can use a wildcard character in some methods (like "getting" files), but in this case, it's invalid.

    You can't use that in what you're doing any more than you can rename a file with an asterisk in it.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:05 PM
  • Sounds good. 

    Question: why doesn't msdn ever do live chat supports like they do with xbox customer and support and stuff?I always wondered that because microsoft is a big company


    WRA


    You'd have to ask them. ;-)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:07 PM
  • I see 

            If fi.Exists Then
                ' Now do the rest...
                Dim pic As New PictureBox
                With Pic
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & ".png")
                End With
                MsgBox("File extracted")
            Else
                MsgBox("File doesn't exist ")
            End If

    I have done this so far since the file I'm selecting I want to create a sprite with it but it still shows that the file doesn't exist


    WRA

    Saturday, March 25, 2017 9:07 PM
  • sounds good, Ill send them an email 

    I was actually before this gonna move to setting the range of how far can the sprites attack but I thought nope. I gotta deal with this first because I can't be calling all these tanks 1.png etc its too simplistic and examinor would probably think I'm lazy


    WRA


    Saturday, March 25, 2017 9:07 PM
  • I see 

            If fi.Exists Then
                ' Now do the rest...
                Dim pic As New PictureBox
                With Pic
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & ".png")
                End With
                MsgBox("File extracted")
            Else
                MsgBox("File doesn't exist ")
            End If

    I have done this so far since the file I'm selecting I want to create a sprite with it but it still shows that the file doesn't exist


    WRA

    You have the order of things wrong:

    Create the path in the loop, then create a new instance of FileInfo using that path. Now test that it exists and if so - do the rest.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:10 PM
  • Ohhhh so write the actual file location before doing anything else further 

    WRA

    Saturday, March 25, 2017 9:13 PM
  • Ohhhh so write the actual file location before doing anything else further 

    WRA

    Per loop, it's a new instance:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.IO.Path
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Const baseDirectory As String = _
                "C:\Users\BusinessOffice1Room\Documents"
    
            For i As Integer = 1 To 3
                Dim fi As New  _
                    FileInfo(Combine(baseDirectory, _
                                     i.ToString) & ".png")
    
                If fi.Exists Then
                    ' Now you know you can use it
                End If
            Next
    
            Stop
    
        End Sub
    End Class
    

    See?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Waliur Rahman Saturday, March 25, 2017 9:31 PM
    Saturday, March 25, 2017 9:16 PM
  • I got it, it shows the file exists via indications of the msgbox

     If fi.Exists Then
                imagePath = Combine("C:\Users\BusinessOffice1Room\Documents\",
                        i.ToString) & ".png")
                        ' Now do the rest...  
                MsgBox("File extracted")
            Else
                MsgBox("File doesn't exist ")
            End If

    But shows this 


    WRA

    Saturday, March 25, 2017 9:17 PM
  • @Frank

    Don't worry about the issue I just mentioned its because I put a bracket near .png so that is solved and it shows the file is extracted. 


    WRA

    Saturday, March 25, 2017 9:18 PM
  • @Frank

    Don't worry about the issue I just mentioned its because I put a bracket near .png so that is solved and it shows the file is extracted. 


    WRA


    I'm a bit lost in all of this but hopefully you've got it worked out now?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:19 PM
  • yes I did apologies If I confused you anywhere

    WRA


    Saturday, March 25, 2017 9:20 PM
  • @Frank

    Alright, now that the file path is precise I just need to use the way to implement them as sprites so I could use pieces of code that I have used from my old versions


    WRA

    Saturday, March 25, 2017 9:21 PM
  • yes I did apologies If I confused you anywhere

    WRA


    Ok, good then.

    The framework has a lot of good classes to work with files and directories, so hopefully some of what I showed you will come in handy down the road. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:22 PM
  • sounds good I'll msg if a problem occurs. Ill be using the following code here but in doubt which parts are suppose to go in. 

    Private Class Sprite
            Public Name As String
            Public Pic As PictureBox
            Public ColRow As Point
            Public Steps As Integer = -1    '-1 = unlimited move
            Public Visible As Integer = 2   '0 = not visible  1= Frozen  2=Active   
            Public Range As Integer
            Public AP As Integer
            Public HP As Integer
        End Class

    This part of course because it outlines the characteristics of the sprites

    Then it is this part where there is a .backgroundimage (do I replace that with the code with works)

        Private Sprites As New List(Of Sprite)
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
    
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
            Next
    
            Sprites(1).Pic.Name = "Heavy Panzer"
            Sprites(2).Pic.Name = "Heavy Scorcher"
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
    
            Form1_Resize(1, Nothing)
        End Sub


    WRA

    Saturday, March 25, 2017 9:24 PM
  • sounds good I'll msg if a problem occurs. Ill be using the following code here but in doubt which parts are suppose to go in.

    Waliur,

    That's getting off course for the question you asked.

    If I answered it - hopefully I did - then close this thread by marking me as the answerer and ask a new one for what you're showing.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:29 PM
  • Better proposed idea Frank, Ill get into that habit where I won't chain questions together

    WRA

    Saturday, March 25, 2017 9:30 PM
  • Better proposed idea Frank, Ill get into that habit where I won't chain questions together

    WRA

    :)

    It's not being rude - think about it:

    If it's closely related and "makes sense" to be in the same thread, then sure -- let's go there and explore that, but once it's way out, well think about someone who finds the thread later because they have a similar problem.

    Anyway, I hope it all helped. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 9:35 PM
  • Yeh I get the approach you are taking and yes i know you are not being rude or anything you are just proposing a better idea for me and i like that

    WRA

    Saturday, March 25, 2017 9:54 PM
  • This part of course because it outlines the characteristics of the sprites

    You are still thinking in terms of the pictureboxes instead of your sprites.  The pictureboxes are simply a tool for displaying and manipulating.  The sprites are the objects that your game play works with.  Don't use the picturebox name for the name of the sprite - add a new property to your Sprite class called Name.

    Public Class Sprite
        Public Property Name As String
    Public Property pic As PictureBox Public Property ColRow As Point Public Property HP As Integer Public Property AP As Integer End Class

    Then, to use names instead of numbers for your image files, you can name the files the same as the sprite name, and use the sprite name to find the correct file when you create the pictureboxes.  This requires setting up the sprites before doing the pictureboxes and the images, which is probably sensible anyway. 

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DoubleBuffered = True
            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)
            Sprites(0).Name = "Light Battle"
            Sprites(0).HP = 10
            Sprites(0).AP = 5
            Sprites(1).Name = "Heavy Panzer"
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).Name = "Heavy Scorcher"
            Sprites(2).HP = 10
            Sprites(2).AP = 5
    
            For i = 0 To 2
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    Dim ImageFile As String = System.IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\", Sprites(i).Name & ".png")
                    .BackgroundImage = Image.FromFile(ImageFile)
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf Pics_MouseMove
                    AddHandler .MouseUp, AddressOf Pics_MouseUp
                End With
                Sprites(i).pic = pic
                Sprites(i).ColRow = New Point(i)
            Next
    
        End Sub

    • Edited by AcamarMVP Saturday, March 25, 2017 9:58 PM sp
    Saturday, March 25, 2017 9:58 PM
  • I see cheers. 

    WRA

    Saturday, March 25, 2017 10:04 PM
  • Hi Acamar, an error ocurred 


    WRA

    Saturday, March 25, 2017 10:09 PM
  • an error occurred 

    Please show the code you are using.  If you have this:

            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)

    and this

            For i = 0 To 2
    then this:
    Sprites(i).Name
    cannot produce the error you have posted.



    Saturday, March 25, 2017 10:41 PM
  • Alright this is the following code I have 

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DoubleBuffered = True
    
    
            For i = 0 To 2
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    Dim ImageFile As String = IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\", Sprites(i).Name & ".png")
                    .BackgroundImage = Image.FromFile(ImageFile)
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
                Sprites(i).Pic = pic
                Sprites(i).ColRow = New Point(i)
            Next
            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)
            Sprites.Add(New Sprite)
            Sprites(0).Name = "Heavy Panzer"
            Sprites(0).HP = 10
            Sprites(0).AP = 5
            Sprites(1).Name = "Heavy Artillery"
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).Name = "Heavy Scorcher"
            Sprites(2).HP = 10
            Sprites(2).AP = 5
    
        End Sub


    WRA

    Saturday, March 25, 2017 10:56 PM
  • Alright this is the following code I have

    That won't work.  Use what I posted.  Then think about why it won't work, and do not proceed until you have precisely identified the reason.

    Saturday, March 25, 2017 11:20 PM
  • I'll do tomorrow I have just gone way tired. I worked for hours today.

    literally 11:10 in the morning to 12:15 then 1pm to 2:30.

    2:30 to 3:20 

    4:00 to 4:30 

    7:30 to 9

    last was 9 to 10

    thats a sum of 385 minutes which is nearly 7hrs for a days work


    WRA

    Saturday, March 25, 2017 11:50 PM
  • Hey acamar just to make life easy I made a new thread and leave messages there

    WRA

    Sunday, March 26, 2017 11:48 AM