Results 1 to 4 of 4

Thread: [BEGINNER HElP] Multiple pictures extensions choices

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation [BEGINNER HElP] Multiple pictures extensions choices

    Hello, i am writting a little program and i want to select a picture with one extensions but several possibilities. my first option is .png but i want to add ".gif"vpicture if "png" picture is not found.
    i am starting in coding. i tried this:
    Code:
    PictureBox9.Image = Image.FromFile(Application.StartupPath & "\DANfiles\challengersgif\" & Me.ComboBox1.Text & (".png" or ".gif"))
    but i have a error!
    please! give me a hand ! i am looking for a solution. thank you in advance for your answer!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [BEGINNER HElP] Multiple pictures extensions choices

    vb.net Code:
    1. Dim filePath = Path.Combine(Application.StartupPath,
    2.                             "DANfiles\challengersgif",
    3.                             Me.ComboBox1.Text & ".png")
    4.  
    5. If Not File.Exists(filePath) Then
    6.     filePath = Path.ChangeExtension(filePath, ".gif")
    7. End If
    8.  
    9. 'Use filePath here.
    You may want to add a check to see that the second file path exists if there's a possibility that it might not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [BEGINNER HElP] Multiple pictures extensions choices

    Here's another option that incorporates a check for at least one existent file and can be easily extended to more extensions just by adding more elements to the array:
    vb.net Code:
    1. Dim filePath = Path.Combine(Application.StartupPath,
    2.                             "DANfiles\challengersgif",
    3.                             Me.ComboBox1.Text)
    4. Dim extensions = {"png", "gif"}
    5.  
    6. filePath = extensions.Select(Function(ext) Path.ChangeExtension(filePath, ext)).
    7.                       FirstOrDefault(Function(fp) File.Exists(fp))
    8.  
    9. If filePath IsNot Nothing Then
    10.     'Use filePath here.
    11. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [BEGINNER HElP] Multiple pictures extensions choices

    ok thank you very much for your help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width