I have the following code in the PushButton Action event
Dim dialog As OpenDialog
Dim file As FolderItem
dialog = New OpenDialog
dialog.Filter = "text/plain"
file = dialog.ShowModal
If file <> Nil Then
TextField1.Text = file.Name
Else
TextField1.Text = "User clicked Cancel."
End If
I have several questions:
Why does the dialog show all files and folder while dialog.Filter is set equal to “text/plain?”
How to remove file’s extension from the TextField1.Text?
You can do what smart Xojo developers do and invest in the Monkey Bread Software plugins (MBS) and use f.NameWithoutExtensionMBS or you can write a method to read the name from the end backwards dropping everything from the end to and including the first period encountered.
[code]Public Function NameWithoutExtension(extends afi as FolderItem) as String
dim ch as String = afi.Name
dim i as Integer = CountFieldsB(ch,".")
dim ext as String = NthFieldB( ch, “.”, i)
Note a very important part of when Jean-Yves posted - the use of the “B” variants for NthField, CountField, Left, and Len. They really speed things up if you are dealing with lots of strings.
That’s because project File Type Sets and filters are broken. The engineers involved with the feedback case insist everyone is just doing it wrong. The most reliable fix is to define the FileType object in code directly before using it.
dim ftFilter as new FileType
ftFilter.Name = "PDF"
ftFilter.Extensions = ".pdf"
dim dlgOpen as new OpenDialog
dlgOpen.Filter = ftFilter
call dlgOpen.ShowModal
You both seem to assume that the FileName actually contains a Dot…
Guess what happens with the code above and these filenames:
MyFileWithoutExtension
.myappprefs
Not thought-through… but how about these additional checks?
[code]Dim fn As String = afi.Name
Dim parts() As String = SplitB(fn, “.”)
if (InStrB(fn, “.”) > 1) or ((parts.Ubound = 1) and (parts(0) <> “”)) or (parts.Ubound > 1) then
parts.Remove(parts.Ubound)
fn = Join(parts, “.”)
end if
return fn[/code]
I know… that still doesn’t account for Filenames such as ..test (even though one can argue what the Filename/Extension is in this case) - so there is still room for improvements
Oh, and I guess @Kem Tekinay would do that with a RegEx