none
How to display next picture in picturebox from list variable RRS feed

  • Question

  • Hi, I need help please,

    I have my below code but it's not working, what I want is to be able to move forward and backward pictures stored in list variable named "allPictures", and to be displayed in a picturebox, and have a small label to display which position is currently (Integer)

    I also have two buttons (Back & Next) to respectivelly move backward and forward pictures, and at the same time update the label current position with a number (1 = first picture, 2= second picture, and so on)

    Personally, my list variable already has pictures' name in it but the only thing I'm unable to accomplish is to display the next picture's name from the list variable in the picturebox when I click "Next Button" and "Back" button

         Dim mypict As String = ""
                Dim count As Integer = 0
    
                For Each pict In allPictures
    
    	count += 1
    
                PictureBox1.Visible = True
                Dim fullpathtopict As String = ("C:\myPicture\" & mypict & count)
                PictureBox1.Image = System.Drawing.Image.FromFile(fullpathtopict)
    
                Next
    


    Monday, March 6, 2017 9:15 AM

Answers

  • Thanks tommytwotrain.

    I got rid of loop.

    but How do I show the pic that corresponds to the count?

    and it's a list variable !!!  how do I read a list variable step by step (click) ?

    Cause I always read a list variable via a "For Each Next..." statement !!!  

    I don't know another way to read list one by one manually.



        Private count As Integer
        Private Pics As new List(Of String)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Pics.Add("c:\bitmaps\rusty.jpg")
            Pics.Add("c:\bitmaps\kellymilly.png")
            Pics.Add("c:\bitmaps\lilly1.jpg")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
            PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(Pics(count))
        End Sub

    PS a list is similar to an array it just has more features.

    • Edited by tommytwotrain Monday, March 6, 2017 2:33 PM
    • Marked as answer by LetMeCode Monday, March 6, 2017 4:03 PM
    Monday, March 6, 2017 2:30 PM
  • LMC,

    Food for thought: You have a control that's perfect for this if you just think it through some:

    In the following I'm using a List(Of Object) but that's just so you can duplicate this to test it. In reality it'll be a List(Of String) - like Tommy showed - or any other way you want to set it up:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private myList As List(Of Object) Private currentItemNumber As Integer Private WithEvents bs As New BindingSource Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load myList = New List(Of Object) For i As Integer = 1 To 10 myList.Add(New Object) Next bs.DataSource = myList BindingNavigator1.BindingSource = bs End Sub Private Sub _ bs_PositionChanged(sender As Object, _ e As System.EventArgs) _ Handles bs.PositionChanged MessageBox.Show(bs.Position.ToString("n0")) End Sub End Class


    Now your code needs to react to the position of the BindingSource being changed. In the example I'm just showing the position (also zero-based) and in the real one, show the image that's at that index.


    "One who has no vices also has no virtues..."

    • Marked as answer by LetMeCode Tuesday, March 7, 2017 2:11 PM
    Monday, March 6, 2017 2:56 PM

All replies

  • I have my below code but it's not working, what I want is to be able to move forward and backward pictures stored in list variable named "allPictures", and to be displayed in a picturebox, and have a small label to display which position is currently (Integer)

    Show the code for the Next button click event and the Back button click event. The code you have posted above does not include any of the elements you refer to in your description other than the picture box. You need to show how you are using the list of image filenames and the counter where you are recording the current image index.

    Monday, March 6, 2017 10:41 AM
  • That's the only code.
    Monday, March 6, 2017 11:16 AM
  • That's the only code.

    This is not hard to do.

    The code you have shown is not even close to what say you want to do.

    You need a button click event at least.

    So we can either insult you for not trying, or just give you the code and hope you go away, or we can try to help you understand how to write code.

    Which will it be?

    Monday, March 6, 2017 1:14 PM
  • the above code is in a button of course !!!

    I just cut it.

    Monday, March 6, 2017 1:28 PM
  • the above code is in a button of course !!!

    I just cut it.

    Ok. I cant see your screen from here.  :)

    You should think of what we need to know to help you.

    What do you think the code you show should do? Its a for loop. Do you want to show all the pics in the picturebox when you click the button? Thats the code you wrote. Why do you think that would work.?

    Explain what you think is supposed to happen with your code. Each line if needed. Lets see where your misunderstanding is.

    Otherwise. Start over using what Acamar described. Then show us how far you get with that. And there should be two button click events.

    I am just trying to help you understand whatever you are not understanding. It seems simple to me. So you need to tell us what is not simple to you.

    Monday, March 6, 2017 1:33 PM
  •  Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim mypict As String = ""
    
            For Each mypict In allPictures
    
                PictureBox1.Visible = True
    
                Dim fullpathtopict As String = (Application.StartupPath & "\pict\" & mypict)
                MsgBox("After" & fullpathtopict)
                PictureBox1.Image = System.Drawing.Image.FromFile(fullpathtopict)
    
            Next
    
        End Sub

    I'm able to display all the pictures stored in "allPictures" list variable if I put a MsgBox,

    So now the only problem I'm facing is I'm unable to display second one and so forth in picturebox one by one if I click on "Button1"

    I managed to display the first one when the window loads but the rest (second and...) I'm still struggling.


    • Edited by LetMeCode Monday, March 6, 2017 1:41 PM
    Monday, March 6, 2017 1:39 PM
  • Hello LetMeCode

    I think we should let you code. Be aware that writing this for me takes more time than making this code. However, in contrary to some others I want to teach fish instead of giving fish. 

    What you have to create is 2 buttons on a form and a Picturebox that is all. One of the Buttons you name Foreward the other you name backward. 

    You create global a List. Keep in mind that a list of images in memory eats that memory, so that list can also contain file names and you read those everytime you want to show them from the list.

    Also global you create an index for the current image which is showed.

    In the load event of the form you show the image in the list  with the first index which is initial set by VB to zero.  

    Now you create two methods one is handled by the foreward button click and one by the backward click. 

    In that method you check with foreward if the index is not the same as the last one. Then you do simple nothing otherwise you add one to the index.

     

    You show the image in the list with the then gotten index. 

    The same you do with the backward button, but now you check if the index is not 0. Then you do nothing. Otherwise you subtract one from the index and show the image with the tan gotten image. 

     


    Success
    Cor


    Monday, March 6, 2017 1:42 PM
  • Okay, the above code show all the pics in the picturebox.

    so how do I show one pic by click in the picturebox?

    by the way, I added "Threading.Thread.Sleep(3000)" below "For" statement, and it worked using a timer (automatically) whereas I need it manually.

    Please I need a clue !




    • Edited by LetMeCode Monday, March 6, 2017 1:49 PM
    Monday, March 6, 2017 1:45 PM
  • I showed how to do it, 

    You use as Nick Let me code, but do you mean in fact, IOrderForCode so I can copy and paste?


    Success
    Cor

    Monday, March 6, 2017 1:47 PM
  • Okay, the above code show all the pics in the picturebox.

    so how do I show one pic by click in the picturebox?

    by the way, I added "Threading.Thread.Sleep(3000)" below "For" statement, and it worked using a timer (automatically) whereas I need it manually.

    Please I need a clue !




    Why are you looping through all the pics? Just show the pic that corresponds to the count, then increment the count for next time. Done. Get rid of the loop. You dont need a timer (unless you want it to run automatically through all the pics all the time??).

    If you want to show one pic when you click the back or next button then just show the one pic for the count. Done. Wait for next click.

    If you want to click go and show all pics one second at a time and keep going then you need a timer more like you show and a go button to start the animation.

    Monday, March 6, 2017 1:57 PM
  • Thanks tommytwotrain.

    I got rid of loop.

    but How do I show the pic that corresponds to the count?

    and it's a list variable !!!  how do I read a list variable step by step (click) ?

    Cause I always read a list variable via a "For Each Next..." statement !!!  

    I don't know another way to read list one by one manually.



    • Edited by LetMeCode Monday, March 6, 2017 2:07 PM
    Monday, March 6, 2017 2:05 PM
  • You still did not look to what I wrote and Tommy wrote in fact less in steps the same.

    Success
    Cor


    Monday, March 6, 2017 2:11 PM
  • LMC,

    There are three numbers of import here:

    1) Zero. Your collection is zero-based so the first one is zero.

    2) The count of your collection minus one (again, because it's zero-based).

    3) The current number. The current number will increase/decrease based on an event - the click of a button in this case. Your code needs to do that and - because the current number has changed, react to that change by showing the picture referenced at that index number.

    Think that through some and I think you'll see that it's really pretty simple.


    "One who has no vices also has no virtues..."

    Monday, March 6, 2017 2:19 PM
  • @ Cor Ligther.

    I read okay.

    first of all, How do I show the index??

    Monday, March 6, 2017 2:26 PM
  • Thanks tommytwotrain.

    I got rid of loop.

    but How do I show the pic that corresponds to the count?

    and it's a list variable !!!  how do I read a list variable step by step (click) ?

    Cause I always read a list variable via a "For Each Next..." statement !!!  

    I don't know another way to read list one by one manually.



        Private count As Integer
        Private Pics As new List(Of String)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Pics.Add("c:\bitmaps\rusty.jpg")
            Pics.Add("c:\bitmaps\kellymilly.png")
            Pics.Add("c:\bitmaps\lilly1.jpg")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
            PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(Pics(count))
        End Sub

    PS a list is similar to an array it just has more features.

    • Edited by tommytwotrain Monday, March 6, 2017 2:33 PM
    • Marked as answer by LetMeCode Monday, March 6, 2017 4:03 PM
    Monday, March 6, 2017 2:30 PM
  • Some code then, look at what Frank and Tommy wrote and then to answer you in the methods is in both the code (be aware that index and count are just names we use)

    ThePictureBox.Image = Image.FromFile(List(index))

    And then the rest again related to what I wrote. 


    Success
    Cor



    Monday, March 6, 2017 2:33 PM
  • tommytwotrain

    your above example only shows the second image on the list.

    don't know why!

    Monday, March 6, 2017 2:49 PM
  • tommytwotrain

    your above example only shows the second image on the list.

    don't know why!

    I give up

    Success
    Cor

    Monday, March 6, 2017 2:52 PM
  • LMC,

    Food for thought: You have a control that's perfect for this if you just think it through some:

    In the following I'm using a List(Of Object) but that's just so you can duplicate this to test it. In reality it'll be a List(Of String) - like Tommy showed - or any other way you want to set it up:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private myList As List(Of Object) Private currentItemNumber As Integer Private WithEvents bs As New BindingSource Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load myList = New List(Of Object) For i As Integer = 1 To 10 myList.Add(New Object) Next bs.DataSource = myList BindingNavigator1.BindingSource = bs End Sub Private Sub _ bs_PositionChanged(sender As Object, _ e As System.EventArgs) _ Handles bs.PositionChanged MessageBox.Show(bs.Position.ToString("n0")) End Sub End Class


    Now your code needs to react to the position of the BindingSource being changed. In the example I'm just showing the position (also zero-based) and in the real one, show the image that's at that index.


    "One who has no vices also has no virtues..."

    • Marked as answer by LetMeCode Tuesday, March 7, 2017 2:11 PM
    Monday, March 6, 2017 2:56 PM
  • tommytwotrain

    your above example only shows the second image on the list.

    don't know why!

    My example shows the first image in the list.

    Here is my list:

            Pics(0) is "c:\bitmaps\rusty.jpg"
            Pics(1) is "c:\bitmaps\kellymilly.png"
            Pics(2) is "c:\bitmaps\lilly1.jpg"

    My count =0 so it shows the first pic in the list, lists start at zero. When count = 1 (count is same as index) then the pic is the second item in the list at index = count = 1 = "c:\bitmaps\kellymilly.png"

    So when you click button next you show the count, then you increment the count +1, then you wait for the next click. On the next click count is 1. You will see the second item in the list. Because starting from zero there are two items, 0 and 1.


    PS my variable count can be changed to tomCount. It is unrelated to the list. It is not the same a list.count, or list index. I just picked the word count. I should have used mycount, or i or someothercountname instead of count then there is no confusion with list.count which is the total items in the list.
    Monday, March 6, 2017 3:10 PM
  • Thanks everybody for your time.

    @ tommytwotrain

    Thanks, it's work perfect !!! Great Thanks.

    I just added ( count += 1 ) after the last statement and got it done.

    Private count As Integer
        Private Pics As new List(Of String)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Pics.Add("c:\bitmaps\rusty.jpg")
            Pics.Add("c:\bitmaps\kellymilly.png")
            Pics.Add("c:\bitmaps\lilly1.jpg")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
            PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(Pics(count))
    
            count += 1
    
        End Sub

    But I still have few questions, please.

    How do i get the current position of the picture in a labelbox?

    I mean like in your code snippet,

    if I click next button, it does display the image and it has to show in a labelbox which position is the current picture (an integer).

    Please see picture below

    I tried with ".count" method and I got the total number of pictures in the list.

    • Edited by LetMeCode Monday, March 6, 2017 3:46 PM
    Monday, March 6, 2017 3:43 PM
  • and how do I delete a picture on a current position.
    Monday, March 6, 2017 3:52 PM
  • Thanks everybody for your time.

    @ tommytwotrain

    Thanks, it's work perfect !!! Great Thanks.

    I just added ( count += 1 ) after the last statement and got it done.

    Private count As Integer
        Private Pics As new List(Of String)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Pics.Add("c:\bitmaps\rusty.jpg")
            Pics.Add("c:\bitmaps\kellymilly.png")
            Pics.Add("c:\bitmaps\lilly1.jpg")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
            PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(Pics(count))
    
            count += 1
    
        End Sub

    But I still have few questions, please.

    How do i get the current position of the picture in a labelbox?

    I mean like in your code snippet,

    if I click next button, it does display the image and it has to show in a labelbox which position is the current picture (an integer).

    Please see picture below

    I tried with ".count" method and I got the total number of pictures in the list.

    See the ps in my last post. Just change my counter name from count to something else like picnumber.

    Then label.text = picnumber.

    My count variable is not the same as the list.count. Just rename it to something else. Here I changed it to picnumber to avoid confusion with list.count. This is why we try to avoid using the same variable names as vb uses, which I should have done.

        Private picnumber As Integer
        Private Pics As New List(Of String)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Pics.Add("c:\bitmaps\rusty.jpg")
            Pics.Add("c:\bitmaps\kellymilly.png")
            Pics.Add("c:\bitmaps\lilly1.jpg")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
            PictureBox1.BackgroundImage = System.Drawing.Image.FromFile(Pics(picnumber))
        End Sub

    Monday, March 6, 2017 3:54 PM
  • Thanks so much !!

    By the way, How do I proceed to delete the current picture?

    Can I just delete the real file? or I also need to make change into "Pics" List variable


    Monday, March 6, 2017 4:02 PM
  • How do i get the current position of the picture in a labelbox?

    I see that you've shown the BindingNavigator from the example that I posted so you might be confused here. Once it's bound to the data (your list), the number and count shown is automatically changed as you navigate.

    ...and how do I delete a picture on a current position.
    If you're talking about with my example, it uses a BindingSource as the "go-between" and if you want to remove the picture's reference from the datasource (the List(Of Type) here), you can use the the .RemoveCurrent method. The image file itself is untouched; it just removes the reference from the datasource - i.e., it removes it from your list.

    If the image in your PictureBox is from the file path of a local image file, the image in the PictureBox won't change; you'll have to handle that yourself.


    "One who has no vices also has no virtues..."


    Monday, March 6, 2017 4:27 PM
  • Thanks so much !!

    By the way, How do I proceed to delete the current picture?

    Can I just delete the real file? or I also need to make change into "Pics" List variable


    "Can I just delete the real file?"

    No of course not. Why would you do that?

    "By the way, How do I proceed to delete the current picture?"

    There is no picture! There is a list of strings that reference the file on the disc. There are dots on the screen.

    Just remove the ref for the pic from the list. See the list docs. Learn to use a list. Search for examples of deleting items from a list.

    :)

    Monday, March 6, 2017 4:50 PM
  • Thanks so much !!

    By the way, How do I proceed to delete the current picture?

    Can I just delete the real file? or I also need to make change into "Pics" List variable


    "Can I just delete the real file?"

    No of course not. Why would you do that?

    "By the way, How do I proceed to delete the current picture?"

    There is no picture! There is a list of strings that reference the file on the disc. There are dots on the screen.

    Just remove the ref for the pic from the list. See the list docs. Learn to use a list. Search for examples of deleting items from a list.

    :)

    Oh wait,, sorry, you really really want to delete the file?

    Yes you just delete the file on the disc. Update the list if needed.


    PS you may have to dispose the reference by showing another picture in the picturebox. Then you delete the file using the path from the list. But that is another question.
    Monday, March 6, 2017 4:54 PM
  • @ Frank L. Smith

    Hi, I'm trying to use your example.

    I'm stuck in couple of things :

    I'm able to show pictures correctly as you explained and I can nicely navigate using next/previous arrow control.

    But when I add new pictures, seems like it doesn't refresh the position bar automatically in order to display the total of new pictures just added.

    I'd like to use below statement but I don't know how to use it? 

    Private Sub BindingNavigator1_RefreshItems(sender As Object, e As EventArgs) Handles BindingNavigator1.RefreshItems
    
        End Sub

    and I need to delete the current picture? How do I do?

     Please can you help?? 

    Tuesday, March 7, 2017 2:11 PM
  • @ Frank L. Smith

    Hi, I'm trying to use your example.

    I'm stuck in couple of things :

    I'm able to show pictures correctly as you explained and I can nicely navigate using next/previous arrow control.

    But when I add new pictures, seems like it doesn't refresh the position bar automatically in order to display the total of new pictures just added.

    I'd like to use below statement but I don't know how to use it? 

    Private Sub BindingNavigator1_RefreshItems(sender As Object, e As EventArgs) Handles BindingNavigator1.RefreshItems
    
        End Sub

    and I need to delete the current picture? How do I do?

     Please can you help?? 

    My example is based on using a BindingSource. I assume that's what you have?

    If so, then adding and removing through the BindingSource will take care of it all, but if you add or remove directly (your list), then you need to tell the BindingSource to reread the data and you can do that with its .ResetBindings method:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings(v=vs.110).aspx


    "One who has no vices also has no virtues..."

    Tuesday, March 7, 2017 2:19 PM
  • Thanks so much.

    I used 

    BindingSource1.ResetBindings(True)

    Fantastic !! works great

    Can you now please help with the deletion?

    Tuesday, March 7, 2017 2:35 PM
  • Thanks so much.

    I used 

    BindingSource1.ResetBindings(True)

    Fantastic !! works great

    Can you now please help with the deletion?

    You can do the same if you want: Use the List's .Remove or .RemoveAt(index) method then reset the bindings again.

    You can also use the BindingSource's .RemoveCurrent method and it will do all of that for you:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.removecurrent(v=vs.110).aspx


    "One who has no vices also has no virtues..."

    Tuesday, March 7, 2017 2:41 PM
  • okay but How will it know that it deleting the exact file?  (I know I'll write the code to go delete the real file)

    I mean How will it know that this position (like "3") refer to a picture named "Bank Statement"?

    Tuesday, March 7, 2017 2:52 PM
  • okay but How will it know that it deleting the exact file?  (I know I'll write the code to go delete the real file)

    I mean How will it know that this position (like "3") refer to a picture named "Bank Statement"?

    It won't - that's not what this does.

    It removes the data from the source (your list), but it won't do anything more than that.

    If you're sure you want to do that then you could keep up with what's being deleted and do that yourself if you wanted to. I'm always reluctant to delete anything from a user's computer.


    "One who has no vices also has no virtues..."

    Tuesday, March 7, 2017 2:58 PM
  • Thanks.

    I'd like to keep it up with the deletion. Please can you give me some clue?

    And, why are you reluctant to delete anything from a user's computer?

    Tuesday, March 7, 2017 3:10 PM
  • Thanks.

    I'd like to keep it up with the deletion. Please can you give me some clue?

    And, why are you reluctant to delete anything from a user's computer?

    You have a List(Of String) and each of those strings are a file path to an image file, right?


    "One who has no vices also has no virtues..."

    Tuesday, March 7, 2017 3:13 PM
  • Yes.

    But seems like I need to access it via a Loop?

    anyway, waiting for your idea.

    Tuesday, March 7, 2017 5:23 PM
  • Yes.

    But seems like I need to access it via a Loop?

    anyway, waiting for your idea.

    This is one time where I don't think that using the BindingSource for the removal is a good idea.

    When the user has signaled (by a button click, for example) that they want to delete an image, first find the current index. You can do that any way you want but you've got to do this first - because the list will change soon and that index will be wrong.

    You can use the BindingSource's .Position property for this - or any other way you want:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.position(v=vs.110).aspx

    With that known, before you remove the source (ergo, before you remove it from your list), add the file path to a new list which you need to create in your program. Following that, remove it from the source and tell the BindingSource to update.

    At the end of things, you've essentially moved the image from one list to another. Later then decide how you want to do this: For example ask the user to confirm they want to delete the file(s) and maybe even offer to send them to the recycle bin instead.


    "One who has no vices also has no virtues..."

    Tuesday, March 7, 2017 5:30 PM
  • Hi Frank L.

    I did everything you suggested that is above the link and work perfect.

    but what is below, I couldn't accomplish it. 

    With that known, before you remove the source (ergo, before you remove it from your list), add the file path to a new list which you need to create in your program. Following that, remove it from the source and tell the BindingSource to update.
    
    At the end of things, you've essentially moved the image from one list to another. Later then decide how you want to do this: For example ask the user to confirm they want to delete the file(s) and maybe even offer to send them to the recycle bin instead.
    
    
     

    Below is my code. I created a New List and transfered filepath from old list to new created list like this :

    myNewList = myOldList

    and when I try to delete using below code I receive an error saying ( The process cannot access the file c:\mypath\...  because it is being used by another process. )

            IO.File.Delete(Application.StartupPath & "\myPict\" & myNewList(BindingSource1.Position.ToString("n0")))
    

    Please can you help?

    Wednesday, March 8, 2017 1:50 PM
  • Hi Frank L.

    I did everything you suggested that is above the link and work perfect.

    but what is below, I couldn't accomplish it. 

    With that known, before you remove the source (ergo, before you remove it from your list), add the file path to a new list which you need to create in your program. Following that, remove it from the source and tell the BindingSource to update.
    
    At the end of things, you've essentially moved the image from one list to another. Later then decide how you want to do this: For example ask the user to confirm they want to delete the file(s) and maybe even offer to send them to the recycle bin instead.
    
     

    Below is my code. I created a New List and transfered filepath from old list to new created list like this :

    myNewList = myOldList

    and when I try to delete using below code I receive an error saying ( The process cannot access the file c:\mypath\...  because it is being used by another process. )

            IO.File.Delete(Application.StartupPath & "\myPict\" & myNewList(BindingSource1.Position.ToString("n0")))

    Please can you help?

    Just call me Frank. :)

    *****

    Tell me more about what you're using this for? I suspect this isn't so much for browsing through pictures?


    "One who has no vices also has no virtues..."

    Wednesday, March 8, 2017 1:54 PM
  • Thanks, by the way, my real name's also Frank. I just use "LetMeCode" as pseudo.

    It's a small travelling app, with a Form containing field (name, surname, Dob,...) and pictures to upload.

    People need to add many pictures in order to save then along with the Form.

    I reserved a button named "Attach pictures", that has a OpenDialog with MultiSelection,

    and side by side, I placed a small button "Display pictures" which displays a reduced Form, with a Picturebox and BindingNavigator in it in order to display previously attached pictures.

    Displaying options work great like you suggested, but the only thing that I'm unable to accomplish is the "Delete" option.


    • Edited by LetMeCode Wednesday, March 8, 2017 2:16 PM
    Wednesday, March 8, 2017 2:16 PM
  • Thanks, by the way, my real name's also Frank. I just use "LetMeCode" as pseudo.

    It's a small travelling app, with a Form containing field (name, surname, Dob,...) and pictures to upload.

    People need to add many pictures in order to save then along with the Form.

    I reserved a button named "Attach pictures", that has a OpenDialog with MultiSelection,

    and side by side, I placed a small button "Display pictures" which displays a reduced Form, with a Picturebox and BindingNavigator in it in order to display previously attached pictures.

    Displaying options work great like you suggested, but the only thing that I'm unable to accomplish is the "Delete" option.



    I'll try to come up with an example here in a few hours when I can.

    "One who has no vices also has no virtues..."

    Wednesday, March 8, 2017 2:21 PM
  • and when I try to delete using below code I receive an error saying ( The process cannot access the file c:\mypath\...  because it is being used by another process. )

    I told you that you would have to clear the ref to the pic. When you use image.fromfile you have to do this. If you use file stream you should dispose the image anyway.

    Here is an example. See how I dispose the old image in the showpic sub?

    Option Strict On
    Imports System.IO
    
    Public Class Form5
        Private WithEvents ToolPnl As New Panel With {.Parent = Me,
            .Location = New Point(10, 10), .Size = New Size(40, 50), .BackColor = Color.Maroon,
            .Dock = DockStyle.Bottom}
    
        Private WithEvents BackBtn As New Button With {.Parent = ToolPnl, .Text = "<",
            .Location = New Point(10, 10), .Size = New Size(40, 32), .ForeColor = Color.AntiqueWhite,
            .Font = New Font("arial", 14, FontStyle.Bold)}
        Private WithEvents ForwardBtn As New Button With {.Parent = ToolPnl, .Text = ">",
            .Location = New Point(70, 10), .Size = New Size(40, 32), .ForeColor = Color.AntiqueWhite,
            .Font = New Font("arial", 14, FontStyle.Bold)}
        Private WithEvents DeleteBtn As New Button With {.Parent = ToolPnl, .Text = "X",
            .Location = New Point(130, 10), .Size = New Size(40, 32), .ForeColor = Color.AntiqueWhite,
            .Font = New Font("arial", 14, FontStyle.Bold)}
    
        Private WithEvents PicBox As New PictureBox With {.Parent = Me, .Dock = DockStyle.Top,
            .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.Black}
        Private WithEvents NameLbl As New Label With {.Parent = Me, .Dock = DockStyle.Top,
            .Font = New Font("arial", 14, FontStyle.Bold), .TextAlign = ContentAlignment.MiddleCenter,
            .ForeColor = Color.AntiqueWhite, .BackColor = Color.Maroon,
            .AutoSize = False, .Height = 40, .Text = "Click to Select Folder..."}
    
        Private PicsIndex As Integer
        Private Pics As New List(Of String)
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Picture Viewer"
    
            Form5_Resize(0, Nothing)
        End Sub
    
        Private Sub Form5_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            PicBox.Height = ClientSize.Height - (NameLbl.Height + ToolPnl.Height + 0)
    
            BackBtn.Location = New Point(CInt((ToolPnl.Width / 2) - ((3 * BackBtn.Width + 20) / 2)), 10)
            ForwardBtn.Location = New Point(BackBtn.Right + 10, BackBtn.Top)
            DeleteBtn.Location = New Point(ForwardBtn.Right + 10, BackBtn.Top)
    
        End Sub
    
        Private Sub NameLbl_MouseEnter(sender As Object, e As EventArgs) Handles NameLbl.MouseEnter
            NameLbl.Text = "Click to Select Folder..."
        End Sub
    
        Private Sub NameLbl_MouseLeave(sender As Object, e As EventArgs) Handles NameLbl.MouseLeave
            If Pics.Count = 0 Or PicsIndex = -1 Then
                NameLbl.Text = "Click to Select Folder..."
            Else
                NameLbl.Text = Pics(PicsIndex)
            End If
    
        End Sub
    
        Private Sub NameLbl_Click(sender As Object, e As EventArgs) Handles NameLbl.Click
            Using fbd As New FolderBrowserDialog
                If fbd.ShowDialog() = DialogResult.OK Then
                    Text = fbd.SelectedPath
                    Pics = Directory.GetFiles(fbd.SelectedPath, "*.gif").ToList
    
                    If Pics.Count = 0 Then PicsIndex = -1
    
                    ShowPic(PicBox, PicsIndex)
    
                End If
            End Using
        End Sub
    
        Private Sub BackBtn_Click(sender As Object, e As EventArgs) Handles BackBtn.Click
            'back
            PicsIndex -= 1
            If PicsIndex < 0 Then PicsIndex = Pics.Count - 1
            ShowPic(PicBox, PicsIndex)
    
        End Sub
    
        Private Sub ForwardBtn_Click(sender As Object, e As EventArgs) Handles ForwardBtn.Click
            'forward
            PicsIndex += 1
            If PicsIndex > Pics.Count - 1 Then PicsIndex = 0
            ShowPic(PicBox, PicsIndex)
    
        End Sub
    
        Private Sub DeleteBtn_Click(sender As Object, e As EventArgs) Handles DeleteBtn.Click
            'delete
            If Pics.Count > 0 Then
                'get the file from the list
                Dim deletefilename As String = Pics(PicsIndex)
    
                Dim r As Integer = MessageBox.Show("Delete: " & vbLf & vbLf & deletefilename,
                                "Delete File", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    
                If r = System.Windows.Forms.DialogResult.Yes Then
                    'update the list
                    Pics.Remove(deletefilename)
                    PicsIndex -= 1
    
                    If Pics.Count = 0 Then
                        PicsIndex = -1
                    Else
                        If PicsIndex < 0 Then PicsIndex = Pics.Count - 1
                    End If
    
                    'show last image to break the ref to the disk file
                    ShowPic(PicBox, PicsIndex)
                    File.Delete(deletefilename)
                End If
            End If
    
        End Sub
    
        Private Sub ShowPic(thisPb As PictureBox, thisPicIndex As Integer)
            'copy the reference to the old pic file so it can be disposed
            Dim tempBmp As New Bitmap(1, 1)
            If thisPb.BackgroundImage IsNot Nothing Then tempBmp = CType(thisPb.BackgroundImage, Bitmap)
    
            If thisPicIndex > -1 AndAlso Pics.Count > 0 Then
                'show the new image
                thisPb.BackgroundImage = System.Drawing.Image.FromFile(Pics(thisPicIndex))
                NameLbl.Text = "  " & Pics(thisPicIndex)
            Else
                'clear the picturebox
                thisPb.BackgroundImage = Nothing
                NameLbl.Text = "No Pictures"
            End If
    
            tempBmp.Dispose()
        End Sub
    
    End Class



    • Edited by tommytwotrain Wednesday, March 8, 2017 3:06 PM new gif
    Wednesday, March 8, 2017 2:27 PM
  • Okay thanks.

    out of curiousity, yesterday you said : 

     I'm always reluctant to delete anything from a user's computer.

    Why???

    I'm asking cause long time ago, I think 5 years back, I used to work on a software that was lying to the users.

    I don't have a very clear idea on it anymore, but I remember, if I delete something (a file) from the software, it was telling me file deleted, and couldn't see the file not appearing anymore within the program but if I check in the program file, the file was always in there.

    So personnaly I didn't like it, anytime when I delete a file from that program... I must go to program file and delete also the real file.

    That's why I just wanna know why are you reluctant to delete anything from a user's pc.


    Wednesday, March 8, 2017 2:32 PM
  • Okay thanks.

    out of curiousity, yesterday you said : 

     I'm always reluctant to delete anything from a user's computer.

    Why???

    I'm asking cause long time ago, I think 5 years back, I used to work on a software that was lying to the users.

    I don't have a very clear idea on it anymore, but I remember, if I delete something (a file) from the software, it was telling me file deleted, and couldn't see the file not appearing anymore within the program but if I check in the program file, the file was always in there.

    So personnaly I didn't like it, anytime when I delete a file from that program... I must go to program file and delete also the real file.

    That's why I just wanna know why are you reluctant to delete anything from a user's pc.


    I just don't want the responsibility of the potential backlash (whether or not my program was at fault).

    "OH NO! YOUR program deleted all the pictures of my son's graduation!"

    ... I don't want to have that conversation. ;-)


    "One who has no vices also has no virtues..."

    Wednesday, March 8, 2017 2:58 PM
  • Really !!!  laughing....................................................

    Before the deletion, there's must be a MSGBOX (yes/no) in order to confirm that action from the user !!!

    and when displaying a message "picture deleted successfully" for me it means pictures are no longer within the program and/or the computer hard disk.

    Maybe other people would have same way of thinking than you, but that probably needs a new Forum to Discuss about it, and we'll see how programmers think !!!  [laughing...]

    Let assume, it's a video player program with option to delete your video from the program,

    a user was watching PO*N, and his wife doesn't like the man watching it, so the man after watching it, he does click on a button called "Delete" and confirm his deletion by clicking "Yes". The guy is totally sure that there's nothing more in the computer.

    He passed his laptop to his woman to check his email and the women discover the video and CLASH begins !!!

    The guy will send you a message saying : 

    " OHHH...   I THOUGHT DELETE MEANT DELETE in your program"



    • Edited by LetMeCode Wednesday, March 8, 2017 3:22 PM
    Wednesday, March 8, 2017 3:17 PM
  • Really !!!  laughing....................................................

    Before the deletion, there's must be a MSGBOX (yes/no) in order to confirm that action from the user !!!

    and when displaying a message "picture deleted successfully" for me it means pictures are no longer within the program and/or the computer hard disk.

    Maybe other people would have same way of thinking than you, but that probably needs a new Forum to Discuss about it, and we'll see how programmers think !!!  [laughing...]

    Let assume, it's a video player program with option to delete your video from the program,

    a user was watching PO*N, and his wife doesn't like his man watching it, so the man after watching it, he does click on a button called "Delete" and confirm his deletion by clicking "Yes". The guy is totally sure that there's nothing more in the computer.

    He passed his laptop to his woman to check his email and the women discover the video and CLASH begins !!!

    The guy will send you a message saying : 

    " OHHH...   I THOUGHT DELETE MEANT DELETE in your program"



    ;-)

    "One who has no vices also has no virtues..."

    Wednesday, March 8, 2017 3:22 PM
  • @ tommytwotrain.

    Thanks for your code !

    Thursday, March 9, 2017 2:38 PM
  • LMC (Frank),

    I have created a project which includes a small class library in it. The project folder is zipped up and uploaded here:

    https://fls.exavault.com/share/view/ghwt-2jlj8omf

    When you download that zip file, extract it somewhere then open the solution with Visual Studio. You'll be prompted to convert it to your version, so just follow the prompts and it should work.

    Before I move on, I forgot something: In your version near the top of the code, you'll see three imports statements:

    Imports System.IO
    Imports System.IO.Path
    Imports ImageFilesData.Images

    You'll need to add a fourth one please:

    Imports System.IO
    Imports System.IO.Path
    Imports ImageFilesData.Images
    Imports System.Windows.Forms

    I learned that in another thread yesterday - apparently the newer version of VS doesn't automatically import that namespace.

    Before you do anything else, please create a temporary directory and put some images in it. Once you go all the way through this, yes it really does delete them for real! ;-)

    *****

    When you run it, you'll see this:

    The menu bar has three drop-down items and since there are no images in there yet, only one will be enabled:

    My idea here is that the user can mark one or more of the images for deletion and when they do, it's removed from the collection but the file isn't yet deleted. They'll have to confirm that they really do want to and then it will be deleted from the file system. You'll see all of that as I step through this.

    When I click the menu item to add images, I'll add all four from my temporary folder:

    The class library has its own BindingSource and there are some events that are being subscribed to. If I have a need to write a class library then I might as well get it to do what I need.

    As the images are being loaded, note that the BindingNavigator is disabled, but you can see the count increasing:

    Then, once it's all loaded it's enabled again. You can, of course, navigate like you're used to doing.

    When any image is shown, the user has the ability to "tag it" for removal:

    Note that the image was removed from the collection (not the file yet), the count decreased, and the text on the status bar shows the count of how many are marked to be deleted. I'll continue with the rest of them although at any point that at least one has been removed the user can confirm it and the file will then be deleted:

    Now I'll tell it that I really do want those files to be gone:

    Just as evidence here:

    Yep, it really is gone so use a temporary directory please!


    "One who has no vices also has no virtues..."

    Thursday, March 9, 2017 2:52 PM

  • Thanks Frank, will give it a try and let you know.

    Thursday, March 9, 2017 3:58 PM