Add a badge to a C# winforms control

前端 未结 3 1682
暖寄归人
暖寄归人 2020-12-11 13:24

When writing in CSS I can add a class of \"badge\" and get what I want. A small number near a button or tab with some styling to it, to show that this control has pending in

3条回答
  •  借酒劲吻你
    2020-12-11 14:01

    Here is a rough way to do it with a UserControl:

    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();
            }
        }
    

    Then put it in your form:

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

    Gives you:

    enter image description here

提交回复
热议问题