Highlight next file in list after deletion

Post your Gambas programming questions here.
Post Reply
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Highlight next file in list after deletion

Post by stevedee »

I find it annoying when working through a list of files in a FileChooser, that when I delete one, I lose my place. I want the FileChooser to highlight the next file following the deleted one.

Recently, when building my BatCallSorter, I fixed this issue in my code by finding the name of the next file in the list...
Public Function GetNextFile(strFile2Delete As String) As String
'look for current selected file and return the next one (if there is a next one!)
Dim strFileName As String
Dim iCount As Integer
Dim blnMatch As Boolean
  
  For Each strFileName In Dir(FileChooser1.Dir, "*", gb.File).Sort()
    Inc iCount
    If blnMatch Then
      Return strFileName  'next file in list
    Endif
    If strFile2Delete = FileChooser1.Dir & "/" & strFileName And FileChooser1.FileView.Count > iCount Then
        blnMatch = True
    Endif
  Next
  Return "" 'no match or no more files
  
End
...then deleting the current file, and selecting the next file.
Public Sub btnDelete_Click()
'Delete currently selected file and load the next file
Dim lngReply As Long
Dim strSelectNextFile As String

  If FileChooser1.SelectedPath = "" Then
    Message.Warning("You didn't select a file for deletion!", "close")
  Else
    lngReply = Message.Question("Are you sure you want to delete: " & FileChooser1.FileView.Current & "?", "Yes", "No")
    If lngReply = 1 Then
      strSelectNextFile = GetNextFile(FileChooser1.SelectedPath)
      Try Kill FileChooser1.SelectedPath
      If Not Error Then
        FileChooser1.FileView.Current = strSelectNextFile
      Endif
    Endif
  Endif
  FileChooser1.FileView.Reload
  FileChooser1.FileView.SetFocus
End
If there is a easier (less code) way of doing this, please let me know.

The code above just needs a Form, a FileChooser and a Button control.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Highlight next file in list after deletion

Post by cogier »

I think you can simplify some of your code by storing the list of files in a string
sFileList = Dir(sFolder).Sort()

Then highlight the first file in the string
FileChooser1.SelectedPath = sFolder &/ sFileList[0]
You can delete any file then you can repeat the same as above. This way there is no need to loop through any lists.
Have a look at the attached program below which creates a load of files in /tmp/ to play with.
On a side note Gambas can sort out if you need a "/" in a Path for you so: -
FileChooser1.Dir & "/" & strFileName
can be simplified to: -
FileChooser1.Dir &/ strFileName
To quote the Gambas wiki: -
To generate a directory or file path at runtime, use the file path concatenate operator &/:
Mkdir "/home/gambas/" &/ "/tmp" &/ "foo.bar"
HighLightNextFile.tar.gz
(14.15 KiB) Downloaded 446 times
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Highlight next file in list after deletion

Post by stevedee »

Hi Charlie, thanks for your input.

Unless I'm missing something, I don't think your solution meets the requirements of highlighting the next file in the list after the one that has been deleted.

It seems to me that the FileView control should have included an "Index" property (as the ListBox has). That way we could simply use the index of the file to be deleted to select/highlight the next file in the list, rather than having to find the file name.

I agree with the use of "&/" I just keep forgetting to use it. Maybe I'm getting too old for all this technical stuff!


A side issue; when you run your test code (or mine) in the IDE, do you see warning messages after you stop/close the program?

I get this:-
(HighLightNextFile:2459): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(HighLightNextFile:2459): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Highlight next file in list after deletion

Post by cogier »

Hi Steve,

I tried my program again without issue, the next file is selected and the program closes without any errors. See herehttp://www.cogier.com/stevedee/stevedee-20180510-1.webm.

I'm using Linux Mint 18.3 with the Cinnamon desktop and Gambas 3.10

Or did I miss the point due to being so old? :?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Highlight next file in list after deletion

Post by stevedee »

cogier wrote: Thursday 10th May 2018 1:35pm Hi Steve,

I tried my program again without issue, the next file is selected and the program closes without any errors. See herehttp://www.cogier.com/stevedee/stevedee-20180510-1.webm.

I'm using Linux Mint 18.3 with the Cinnamon desktop and Gambas 3.10

Or did I miss the point due to being so old? :?
You may be missing the point, but I'm sure you are just a youngster!

When you delete file12 I expect it to move on and highlight file13, not go back to the first listed file.

I guess my IDE warning is a Lubuntu related thing.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Highlight next file in list after deletion

Post by stevedee »

On second thoughts, I probably should have explained my app as an example of what I wanted to do;

When I open my BatCallSorter I view a folder containing short bat recordings (...the recordings are short, not necessarily the bats).

The app shows a spectrogram image for each highlighted file. As I 'arrow' down the list of files, I may spot an empty recording, so I hit the delete button. I then carry on going down the list of files from where I left off (i.e. if I deleted the 5th file, the 6th file becomes the 5th file, and that is the next one I see. I don't have to start again at file 1 and work down the list again).

I hope that makes more sense.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Highlight next file in list after deletion

Post by cogier »

OK I missed the point, sorry. :cry:

I have had another go and spent some time, while cooking the evening meal, reducing the code. This is about as tight as I can make it.

Here is the video http://www.cogier.com/stevedee/stevedee1.webm

Here is the program
HighLightNextFile.tar.gz
(14.44 KiB) Downloaded 492 times
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Highlight next file in list after deletion

Post by stevedee »

cogier wrote: Thursday 10th May 2018 7:37pm ...I have had another go...
It looks good to me, thanks Charlie!
Post Reply