2015-04-20 84 views
0

當CSS寫,我可以添加一個類「徽章」,並得到我想要的東西。靠近按鈕或標籤附近的一小部分樣式,以顯示此控件具有需要審閱的待處理信息。徽章添加到C#的WinForms控制

甚至還有人試圖做我想要iOS上

什麼,我想這樣做對的WinForms在任一按鈕或選項卡後here。如果WPF中提供了更簡單的解決方案,那麼我可能會考慮使用它。

這是表示什麼,我想實現一例的印象:

Buttons and controls with badges

+3

'伊恩簡單的解決方案是在WPF提供我可能會考慮使用instead.' - 當然,是的WinForms完全無用,不支持什麼。你有什麼嘗試? –

+3

@HighCore你不覺得你對winforms的「無用」感到厭倦嗎? – Eminem

+1

@Eminem no。一點也不。無論你在的WinForms實現,我可以實現在WPF同樣用30%的精力和代碼,並用更大的可擴展性,可定製性,和適當的分離量。 winforms是無用的。 –

回答

4

這裏是一個靜態的裝飾器類,非常快,而骯髒的方式..

它可以添加標籤許多控件,它包括點擊動作,動態文本和具有代碼刪除標籤。

an adorned button

添加徽章的按鈕佔一行:

public Form1() 
    { 
     InitializeComponent(); 
     // adorn one Button with a Badge Label: 
     Adorner.AddBadgeTo(button1, "123"); 
     // if you want to you can add a click action: 
     Adorner.SetClickAction(button1, dobidoo); 
    } 

    // a test action 
    void dobidoo(Control ctl) 
    { 
     Console.WriteLine("You have clicked on :" + ctl.Text); 
    } 

這裏是裝飾器類:

static class Adorner 
{ 
    private static List<Control> controls = new List<Control>(); 

    static public bool AddBadgeTo(Control ctl, string Text) 
    { 
     if (controls.Contains(ctl)) return false; 

     Badge badge = new Badge(); 
     badge.AutoSize = true; 
     badge.Text = Text; 
     badge.BackColor = Color.Transparent; 
     controls.Add(ctl); 
     ctl.Controls.Add(badge); 
     SetPosition(badge, ctl); 

     return true; 
    } 

    static public bool RemoveBadgeFrom(Control ctl) 
    { 
     Badge badge = GetBadge(ctl); 
     if (badge != null) 
     { 
      ctl.Controls.Remove(badge); 
      controls.Remove(ctl); 
      return true; 
     } 
     else return false; 
    } 

    static public void SetBadgeText(Control ctl, string newText) 
    { 
     Badge badge = GetBadge(ctl); 
     if (badge != null) 
     { 
       badge.Text = newText; 
       SetPosition(badge, ctl); 
     } 
    } 

    static public string GetBadgeText(Control ctl) 
    { 
     Badge badge = GetBadge(ctl); 
     if (badge != null) return badge.Text; 
     return ""; 
    } 

    static private void SetPosition(Badge badge, Control ctl) 
    { 
     badge.Location = new Point(ctl.Width - badge.Width - 5, 
            ctl.Height - badge.Height - 5); 
    } 

    static public void SetClickAction(Control ctl, Action<Control> action) 
    { 
     Badge badge = GetBadge(ctl); 
     if (badge != null) badge.ClickEvent = action; 
    } 

    static Badge GetBadge(Control ctl) 
    { 
     for (int c = 0; c < ctl.Controls.Count; c++) 
      if (ctl.Controls[c] is Badge) return ctl.Controls[c] as Badge; 
     return null; 
    } 


    class Badge : Label 
    { 
     Color BackColor = Color.SkyBlue; 
     Color ForeColor = Color.White; 
     Font font = new Font("Sans Serif", 8f); 

     public Action<Control> ClickEvent; 

     public Badge() {} 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      e.Graphics.FillEllipse(new SolidBrush(BackColor), this.ClientRectangle); 
      e.Graphics.DrawString(Text, font, new SolidBrush(ForeColor), 3, 1); 
     } 

     protected override void OnClick(EventArgs e) 
     { 
      ClickEvent(this); 
     } 

    } 
} 

請注意,雖然你可以把它添加到大多數控件,不是所有的工作以及Button。一個TabControl是比較硬裝飾爲Tabs真的不Controls只是漆面上,所以就像添加一個「關閉X」它,你將不得不user draw所有TabPages的徽章..

0

這裏是一個粗略的方式與用戶控件來做到這一點:

public partial class btnControl : UserControl 
    { 
     public Label label = new Label(); 
     public TextBox box = new TextBox(); 

     public btnControl() 
     { 
      this.label = new System.Windows.Forms.Label(); 
      this.box = new System.Windows.Forms.TextBox(); 
      this.SuspendLayout(); 
      // 
      // label 
      // 
      this.label.AutoSize = true; 
      this.label.ForeColor = System.Drawing.Color.White; 
      this.label.Location = new System.Drawing.Point(4, 7); 
      this.label.Name = "label"; 
      this.label.Size = new System.Drawing.Size(35, 13); 
      this.label.TabIndex = 0; 
      this.label.Text = "label"; 
      // 
      // box 
      // 
      this.box.Location = new System.Drawing.Point(110, 3); 
      this.box.Name = "box"; 
      this.box.Size = new System.Drawing.Size(31, 20); 
      this.box.TabIndex = 1; 
      this.box.Enabled = false; 
      // 
      // btnControl 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.BackColor = System.Drawing.Color.Blue; 
      this.Controls.Add(this.box); 
      this.Controls.Add(this.label); 
      this.Name = "btnControl"; 
      this.Size = new System.Drawing.Size(144, 26); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 
     } 
    } 

然後把它放在您的表格:

private void Form1_Load(object sender, EventArgs e) 
     { 
      btnControl Control = new btnControl(); 
      this.Controls.Add(Control); 
      Control.label.Text = "Home"; 
      Control.box.Text = "42"; 
     } 

給您:

enter image description here

3

真的最簡單,達到此目的的最佳方法是創建一個新的自定義UserControl。只需添加一個按鈕並在右側插入一個標籤即可。然後加入getterssettersUserControl裏面你的控件。這裏的/ GET的一個例子設置來配置按鈕的通知:

public String ButtonNotification { 
    get { return yourUserControlLabel.Text; } 
    set { 
     if (value == null || value == "") { yourUserControlLabel.Visibility = Hidden; } 
     else { yourUserControlLabel.Visibility = Visible; } 

     yourUserControlLabel.Text = value; 
    } 
} 

然後,你可以自定義標籤的可見性和其他性能與getter/setter方法。