INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

comdlg32 filename: right justified and can not see the file name in the dialog textbox

comdlg32 filename: right justified and can not see the file name in the dialog textbox

comdlg32 filename: right justified and can not see the file name in the dialog textbox

(OP)
Hi,

I am using the Common Dialog Box GetOpenFileNameW (comdlg32.dll) call and I am setting the file name buffer to 255 characters. The problem I am experiencing is that I can not see the file name because the textbox is right-justified and all I can see is the spaces padding the name.

I'd like to be able to either not have the spaces and still be able to select a variable length file name or simply set the cursor to the first character of the textbox.

Does anyone know how to do that?

Thanks,

Tom

RE: comdlg32 filename: right justified and can not see the file name in the dialog textbox

Odd. I don't see that problem.

Perhaps you can show your code

(by the way, the documentation expicitly states that "The buffer should be at least 256 characters long", but this won't - or shouldn't - be the cause of your problem)

RE: comdlg32 filename: right justified and can not see the file name in the dialog textbox

(OP)
True it is 256 sorry.

Here's the Function:

Private Function ShowFileDialog(ByVal viAction As Integer) As Boolean
'------------------------------------------------------------------------------
' Name : ShowFileDialog
' Author : Microsoft Devnet Article - modified by BAB
' Date : 11/22/99
' Purpose: To display the file dialog box for ShowOpen or ShowSave.
' Method : Not documented
'
' Update : Added code to return multi-selected files. Didn't work BAB
'------------------------------------------------------------------------------

Dim lMaxSize As Long

Dim sFileNameBuff As String
Dim sFileTitleBuff As String

Dim nDoubleNull As Integer

Dim tOpenFile As OpenFilename

'On Error Resume Next 'XMODG95


'*** init property buffers

miAction = viAction 'Action property
mlApiReturn = 0 'APIReturn property
mlExtendedError = 0 'ExtendedError property


'*** prepare tOpenFile data

With tOpenFile
.lStructSize = Len(tOpenFile)
.hwndOwner = mlhdc 'init from hdc property
.lpstrFilter = sAPIFilter(msFilter) 'init from Filter property
.iFilterIndex = miFilterIndex 'init from FilterIndex property


'tOpenFile.lpstrFile As String
'determine size of buffer from MaxFileSize property. Min is 256
If mlMaxFileSize > 256 Then
lMaxSize = mlMaxFileSize
Else
lMaxSize = 256
End If

'prepare sFileNameBuff init from FileName property
sFileNameBuff = msFileName

While Len(sFileNameBuff) < lMaxSize - 1 'pad with spaces
sFileNameBuff = sFileNameBuff & " "
Wend

'trim to length of lMaxFileSize - 1
sFileNameBuff = Mid$(sFileNameBuff, 1, lMaxSize - 1)

sFileNameBuff = sFileNameBuff & Chr$(0) 'null terminate
.lpstrFile = sFileNameBuff

'init from MaxFileSize property
.nMaxFile = lMaxSize

'prepare sFileTitleBuff
'init from FileTitle property
sFileTitleBuff = msFileTitle

While Len(sFileTitleBuff) < lMaxSize - 1 'pad with spaces
sFileTitleBuff = sFileTitleBuff & " "
Wend

sFileTitleBuff = Mid$(sFileTitleBuff, 1, lMaxSize - 1) 'trim to length of lMaxFileSize - 1
sFileTitleBuff = sFileTitleBuff & Chr$(0) 'null terminate
.lpstrFileTitle = sFileTitleBuff


.lpstrInitialDir = msInitDir 'init from InitDir property
.lpstrTitle = msDialogTitle 'init from DialogTitle property
.Flags = mlFlags 'init from Flags property
.lpstrDefExt = msDefaultExt 'init from DefaultExt property

End With 'tOpenFile


mbCancelSelected = False 'Initialize the Cancel Selected Property

'*** call the GetOpenFileName API function
Select Case miAction
Case 1 'ShowOpen
On Error Resume Next 'XMODG95

mlApiReturn = CApisString.GetOpenFileName(tOpenFile)

'Error indicates the DLL has problems.
If (Err) Then
MsgBox Err.Description & " - Comdlg32.dll." & vbCrLf & "Function - GetOpenfileName", , "CCommonDialog::Error" 'don't translate - all procedure names
End If
On Error GoTo 0 'XMODG95

mlFlags = tOpenFile.Flags 'Save any returned flags from Call to DLL

Case 2 'ShowSave

On Error Resume Next 'XMODG95

'- Some users were experiencing a system crash when calling this method
') it was requested to use the GetSave dialog instead as it seemed to be
') working OK. This was occuring in Windows 7 64-bit OS's.

'- Old Call
'- mlApiReturn = CApisString.GetSaveFileName(tOpenFile)
mlApiReturn = CApisString.GetOpenFileName(tOpenFile)

'Error indicates the DLL has problems.
If (Err) Then
MsgBox Err.Description & " - Comdlg32.dll." & vbCrLf & "Function - GetSavefileName", , "CCommonDialog::Error" 'don't translate - all procedure names
End If
On Error GoTo 0 'XMODG95

mlFlags = tOpenFile.Flags 'Save any returned flags from Call to DLL

Case Else 'unknown action
ShowFileDialog = False
Exit Function
End Select

miFilterIndex = tOpenFile.iFilterIndex 'get user's index PM 11/14/01

'*** handle return from GetOpenFileName API function
Select Case mlApiReturn
Case 0 'user canceled
mlExtendedError = CommDlgExtendedError
If mlExtendedError = 0 Then
mbCancelSelected = True
End If

ShowFileDialog = False
Case 1 'user selected or entered a file
'sFileName gets part of tOpenFile.lpstrFile to the left of first Chr$(0)
msFileName = sLeftOfNull(tOpenFile.lpstrFile)

msFileTitle = sLeftOfNull(tOpenFile.lpstrFileTitle)
ShowFileDialog = True
Case Else 'an error occured
'call CommDlgExtendedError
mlExtendedError = CommDlgExtendedError
ShowFileDialog = False
End Select

'On Error GoTo 0 'XMODG95

End Function



Here's the API call:

Friend Function GetOpenFileName(pOpenfilename As OpenFilename) As Long

Dim OfnW As OpenFilenameW

Select Case mbUniCode
Case VbTriState.vbFalse
GetOpenFileName = GetOpenFileNameA(pOpenfilename)
Case VbTriState.vbTrue
With OfnW
.lpstrCustomFilter = StrPtr(pOpenfilename.lpstrCustomFilter)
.lpstrDefExt = StrPtr(pOpenfilename.lpstrDefExt)
.lpstrFile = StrPtr(pOpenfilename.lpstrFile)
.lpstrFileTitle = StrPtr(pOpenfilename.lpstrFileTitle)
.lpstrFilter = StrPtr(pOpenfilename.lpstrFilter)
.lpstrInitialDir = StrPtr(pOpenfilename.lpstrInitialDir)
.lpstrTitle = StrPtr(pOpenfilename.lpstrTitle)
.lpTemplateName = StrPtr(pOpenfilename.lpTemplateName)
.Flags = pOpenfilename.Flags
.hInstance = pOpenfilename.hInstance
.hwndOwner = pOpenfilename.hwndOwner
.iFilterIndex = pOpenfilename.iFilterIndex
.lCustData = pOpenfilename.lCustData
.lpfnHook = pOpenfilename.lpfnHook
.nFileExtension = pOpenfilename.nFileExtension
.nFileOffset = pOpenfilename.nFileOffset
.nMaxCustFilter = pOpenfilename.nMaxCustFilter
.nMaxFile = pOpenfilename.nMaxFile
.nMaxFileTitle = pOpenfilename.nMaxFileTitle
.lStructSize = Len(OfnW)
End With
GetOpenFileName = GetOpenFileNameW(OfnW)
With pOpenfilename
.Flags = OfnW.Flags
.hInstance = OfnW.hInstance
.hwndOwner = OfnW.hwndOwner
.iFilterIndex = OfnW.iFilterIndex
.lCustData = OfnW.lCustData
.lpfnHook = OfnW.lpfnHook
.nFileExtension = OfnW.nFileExtension
.nFileOffset = OfnW.nFileOffset
.nMaxCustFilter = OfnW.nMaxCustFilter
.nMaxFile = OfnW.nMaxFile
.nMaxFileTitle = OfnW.nMaxFileTitle
.lStructSize = Len(pOpenfilename)
End With
Case VbTriState.vbUseDefault
Call Err.Raise(Errors.COMMON_OBJECT_NOT_INITIALIZED)
End Select
End Function

RE: comdlg32 filename: right justified and can not see the file name in the dialog textbox

Ok.

Firstly:

.hwndOwner = mlhdc 'init from hdc property

An hDC is very different from a hWnd. You may want to correct this, as you may get unexpected results of you pass a bad value here. Agian, I wouldn't expect this to be the cause of this particular error.


Need more time to examine the code for anything else.

However, here's a short version of my own. If you run this, do you see the same problem?

CODE

Option Explicit

Private Declare Function GetOpenFileName Lib "COMDLG32.DLL" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  
Private Type OPENFILENAME
  lStructSize       As Long
  hwndOwner         As Long
  hInstance         As Long
  lpstrFilter       As String
  lpstrCustomFilter As String
  nMaxCustFilter    As Long
  nFilterIndex      As Long
  lpstrFile         As String
  nMaxFile          As Long
  lpstrFileTitle    As String
  nMaxFileTitle     As Long
  lpstrInitialDir   As String
  lpstrTitle        As String
  Flags             As Long
  nFileOffset       As Integer
  nFileExtension    As Integer
  lpstrDefExt       As String
  lCustData         As Long
  lpfnHook          As Long
  lpTemplateName    As String
End Type

Public Sub spoon()
    Dim OFN As OPENFILENAME
    Dim ret As Long
    Dim n As Long

  With OFN
    .lStructSize = Len(OFN)     ' Size of structure.
    .nMaxFile = 260             ' Size of buffer.
    ' Create buffer.
    .lpstrFile = String(.nMaxFile - 1, 0)
    ret = GetOpenFileName(OFN)  ' Call function.
    If ret <> 0 Then            ' Non-zero is success.
      ' Find first null char.
      n = InStr(.lpstrFile, vbNullChar)
      ' Return what's before it.
      MsgBox Left(.lpstrFile, n - 1)
    End If
  End With
  
End Sub 

RE: comdlg32 filename: right justified and can not see the file name in the dialog textbox

(OP)
Hey Strongm,

Thanks for the code snippet. I changed my code to use the .lpstrFile = String(.nMaxFile - 1, 0) statement and that fixed the problem. I was padding the name with " " and it was creating a huge string that was getting selected in the dialog textbox and hiding the file name. When padding with the 0 those characters are not included in the textbox so only just the file name is selected.

Thanks again!

-Tom.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close