すらすらプログラマーへのお問い合わせ
すらすらのブログ
アクセス数  累計:000,101,974  昨日:000,000,160  本日:000,000,121
Loading
プロパティグリッド
.NET プログラミング
プロパティグリッド
印刷
リストボックス
スレッド
リストビュー
インストーラー
やってみよう!

できるVisual Studio 2015 Windows /Android/iOS アプリ対応

独習C# 第3版

VisualBasic2013パーフェクトマスター (Perfect Master SERIES)

プログラミング.NET Framework 第4版 (Microsoft Press)

VisualBasic2013逆引き大全555の極意

猫でもわかるWindowsプログラミング 第4版 (猫でもわかるプログラミング)

VisualC#2013逆引き大全555の極意

ファイル選択ダイアログ 最終更新:2010/07/22
【Amazon ランキング:ゲーム - ダウンロード】

 プロパティグリッドにファイル名を表示したい場合に参照用のファイルオープンダイアログを使用する方法を紹介します。
エディタクラスの作成
まず、ファイルダイアログを表示するためのエディタクラスを作成します。エディタ画面に OpenFileDialog を使っています。
VB[ImagePropertyEditor.vb]
Imports System.Windows.Forms.Design

Public Class ImagePropertyEditor
    
Inherits System.Drawing.Design.UITypeEditor

    
' ファイルオープンダイアログ
    Private _editorUi As OpenFileDialog = Nothing

    ' 編集スタイルの取得
    Public Overrides Function GetEditStyle( _
        
ByVal context As System.ComponentModel.ITypeDescriptorContext) _
        
As System.Drawing.Design.UITypeEditorEditStyle

        
' モーダルウィンドウスタイル
        Return System.Drawing.Design.UITypeEditorEditStyle.Modal

    
End Function

    ' 値の編集
    Public Overrides Function EditValue( _
        
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
        
ByVal provider As System.IServiceProvider, _
        
ByVal value As ObjectAs Object

        ' エディタサービスの取得
        Dim editservice As IWindowsFormsEditorService = _
            provider.GetService(
GetType(IWindowsFormsEditorService))

        
' ファイルオープンダイアログの作成
        If _editorUi Is Nothing Then
            _editorUi = New OpenFileDialog()

            
' ダイアログの設定
            _editorUi.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|" & _
                
"PNG files (*.png)|*.png|all files (*.*)|*.*"
            _editorUi.CheckFileExists = True
        End If

        ' ダイアログに値を設定
        _editorUi.FileName = DirectCast(value, String)

        
' ファイルオープンダイアログ表示
        If _editorUi.ShowDialog() = DialogResult.OK Then
            Return _editorUi.FileName
        
Else
            Return value
        
End If

    End Function

End
 Class
C#[ImagePropertyEditor.cs]
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropatyGrid_CS
{
    
public class ImagePropertyEditor: System.Drawing.Design.UITypeEditor 
    {
        
// ファイルオープンダイアログ
        private OpenFileDialog _editorUi = null;

        
// 編集スタイルの取得
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(
            System.ComponentModel.
ITypeDescriptorContext context)
        {
            
// モーダルウィンドウスタイル
            return System.Drawing.Design.UITypeEditorEditStyle.Modal;
        }

        
// 値の編集
        public override object EditValue(
            System.ComponentModel.
ITypeDescriptorContext context,
            
IServiceProvider provider,
            
object value)
        {

            // エディタサービスの取得
            IWindowsFormsEditorService editservice =
                (
IWindowsFormsEditorService)provider.GetService(
                
typeof(IWindowsFormsEditorService));

            
// 編集用のユーザーインターフェイス作成
            if (_editorUi == null)
            {
                _editorUi = 
new OpenFileDialog();

                                // ダイアログの設定
                _editorUi.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|" +
                    
"PNG files (*.png)|*.png|all files (*.*)|*.*";
                _editorUi.CheckFileExists = 
true;
            }

            
// ダイアログに値を設定
            _editorUi.FileName = value as string;

            
// ファイルオープンダイアログ表示
            if (_editorUi.ShowDialog() == DialogResult.OK)
            {
                
// 編集された値を返す
                return _editorUi.FileName;
            }
            
else
            {
                
return value;
            }
        }
    }
}
次にファイル名のプロパティを持ったクラス [Class4] を作成します。
ファイル名プロパティに System.ComponentModel.Editor 属性を設定して先ほど作成した [ImagePropertyEditor]との関連づけを行います。
VB[Class4.vb]
Public Class Class4

    
' クラスメンバ変数
    Private _fileName As String = String.Empty

    
' イメージプロパティ
    <System.ComponentModel.Category("すらすら")> _
    <System.ComponentModel.Description(
"イメージファイル名のプロパティ")> _
    <System.ComponentModel.Editor(
GetType(ImagePropertyEditor), _
                                  
GetType(System.Drawing.Design.UITypeEditor))> _
    
Public Property ImageFile() As String
        Get
            Return _fileName
        
End Get
        Set(ByVal value As String)
            _fileName = value
        
End Set
    End Property

End
 Class
C#[Class4.cs]
using System;
using System.Collections.Generic;
using System.Text;

namespace PropatyGrid_CS
{
    
class Class4
    {

        
//クラスメンバ変数
        private string _fileName = string.Empty;

        
// イメージプロパティ
        [System.ComponentModel.Category("すらすら")]
        [System.ComponentModel.
Description("イメージファイル名のプロパティ")]
        [System.ComponentModel.
Editor(typeof(ImagePropertyEditor),
            
typeof(System.Drawing.Design.UITypeEditor))]
        
public string ImageFile
        {
            
get { return _fileName; }
            
set { _fileName = value; }
        }
    }
}
※このページで紹介しているサンプルコードについて管理者は動作保障をいたしません※
※サンプルコードを使用する場合は、自己責任でお願いします※

【楽天 ランキング:スマートフォン】




このサイトはフリーソフトのMerge HTMLで作成されています。
このサイトはリンクフリーです。

ページの先頭に戻る Copyright© 2010-2015 Jun.Shiozaki All rights reserved.