How to filter invalid characters for a filename ?

Started by Jean-pierre Leroy, July 10, 2015, 11:45:05 AM

Previous topic - Next topic

Jean-pierre Leroy

Dear Firefly users,

I have a FORM with a Textbox where the user can enter manually a filename; as you may know, some characters are not valid for a filename:

\ / : * ? " < > |

More information here:
https://support.microsoft.com/en-us/kb/177506

Is-there a simple way to filter these invalid characters when the user fills the filename and optionnaly display an error message like Windows does, when trying to use an invalid character when renaming a file in Windows explorer.

Eddy Van Esch

Jean-Pierre,

You need to trap the WM_CHAR message of the textbox and process the characters (intercept or let them pass) .
This is something simular:
http://www.planetsquires.com/protect/forum/index.php?topic=2916.msg21686#msg21686

Kind regards
Eddy
Eddy

Jean-pierre Leroy

Eddy,

Thanks for the info about WM_CHAR handler; it works exactly as expected with this code:


Function RENAMEFILENAME_FILENAME_WM_CHAR ( _
                                         ControlIndex  As Long,  _  ' index in Control Array
                                         hWndForm      As Dword, _  ' handle of Form
                                         hWndControl   As Dword, _  ' handle of Control
                                         chCharCode    As Long,  _  ' character code
                                         lKeyData      As Long   _  ' key data
                                         ) As Long
                                         
    ' Keys to filter
    Select Case Chr$(chCharCode)
        Case "\", "/", ":", "*", "?", $Dq, "<", ">", "|"
            Function = 1
            Exit Function
    End Select                                             

    Function = 0   ' change according to your needs
   
End Function


Regarding the message, it's a Balloon control :
https://msdn.microsoft.com/en-us/library/windows/desktop/dn742400%28v=vs.85%29.aspx

Jose provided two wrappers for that:

Edit_ShowBalloonTip
Edit_HideBalloonTip


That should be ok.


Jean-pierre Leroy

With the Balloon Window:


'--------------------------------------------------------------------------------
Function RENAMEFILENAME_FILENAME_WM_CHAR ( _
                                         ControlIndex  As Long,  _  ' index in Control Array
                                         hWndForm      As Dword, _  ' handle of Form
                                         hWndControl   As Dword, _  ' handle of Control
                                         chCharCode    As Long,  _  ' character code
                                         lKeyData      As Long   _  ' key data
                                         ) As Long
                                         
    ' Keys to filter
    Select Case Chr$(chCharCode)
        Case "\", "/", ":", "*", "?", $Dq, "<", ">", "|"
            Function = 1
            ShowBalloonTip(hWndControl, "", "A file name cannot contain any of the following characters:"+$CrLf+$Tab+"\ / : * ? "" < > |", %TTI_NONE)
            Exit Function
    End Select
    Edit_HideBalloonTip(hWndControl)                                           

    Function = 0   ' change according to your needs
   
End Function

Sub ShowBalloonTip(ByVal hWndControl As Dword, ByVal pszTitle As wString, ByVal pszText As wString, ByVal tticon As Long)
   
    Local bt As EDITBALLOONTIP
     
    bt.cbStruct = SizeOf(bt)
    bt.pszTitle = StrPtr(pszTitle)
    bt.pszText  = StrPtr(pszText)   
    bt.ttiIcon  = tticon   
    Edit_ShowBalloonTip(hWndControl, bt)
   
End Sub