限制 OpenFileDialog 的多选 [英] Limit OpenFileDialog's Multiselect

查看:52
本文介绍了限制 OpenFileDialog 的多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 OpenFileDialog,它的 MultiSelect 属性是 ON.我的问题是如何限制要选择的项目数量,例如只能选择 5 个项目?

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?

谢谢

推荐答案

当用户单击确定"按钮时,您可以使用 FileOk 事件检查用户选择的文件.如果您不满意,则显示一条消息并将 CancelEventArgs.Cancel 属性设置为 True 以防止对话框关闭.像这样:

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

这篇关于限制 OpenFileDialog 的多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆