Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 6, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Use MaxFileSize With Openfile Common Dialogs

When working with OpenFile common dialogs, set a large value for the MaxFileSize property, which affects the maximum length of the string returned in the FileName property. This is the string containing the names of all the files selected by the user. For example, you can reserve 10 kilobytes for this string by executing this statement before showing the dialog:
CommonDialog1.MaxFileSize = 10240
The returned string's format depends on how many files the user selects. If the user selects only one file, the string returns in the FileName property that contains the complete name of this file (path + name). If the user selects multiple files, the returned value contains the directory name, followed by all the names without the path. If you use the cdlOFNExplorer flag, the separator character is the Null character. If you're using VB6, you can use the Split function to quickly parse the returned value:
CommonDialog1.Filter = "All files (*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNAllowMultiselect Or _
	cdlOFNFileMustExist Or cdlOFNExplorer
CommonDialog1.MaxFileSize = 10240
CommonDialog1.CancelError = True
CommonDialog1.Filename = ""
If Err = 0 Then
	' Parse the result into an array of strings
	Dim names() As String, i As Integer
	names() = Split(CommonDialog1.Filename, vbNullChar)
	' Print file names, including path
	If UBound(names) = 0 Then
		' only one file was selected
		Print names(0)
		' multiple files were selected
		For i = 1 To UBound(names)
			Print names(0) & "\" & names(i)
	End If
End If
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date