0

カスタム FileNameEditor を実装したい。独自のフィルターを設定し、複数のファイルを選択できるようにしたい。

public class Settings
{
    [EditorAttribute(typeof(FileNamesEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string FileNames { get; set; }
}

public class FileNamesEditor : FileNameEditor
{
    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "Word|*.docx|All|*.*";
        base.InitializeDialog(openFileDialog);

    }
}

これはフィルター プロパティを無視し、複数のファイルを選択できますが、Settings.FileNames の型が string[] であり、派生クラスの結果が string であるため、それらを Settings.FileNames プロパティに割り当てることはできません。派生クラスに openFileDialog の FileNames を返すように指示するにはどうすればよいですか? また、フィルターを機能させるにはどうすればよいですか? 私は何が欠けていますか?

4

3 に答える 3

3

必要な再注文を除いて、元のコードは私にとってはうまくいきました。変更の前に base.Initialize を呼び出す必要があります。そうしないと上書きされます (デバッグするとうまく表示されます)。

public class FileNamesEditor : FileNameEditor
{
    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
        base.InitializeDialog(openFileDialog);
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "Word|*.docx|All|*.*";
    }
}
于 2013-09-25T08:24:08.433 に答える
0

おそらく、string[]にArrayEditorを使用します

public class Settings
{
  [EditorAttribute(typeof(System.ComponentModel.Design.ArrayEditor),  typeof(System.Drawing.Design.UITypeEditor))]
  public string[] FileNames { get ; set; }
}
于 2012-10-20T15:59:15.023 に答える
0

よし、これが仕組みだ…

public class FileNamesEditor : UITypeEditor
{
    private OpenFileDialog ofd;
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if ((context != null) && (provider != null))
        {
            IWindowsFormsEditorService editorService =
            (IWindowsFormsEditorService)
            provider.GetService(typeof(IWindowsFormsEditorService));
            if (editorService != null)
            {
                ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.Filter = "Word|*.docx|All|*.*";
                ofd.FileName = "";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    return ofd.FileNames;
                }
            }
        }
        return base.EditValue(context, provider, value);
    }
}
于 2012-10-21T00:16:40.300 に答える