2012-02-24 21 views
1

ラベルを継承するMLableがあります。バックカラーは赤です。私は赤い背景のフォーム上で使用することができます。しかし、私がMLabelのカスタムコントロールから背景を黒に変更したい場合は、既にラベルを追加しても効果はありません。新しいMLabelの背景は黒、他は赤です。何?ビジュアルプロパティが変更された後、継承されたコントロールがリフレッシュされない

1つずつ変更する必要がありますか?

サンプルコード:

MLabel.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace TestCControl 
{ 
    public partial class CustomControl1 : Label 
    { 
     public CustomControl1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnPaint(PaintEventArgs pe) 
     { 
      base.OnPaint(pe); 
     } 
    } 
} 

のForm1.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace TestCControl 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

Form1.Designer.cs

namespace TestCControl 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.customControl11 = new TestCControl.CustomControl1(); 
      this.customControl12 = new TestCControl.CustomControl1(); 
      this.SuspendLayout(); 
      // 
      // customControl11 
      // 
      this.customControl11.AutoSize = true; 
      this.customControl11.BackColor = System.Drawing.Color.Black; 
      this.customControl11.Location = new System.Drawing.Point(50, 23); 
      this.customControl11.Name = "customControl11"; 
      this.customControl11.Size = new System.Drawing.Size(86, 13); 
      this.customControl11.TabIndex = 0; 
      this.customControl11.Text = "customControl11"; 
      // 
      // customControl12 
      // 
      this.customControl12.AutoSize = true; 
      this.customControl12.BackColor = System.Drawing.Color.Maroon; 
      this.customControl12.Location = new System.Drawing.Point(140, 74); 
      this.customControl12.Name = "customControl12"; 
      this.customControl12.Size = new System.Drawing.Size(86, 13); 
      this.customControl12.TabIndex = 1; 
      this.customControl12.Text = "customControl12"; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Controls.Add(this.customControl12); 
      this.Controls.Add(this.customControl11); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private CustomControl1 customControl11; 
     private CustomControl1 customControl12; 
    } 
} 

あなたのいずれかを見ることができるようにカスタムコントロールには他の黒がありますoon backcolor。 MLabelの変更は、以前に追加されたwinformのコントロールには影響しません。

+0

実行したことを示すサンプルコードと詳細を入力してください。 –

+0

カスタムコントロールを作成し、ラベルから継承し、名前をMLabelにします。 BackColorをDesignerからRedに変更します。 WinFormで使用します。 MLabel.csに戻り、バックカラーをRedに変更します。コンパイル。 winformに追加します。あなたは、最初のMLabelの背景赤が他の黒であることがわかります。私は間違っているか、またはMLabelsの背景をすべて黒に変えなければならないのか? – Murat

+0

ラベルの背景色をすべて変更したい場合は、同じ値でなく同じ色の変数を使用する必要があります。 –

答えて

2

ここで確認することがいくつかあります。

すべてのMLabelが同じバージョンを参照していますか? (おそらく)

MLabelコントロールの背景をどのように設定しますか?デフォルトの背景色を変更しますか?

既存のMLabelsをどのように変更していますか、コードで手動で設定していますか、またはデフォルトの背景色に頼っていますか?

また、winformコードを確認してください。デザイナーコードがMLabelの背景色を明示的に設定する場合、これはコントロールが持つ可能性のあるデフォルトを上書きします。その場合は、デフォルトを使用するように背景色の設定を削除するか、手動で各設定を手動で変更する必要があります。

上記のすべてが正しいと思われる場合は、ここでコードやその他の情報を参考にしてください。

実際のコードを見た後に更新回答:あなたはこのような何かをしたいと思うでしょうCustomControl1で

:Form1.Designer.csで

System.Drawing.Color _backColor = System. 
protected override System.Drawing.Color BackColor 
{ 
    get{return _backColor;} 
    set{_backColor = value;} 
} 

はBackColorプロパティを設定する行を削除します。これらはバックカラーを明示的に設定しており、デフォルトの色を許可していません。

this.customControl11.BackColor = System.Drawing.Color.Black; 
+0

これらはすべて同じバージョンを参照しています。 Visual StudioデザイナーからBackColorを変更します。コードから変更すると、すべて正常に動作します。 私はforms.Designerをチェックします。csファイル、以前に追加されたMLabels BackColorはまだ赤色のままです。黒に変更しても効果はありません。再度、私はVisual Studio DesignerからBackColorを変更しています。 – Murat

+0

私はあなたの問題のように聞こえるコードは見ずに、私の小切手リストにも追加しました。デザイナーから背景色を変更すると、それがデザイナーコードに設定されます。この明示的なセットは、コントロールの既定の色をオーバーライドします。 –

+0

余分なコードはありません。私はデザイナーからこれらすべてのことをやっています。公共部分クラスMLabel:はい – Murat

関連する問題