2016-10-04 5 views
1

私はWindowsアプリケーションで作業しています。ユーザー定義プロパティをプロパティタブに追加する可能性はありますか?C#:Windowsフォームの[プロパティ]タブにユーザー定義のプロパティを追加します。

enter image description here

すなわち:私はプロパティタブにプロパティを追加するtextboxを持っています。プロパティの値は「アルファベットのみ数だけアルファベットと特殊文字番号や特殊文字」です。 アルファベットのみをの値に設定すると、テキストボックスにはアルファベットしか使用できません。どのようにこの機能を行うことができますか?

+0

あなたの質問は不明です。アプリケーションで 'PropertyGrid'コントロールを使用していて、それにカスタム項目を追加したいのですか?あるいは、他の開発者が使用するために構築しているコントロール/コンポーネントのプロパティを追加したいですか? – Dai

+0

@Dai私は、プロパティを追加するテキストボックスを持っています。プロパティの値は、アルファベットのみ、数字のみ、アルファベットと特殊文字、数字と特殊文字です。アルファベットのみの値を選択すると、テキストボックスはアルファベットのみを許可する必要があります。どのようにこの機能を行うことができますか? – user2115618

答えて

2

はい..あなたは、以下のサンプルでこれを行うことができOPの更なるスペックどおり

を編集しました。ユーザーコントロール/カスタムコントロールを作成すると、プロパティウィンドウにプロパティを追加するオプションがあります。

C#のコード - 私のユーザーコントロールクラス

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 
using System.Globalization; 

namespace TestSOF 
{ 
    [ToolboxBitmap(typeof(DataGrid))] 
    public partial class myTextBox : UserControl 
    { 
     TextBoxTypeSettings.Mode textBoxTypeSettings; //edited to support design time as well 

     public myTextBox() 
     { 
      InitializeComponent(); 
     } 

     [Browsable(true)] 
     [EditorBrowsable(EditorBrowsableState.Always)] 
     [Category("u4sSearchBox")] 
     public TextBoxTypeSettings.Mode TextBoxType //edited to support design time as well 
     { 
      get 
      { 
       return this.textBoxTypeSettings; 
      } 

      set 
      { 
       this.textBoxTypeSettings = value; 
      } 
     } 

     private void txtValue_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      switch (textBoxTypeSettings) //edited to support design time as well 
      { 
       case TextBoxTypeSettings.Mode.AlphabetOnly: 
        e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back); 
        break; 
       case TextBoxTypeSettings.Mode.NumberOnly: 
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) 
        { 
         e.Handled = true; 
        } 

        // only allow one decimal point 
        if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) 
        { 
         e.Handled = true; 
        } 
        break; 
      } 
     } 
    } 

    [TypeConverter(typeof(TextBoxTypeSettingsConverter))] 
    public class TextBoxTypeSettings 
    { 
     private Mode _TextBoxMode; 

     [Serializable] 
     public enum Mode 
     { 
      NumberOnly, 
      AlphabetOnly 
     } 

     [Browsable(true)] 
     [Category("u4sSearchBox")] 
     [DefaultValue("")] 
     [Description("Gets and sets the textbox's mode")] 
     [NotifyParentProperty(true)] 
     [EditorBrowsable(EditorBrowsableState.Always)] 
     [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
     public Mode TextBoxMode 
     { 
      get 
      { 
       return _TextBoxMode; 
      } 
      set 
      { 
       _TextBoxMode = value; 
      } 
     } 
    } 

    public class TextBoxTypeSettingsConverter : ExpandableObjectConverter 
    { 
     // This override prevents the PropertyGrid from 
     // displaying the full type name in the value cell. 
     public override object ConvertTo(
      ITypeDescriptorContext context, 
      CultureInfo culture, 
      object value, 
      Type destinationType) 
     { 
      if (destinationType == typeof(string)) 
      { 
       return ""; 
      } 

      return base.ConvertTo(
       context, 
       culture, 
       value, 
       destinationType); 
     } 
    } 
} 
+0

ありがとうございます。病気を今すぐ試して、あなたに返信 – user2115618

+0

素晴らしい!あなたは歓迎です –

+0

@ user2115618私の編集された答えを参照してください..それはあなたを助けるでしょう。 –

関連する問題