如何使自定义控件的属性打开文件对话框? [英] How do I make a property of a custom control open a file dialog?

查看:165
本文介绍了如何使自定义控件的属性打开文件对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,其属性包含目标计算机上存在的文件位置的名称(完整路径).

I have a custom control with a property that holds the name (full path) to a file location that exists on the target computer.

确切的路径将根据目标PC的类型而有所不同,并且通常在我将自定义控件添加到Form后立即设置,而我仍处于项目的设计模式下,因此当我的应用程序运行时,它会启动属性中的文件名.

The exact path will vary according to type of target pc and is typically set right after I add the custom control to my Form, while I am still in design mode of my project, so that when my application runs, it picks up the filename from the property.

如果该属性打开了一个文件对话框以让我浏览到该位置(类似于浏览图像和颜色属性时如何打开对话框),将会很方便,但是在Visual Basic中这似乎是不可能的.

It would be convenient if the property opened a file dialog to let me browse to the location (similar to how dialogs are opened when browsing for image and color properties), but this doesn't seem to be possible in visual basic.

经过数天的搜索,我发现有几篇文章涉及其他编程语言的主题(请参见下面的示例片段),但是我仍无法弄清楚如何使其在Visual Basic中起作用.

After googling for days I have found a couple of articles that touch the subject for other programming languages (see example snippet below) but I haven't been able to work out how to make it work for visual basic.

这里是我发现提到使用编辑器的摘要,这可能是入门的线索.

Here is a snippet I found that mentions the use of an editor, which may be a clue to get started.

[Editor(typeof(FileSelectorTypeEditor), typeof(UITypeEditor))]
public string Filename
{
   get { return _filename; }
   set { _filename = value; }
}

希望那里的某个人可以以正确的方式带领我.

Hope someone out there can lead me in the right way.

推荐答案

FileSelectorTypeEditor可能是从您可以使用标准类来实现这两种方法,也可以使用您自己的标准类来扩展默认值,正如您在所找到的C#源代码中所看到的那样.

You can implement both, using the standard class or extend the default with your own, as you have seen in those C# sources you have found.

这里我使用的是专门的FileNameEditor类,命名为(有些缺乏想象力) SpecializedFileNameEditor ,而标准的FolderNameEditorUITypeEditor分配给一个类的两个属性.

Here I'm using a specialized FileNameEditor class, named (with some lack of imagination) SpecializedFileNameEditor and the standard FolderNameEditor assigning the UITypeEditor to two properties of a class.

ImagePath属性编辑器是SpecializedFileNameEditor对象,该对象使用OpenFileDialog,在其中预选择了过滤器.它还覆盖了 EditValue 方法,以将关联属性(此处为ImagePath)的当前值(如果有)设置为OpenFileDialog的InitialDirectory.
ImageFolder属性编辑器是标准的FolderNameEditor,它将打开FolderBrowserDialog.

► The ImagePath property editor is the SpecializedFileNameEditor object, which uses an OpenFileDialog, where a filter is pre-selected. It also overrides the EditValue method, to set the current value, if any, of an associated property (here, ImagePath) as the InitialDirectory of the OpenFileDialog.
► The ImageFolder property editor is a standard FolderNameEditor, which opens a FolderBrowserDialog.

我还要附加一个 ExpandableObjectConverter 类型转换器,因此您可以将这两个属性显示为PropertyGrid中的可扩展属性选择器.

I'm also attaching an ExpandableObjectConverter type converter, so you can present the two properties as an expandable property selector in a PropertyGrid.

您可以在此处查看示例:
如何将用户控件的子控件绑定到公共属性

You can see an example here:
How to bind child Controls of a User Control to a Public Property

Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.IO
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

<TypeConverter(GetType(ExpandableObjectConverter))>
Public Class ImagePickerClass

    Public Sub New()  
        ' Initialize [...]
    End Sub

    <Editor(GetType(SpecializedFileNameEditor), GetType(UITypeEditor))>
    Public Property ImagePath As String

    <Editor(GetType(FolderNameEditor), GetType(UITypeEditor))>
    Public Property ImageFolder As String

    Public Class SpecializedFileNameEditor
        Inherits FileNameEditor

        Private currentValue As String = String.Empty

        Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
            If TypeOf value Is String Then
                currentValue = DirectCast(value, String)
            End If
            Return MyBase.EditValue(context, provider, value)
        End Function

        Protected Overrides Sub InitializeDialog(ofd As OpenFileDialog)
            MyBase.InitializeDialog(ofd)
            If Not currentValue.Equals(String.Empty) Then
                ofd.InitialDirectory = Path.GetDirectoryName(currentValue)
            End If
            ofd.Filter = "PNG Images (*.png)|*.png"
        End Sub
    End Class
End Class

这篇关于如何使自定义控件的属性打开文件对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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