I have a OpenFileDialog and its MultiSelect property is ON. My question is how can I limit the number of items to be selected, for example 5 items only?
Thanks
You can use the FileOk event to check the file(s) selected by the user when he clicks the OK button. If you are not happy then display a message and set the CancelEventArgs.Cancel property to True to prevent the dialog from closing. Like this:
Dim dlg As New OpenFileDialog()
dlg.Multiselect = True
AddHandler dlg.FileOk, Sub(s, ce)
If dlg.FileNames.Length > 5 Then
MessageBox.Show("Please select no more than 5 files")
ce.Cancel = True
End If
End Sub
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
'' etc...
End If
You cannot. But you have alternatives:
1.- A good alternative could be to put all file names in a text file, and then accept that text file as your program's input.
2.- You should allow user to pick the directory. Then you list all the files and let them select as many files, there will not be any problem.
3.- You may have to use a FolderBrowserDialog instead and then use IO.Directory.GetFiles, which works properly.
There is no built-in feature for that in OpenFileDialog as far as I can see. Possible solution is to check FileNames returned from the dialog. If it count more than 5, for example, alert user and stop without operating the files.
Related
I have existing code here, but it shows a SaveAs dialog immediately and this is quite confusing for some users. How can I improve this?
Dim write As StreamWriter
SaveFileDialog1.Filter = "PDF Files |*.pdf"
SaveFileDialog1.ShowDialog()
write = File.AppendText(SaveFileDialog1.FileName)
write.WriteLine()
write.Close()
If you want us to help you change how the user interface works, we need to see more context from that interface, and you need to talk to your users more about what they expected instead of the SaveAs dialog.
But we can help improve this code some:
SaveFileDialog1.Filter = "PDF Files |*.pdf"
If SaveFileDialog1.ShowDialog() = DialogResult.OK AndAlso Not String.IsNullOrWhitespace(SaveFileDialog1.FileName) Then
Using writer As StreamWriter = File.AppendText(SaveFileDialog1.FileName)
writer.WriteLine()
End Using
End If
Based on the question title, maybe you wanted to show an OpenFileDialog first? Have you tried that? And then maybe a FolderBrowserDialog instead of SaveFileDialog?
I have a program which loads data from text files contained in directories back into text boxes on the user interface.
What I want to achieve is:
if the user changes a text of one of the text boxes and then clicks an "Update" button, the old record and its holding directory should be deleted the new record, saved in a new directory.
The code I am using now is:
Dim dtl = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Cake Orders\" & TextBox1.Text))
'Delete the culled record
Try
If Directory.Exists(dtl.FullName) Then
Dim oDirectory As New DirectoryInfo(dtl.FullName)
If oDirectory.GetFiles.Count > 0 Then
For Each oFile As FileInfo In oDirectory.GetFiles
oFile.Delete()
Next
End If
If oDirectory.GetDirectories.Count > 0 Then
For Each oDir As DirectoryInfo In oDirectory.GetDirectories
oDir.Delete(True)
Next
dtl.Delete()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Continue to save the updated record
The above code just saves the current data in a new directory (which is fine), but does not delete the original directory like I want it to; that is to say that my code is not executing the above delete routine.
How can I achieve my objective? I am using Visual Basic 2010 Express. Thank you.
What I would suggest is that you find a way to walk around your problem, to get your program to do what you want it to.
Granted, some may provide you with some more advanced, Einstein-esque codes that will do what you want and by all means, I would encourage you try them out. In the meantime, why not consider this approach?
During the cull event when you load data from the root directory, why not save the name of your sub-directory to a redundant textbox so that your delete routine can ‘see’ it and execute the delete routine before the rest of your code, which you say works fine, runs?
This is to say:
Dim dtl = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Cake Orders\" & RedundantTextbox.Text))
Surely, you would not be charged with finding a cheeky way to avoid being eaten alive by software programming which sometimes can be a brain-eating dinosaur.
this is my first post here in stackoverflow,but I followed too much about my favourite vb.net articles...
now I want to share my little experience and ask another one that I can't do...
I have precompiled pdf file with some field, textbox and checkbox. My goal is after user filling, open the pdf file with my software that can read text value from textbox and state form check box. The first is done, check the following code:
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
'open file dialog code...
Dim pdfTemplate As String = lbl_file.Text
Dim readerPDF As New PdfReader(pdfTemplate)
Dim name As Object = readerPDF.AcroFields.GetField("name")
Try
txt_name.Text = name
Catch Ex As Exception
End Try
This part of code, search a textbox called "name" and put here text proprieties into my text box. WOW...but to check a checkbox state?do you have any idea please? Thank's in advance for all.
maybe i found a way to do...check this and, what do you think about?
Dim male As Object = readerPDF.AcroFields.GetField("male")
If male = "On" Then
cmb_male.Checked = True
End If
I saw in acrobat default output value from checkbox is "On" than is enough check it and change checkbox value as needed.
There are 3 printers showing when I print from Excel, Outlook etc.
When I call PrintDialog1.ShowDialog() it is only showing the default printer.
How can I show all available printers?
Thank you
This should get you a list of installed printers, if it they don't show up with this code, it may be that you need to update drivers?
Dim controller As New ServiceController("Spooler")
If controller.Status = ServiceControllerStatus.Running Then
'http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fe615570-ce3e-419b-86fc-7f98aed71c8f
For Each currentPrinter As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
'now add the currentPrinter to a list ...
Next
End If
I have a small requirement and that is as follows:
I have opened a file using the "openFileDialog" provision and i have set the filer as (*.txt). Now when the openFileDialog is displayed, only the test files are visible, but the user can also select "All Files" and choose any other file type.
Now what is require is that, if a file type other than .txt is selected by the user, i want to display an error message.
So is there any provision by which i can get to know the file type that is selected by the user.
Regards,
George
Look at http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx
Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim extension As String
extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)
You can use the FileOK event to show the message box while the dialog is still open. Use the GetExtension method to determine the extension.
You should also look at the Filter property of the dialog. If you set it correctly "All Files" should not be shown anymore.
Example:
dlg.Filter = "Test-Files (*.txt)|*.txt"