Highlight next file in list after deletion

Post

Posted
Rating:
#1 (In Topic #166)
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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…

Code (gambas)

  1. Public Function GetNextFile(strFile2Delete As String) As String
  2. 'look for current selected file and return the next one (if there is a next one!)
  3. Dim strFileName As String
  4. Dim iCount As Integer
  5. Dim blnMatch As Boolean
  6.  
  7.   For Each strFileName In Dir(FileChooser1.Dir, "*", gb.File).Sort()
  8.     Inc iCount
  9.     If blnMatch Then
  10.       Return strFileName  'next file in list
  11.     Endif
  12.     If strFile2Delete = FileChooser1.Dir & "/" & strFileName And FileChooser1.FileView.Count > iCount Then
  13.         blnMatch = True
  14.     Endif
  15.   Next
  16.   Return "" 'no match or no more files
  17.  
  18.  

…then deleting the current file, and selecting the next file.

Code (gambas)

  1. Public Sub btnDelete_Click()
  2. 'Delete currently selected file and load the next file
  3. Dim lngReply As Long
  4. Dim strSelectNextFile As String
  5.  
  6.   If FileChooser1.SelectedPath = "" Then
  7.     Message.Warning("You didn't select a file for deletion!", "close")
  8.   Else
  9.     lngReply = Message.Question("Are you sure you want to delete: " & FileChooser1.FileView.Current & "?", "Yes", "No")
  10.     If lngReply = 1 Then
  11.       strSelectNextFile = GetNextFile(FileChooser1.SelectedPath)
  12.       Try Kill FileChooser1.SelectedPath
  13.       If Not Error Then
  14.         FileChooser1.FileView.Current = strSelectNextFile
  15.       Endif
  16.     Endif
  17.   FileChooser1.FileView.Reload
  18.   FileChooser1.FileView.SetFocus
  19.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
I think you can simplify some of your code by storing the list of files in a string

Code (gambas)

  1. sFileList = Dir(sFolder).Sort()
Then highlight the first file in the string

Code (gambas)

  1. 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: -

Code (gambas)

  1. FileChooser1.Dir & "/" & strFileName
can be simplified to: -

Code (gambas)

  1. 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"
Attachment
Online now: Yes Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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-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?  :?
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

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-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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
stevedee is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
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
Attachment
Online now: Yes Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
stevedee is in the usergroup ‘Regular’

cogier said

…I have had another go…

It looks good to me, thanks Charlie!
Online now: No Back to the top
1 guest and 0 members have just viewed this.