すらすらプログラマーへのお問い合わせ
すらすらのブログ
アクセス数  累計:000,099,175  昨日:000,000,137  本日:000,000,075
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 ランキング:DVD - アニメ】

前回はプロパティグリッドに表示されている値をドロップダウン形式で編集しましたが、それともうひとつダイアログ形式のウィンドウを表示して編集する方法もあります。
今回はそのダイアログ形式のウィンドウでプロパティを編集する方法を紹介します。
ユーザーインターフェイスの作成
まずはプロパティの編集に使うユーザーインターフェイスを作成します。
プロジェクトの新しい項目の追加で新規フォーム「Dialog1」を作成してください。VBの場合はダイアログフォームを使うほうが便利です。
使用するコントロールは次の5つです。

MonthCalender:[monthCalendar1]
Label:[label1]
TextBox:[textBox1]
Button:[OK_Button]
Button:[Cancel_Button]

それぞれのコントロールを左のイメージのように配置してください。
プロパティの値はすべてデフォルトを使用します。
VB[Dialog1.vb]
Imports System.Windows.Forms

Public Class Dialog1

    
' Class2 値プロパティ
    Public Property Class2Value() As Class2
        
Get
            Return New Class2(Me.monthCalendar1.SelectionStart, Me.textBox1.Text)
        
End Get
        Set(ByVal value As Class2)
            
Me.monthCalendar1.SelectionStart = value.BirthDay
            
Me.textBox1.Text = value.Name
        
End Set
    End Property

    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                
ByVal e As System.EventArgs) _
                                
Handles OK_Button.Click
        
Me.DialogResult = System.Windows.Forms.DialogResult.OK
        
Me.Close()
    
End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    
ByVal e As System.EventArgs) _
                                    
Handles Cancel_Button.Click
        
Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        
Me.Close()
    
End Sub

End
 Class
C#[Dialog1.cs]
namespace PropatyGrid_CS
{
    
public partial class Dialog1 : Form
    {
        
public Dialog1()
        {
            InitializeComponent();
        }

        
// Class2 値プロパティ
        public Class2 Class2Value
        {
            
get
            {
                
return new Class2(this.monthCalendar1.SelectionStart, this.textBox1.Text);
            }
            
set
            {
                
this.monthCalendar1.SelectionStart = value.Birthday;
                
this.textBox1.Text = value.Name;
            }
        }

        
private void OK_Button_Click(object sender, EventArgs e)
        {
            
this.DialogResult = DialogResult.OK;
            
this.Close();
        }

        
private void Cancel_Button_Click(object sender, EventArgs e)
        {
            
this.DialogResult = DialogResult.Cancel;
            
this.Close();
        }
    }
}
エディタクラスの作成
エディタークラスは基本的に前回のドロップダウン形式と同じです。GetEditStyle メソッドで返すスタイルを [DropDown]⇒[Modal]に変更してEditValue でダイアログを表示するようにします。
VB[Class2Editor.vb]
Imports System.Windows.Forms.Design

Public Class Class2Editor
    
Inherits System.Drawing.Design.UITypeEditor

    
' 編集用ユーザーコントロール
    Private _editorUi As Dialog1 = 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 Dialog1
        
End If

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

        
' 編集用のコントロールを設定
        editservice.ShowDialog(_editorUi)

        
If _editorUi.DialogResult = DialogResult.OK Then
            ' 編集された値を返す
            Return _editorUi.Class2Value
        
Else
            Return value
        
End If

    End Function

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

namespace PropatyGrid_CS 
{
    
public class Class2Editor:System.Drawing.Design.UITypeEditor 
    {

        
// 編集用ユーザーコントロール
        private Dialog1 _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 Dialog1();
            }

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

            
// 編集用のコントロールを設定
            editservice.ShowDialog(_editorUi);

            
if (_editorUi.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                
// 編集された値を返す
                return _editorUi.Class2Value;
            }
            
else
            {
                
return value;
            }              
        }
    }
}
Class2 は前回のドロップダウン形式と同じです。変更はありません。
VB[Class2.vb]
<System.ComponentModel.Editor(GetType(Class2Editor),
 
GetType(System.Drawing.Design.UITypeEditor))> _
<System.ComponentModel.TypeConverter(GetType(Class2PropertyConverter))> _
Public Class Class2

    
' クラスメンバ変数
    Private _birthday As DateTime = Now
    
Private _name As String = "SuraSura"

    ' コンストラクタ
    Public Sub New(ByVal birthday As DateTime, ByVal name As String)

        
' メンバ変数初期化
        _birthday = birthday
        _name = name

    
End Sub

    ' 誕生日プロパティ
    <System.ComponentModel.Category("すらすら")> _
    <System.ComponentModel.Description(
"誕生日")> _
    
Public Property BirthDay() As String
        Get
            Return _birthday
        
End Get
        Set(ByVal value As String)
            _birthday = value
        
End Set
    End Property

    ' 名前プロパティ
    <System.ComponentModel.Category("すらすら")> _
    <System.ComponentModel.Description(
"名前")> _
    
Public Property Name() As String
        Get
            Return _name
        
End Get
        Set(ByVal value As String)
            _name = value
        
End Set
    End Property

    ' 文字列の取得
    Public Overrides Function ToString() As String

        Return String.Format("{0:yyyy/MM/dd},{1}", _birthday, _name)

    
End Function

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

namespace PropatyGrid_CS
{
    [System.ComponentModel.Editor(typeof(Class2Editor),
typeof(System.Drawing.Design.UITypeEditor ))]
    [System.ComponentModel.
TypeConverter(typeof(Class2PropertyConverter))]
    
public class Class2
    {
        
// クラスメンバ変数
        private DateTime _birthday = DateTime.Now;
        
private string _name = "すらすら";

        
// コンストラクタ
        public Class2(DateTime birthday, string name)
        {
            
// メンバ変数初期化
            _birthday = birthday;
            _name = name;
        }

        
// 誕生日プロパティ
        public DateTime Birthday
        {
            
get { return _birthday; }
            
set { _birthday = value; }
        }

        
// 名前プロパティ
        public string Name
        {
            
get { return _name; }
            
set { _name = value; }
        }

        
// 文字列の取得
        public override string ToString()
        {
            
return string.Format("{0:yyyy/MM/dd},{1}", _birthday, _name);
        }
    }
}
Class3 は前回と同じで変更はありません。
VB[Class3.vb]
Public Class Class3

    
' クラスメンバ変数
    Private _class2 As Class2 = New Class2()

    
' Class2値プロパティ
    <System.ComponentModel.Category("すらすら")> _
    <System.ComponentModel.Description(
"Class2 値のプロパティ")> _
    
Public Property Class2Value() As Class2
        
Get
            Return _class2
        
End Get
        Set(ByVal value As Class2)
            _class2 = value
        
End Set
    End Property

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

namespace PropatyGrid_CS
{
    
public class Class3
    {
        
// クラスメンバ変数
        private Class2 _class2 = new Class2();

        
// Class2値プロパティ
        [System.ComponentModel.Category("すらすら")]
        [System.ComponentModel.
Description("Class2値プロパティ")]
        
public Class2 Class2Value
        {
            
get { return _class2; }
            
set { _class2 = value; }
        }
    }
}
※このページで紹介しているサンプルコードについて管理者は動作保障をいたしません※
※サンプルコードを使用する場合は、自己責任でお願いします※

【楽天 ランキング:パソコン・周辺機器 - パソコン周辺機器】




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

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