2017-07-20 8 views
0

私はそれは固定サイズである必要がありカスタムボタンのテキストとイメージのプロパティを取得するには?

、ボタンclass.Theボタンから継承されたカスタムボタンは、物事を以下含まれるべきで作成しました。(私はそれが正常に動作し、これを完成) コントロールのサイズに基づいてテキストを揃えます画像をコントロールに転送します。 複数のアプリケーションにこのコントロールを使用する必要があります。アプリケーションユーザー に基づいて、ボタンのテキストとイメージの値を指定します。

C#の私は、コンストラクタを呼び出します値に基づいて、ボタンのテキストや画像値を取得できますか

Here, there is four constructor, 


public customButton() 
     { 
      InitializeComponent(); 
     } 


     /// <summary> 
     /// This constructor will call when button contains Text property only. 
     /// call the base constructor with Image is null. 
     /// </summary> 
     /// <param name="text"></param> 
     public customButton(string text) 
     { 
      //customButton(text, null); 
      InitializeComponent(); 
     } 


     /// <summary> 
     /// This constructor will call when button contains Image property only. 
     /// call the base constructor with text is null. 
     /// </summary> 
     /// <param name="icon"></param> 
     public customButton(Image icon) 
     { 
      //customButton(null, icon); //How to call the constructor 
      InitializeComponent(); 
     } 


     /// <summary> 
     /// This constructor will call when button contains both image and text values. 
     /// </summary> 
     /// <param name="text"></param> 
     /// <param name="icon"></param> 
     public customButton(string text, Image icon) 
     { 
      InitializeComponent(); 
      if (icon != null) 
      { 
       iconAvailable = true; 
       this.Image = icon; //Here I Need to get the icon value here,    } 
      if (text != string.Empty || text != null) 
      { 
       textAvailable = true; 
       this.Text = text;// // Here Need to get the text value here, Not completed 
      } 

      //align icon and text on booleans 
      setControlAlignment(); 
     } 

編集:ボタンはテキストのみが含まれている場合

、テキストボタンは画像のみが含まれている場合、画像は中央の中央にある必要があり、真ん中の中心部に位置する必要があります。だから私はコンストラクタを呼び出す必要があることに基づいて、テキストとイメージの可用性をチェックする必要があります。イメージとテキストの値を取得するには?コンストラクタコール

答えて

0

これを行うための特別な構文があります。専門的なcontructorsから削除するために必要なInitializeComponentへの呼び出し、それ以外の場合は二回呼び出されます:

 public customButton() : this(null, null) 
     { 
     } 


     /// <summary> 
     /// This constructor will call when button contains Text property only. 
     /// call the base constructor with Image is null. 
     /// </summary> 
     /// <param name="text"></param> 
     public customButton(string text) : this(text, null) 
     { 
     } 


     /// <summary> 
     /// This constructor will call when button contains Image property only. 
     /// call the base constructor with text is null. 
     /// </summary> 
     /// <param name="icon"></param> 
     public customButton(Image icon) : this(null, icon) 
     { 
     } 


     /// <summary> 
     /// This constructor will call when button contains both image and text values. 
     /// </summary> 
     /// <param name="text"></param> 
     /// <param name="icon"></param> 
     public customButton(string text, Image icon) 
     { 
      InitializeComponent(); 
      if (icon != null) 
      { 
       iconAvailable = true; 
       this.Image = icon; //Here I Need to get the icon value here,    } 
      if (text != string.Empty || text != null) 
      { 
       textAvailable = true; 
       this.Text = text;// // Here Need to get the text value here, Not completed 
      } 

      //align icon and text on booleans 
      setControlAlignment(); 
     } 
+0

ありがとう、私はテキストとアイコンの値が必要です。それを得る方法?画像とテキストの値に基づいて、テキストと画像をボタンコントロールに合わせます。 – user8331467

+0

私はあなたが何を意味するのか分かりません。 'customButton'をインスタンス化する際に、これらの値を渡します(' CustomButton'に名前を変更することをお勧めします)。このカスタムビートのように、新しいカスタムボタン(Button Text)、Image.FromFile(@ "C:\ test.png")) ; '。この例では、イメージはファイルからロードされますが、リソースでもプログラムで描画されていても、リモートソースからロードされていてもかまいません。 –

0

は、なぜあなたは、あなたが使用することができ、あなたはコンストラクタに渡す値を格納するプライベート変数を作成しないでくださいそれら後で...

すなわち

private string _text; 
private Image _icon; 

public customButton() : this(null, null) 
    { 
    } 


    /// <summary> 
    /// This constructor will call when button contains Text property only. 
    /// call the base constructor with Image is null. 
    /// </summary> 
    /// <param name="text"></param> 
    public customButton(string text) : this(text, null) 
    { 

    } 


    /// <summary> 
    /// This constructor will call when button contains Image property only. 
    /// call the base constructor with text is null. 
    /// </summary> 
    /// <param name="icon"></param> 
    public customButton(Image icon) : this(null, icon) 
    { 

    } 


    /// <summary> 
    /// This constructor will call when button contains both image and text values. 
    /// </summary> 
    /// <param name="text"></param> 
    /// <param name="icon"></param> 
    public customButton(string text, Image icon) 
    { 
     InitializeComponent(); 

    _text = this.text; 
     _icon = this.icon; 

     if (icon != null) 
     { 
      iconAvailable = true; 
      this.Image = this.icon; //Here I Need to get the icon value here,    } 
     if (text != string.Empty || this.text != null) 
     { 
      textAvailable = true; 
      this.Text = this.text;// // Here Need to get the text value here, Not completed 
     } 

     //align icon and text on booleans 
     setControlAlignment(this.text, this.icon); 
    } 

    private void setControlAlignment(string text, Image icon); 
    { 
     HorizontalAlignment textPos; 
     HorizontalAlignment iconPos; 

     if (!string.IsNullOrWhiteSpace(text) && this.icon != null)  // If we have both text and icon values 
     { 
    textPos = HorizontalAlignment.Left; 
      iconPos = HorizontalAlignment.Right; 
     } 
    else if (string.IsNullOrWhiteSpace(this.text) && this.icon != null) 
    { 
    iconPos = HorizontalAlignment.Center; 
     } 
    else if (!string.IsNullOrWhiteSpace(this.text) && this.icon == null) 
    { 
    textPos = HorizontalAlignment.Center; 
     } 

    // and whatever else you want to do... 
    } 

ライン: this.Image = this.icon。 //ここでアイコンの値を取得する必要があります と this.Text = this.text; // //ここでテキスト値を取得する必要があります。完了しません。

「テキスト」として既に動作しているはずです。 "icon"は渡された値に設定されます。

public customButton(文字列テキスト、画像アイコン) はすべてのコンストラクタによって呼び出されていますので、アクセスするには_textと_iconを一度設定する必要がありますコード内の他のメソッドでも使用できます。

これらの変数にアクセスする必要がある場合(現在はsetControlAlignmentで定義されています)、必要に応じて_textPosと_iconPosをグローバルにすることができます。

コードは、setControlAlignmentメソッドに値を渡すときに、_textまたは_icon(割り当て済みですが使用されていない)を使用しません。完全なコードをここに入れていないので、他のメソッドでこれらの値にアクセスする必要がある場合は、単に_textまたは_iconにアクセスできます。

関連する問題