Question

Im developing a small file explorer, how do i set Back button.

i have:

  • txtAddress.Text which is the address bar of the explorer.
  • string currAddress which has the current address.
  • List<string> prevAddress which should hold some previous addresses which had been visited for Back Button

    and i use:

  • Root(); to get My Computer items.
  • Open(string Address); to get Files/Folder from an address.
  • Search(string Address, string keyword); to get the search result items.

    i need the back button because when i do search in a path, i can't press up button (which go to the parent path) because i need to get back to the path i was searching in, so how do back button works in explorer? and when should i add/remove addresses from it?

    enter image description here

  • Était-ce utile?

    La solution

    To implement a back button well I would suggest using a stack of some kind which maintains the locations which the user has been. Each time a navigation is performed, push the old location onto the stack. When the back button is pressed, pop the top item on the stack and navigate to that location. If the stack is empty, make the back button unusable, as there is nowhere to go back to.

    Autres conseils

    Suggest keeping/managing this state yourself in the application.

    Each time the user navigates/forces a new/different path in the address bar, then add the new directory to a list/collection in your app.

    When the back button is clicked, you can find the 'previous' entry in your list/collection. That's your directory to display.

    Key    Val
      1     D:\
      2     D:\Foo
      3     D:\Foo\Bar
      4     C:\        (here the user may have manually typed into the addr bar)
    

    You may run into issues where the directory no longer exists, is renamed, is unavailable, etc. Perhaps you've already got these cases handled in your code. You could use Directory.Exists before attempting to navigate.

    Back (and forward) is very close to Undo which is more commonly discussed (i.e. mentioned in the "Design Patterns" book).

    Common implementation - state (as pointed by @p.campbell answer) for each operation stored in "current state" and 2 stacks: one for undo/back, another for redo/forward. Whenever user makes a change (i.e. by typing something in or actively navigating somewhere) redo/forward stack is cleared and previous state is pushed to undo/back stack. Back/forward correspondingly put current state into one of the stacks and pop next state from the other one.

    Licencié sous: CC-BY-SA avec attribution
    Non affilié à StackOverflow
    scroll top