Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize a checkbox with Winform in C #

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use Winform to customize a checkbox in C#. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Add a user control named: UCRadioButton

Take a look at the attributes.

[Description ("check change event"), Category ("Custom")]

Public event EventHandler CheckedChangeEvent

Private Font _ Font = new Font ("Microsoft Acer", 12)

[Description ("font"), Category ("custom")]

Public new Font Font

{

Get {return _ Font;}

Set

{

_ Font = value

Label1.Font = value

}

}

Private Color _ ForeColor = Color.FromArgb (62, 62, 62)

[Description ("font color"), Category ("custom")]

Public new Color ForeColor

{

Get {return _ ForeColor;}

Set

{

Label1.ForeColor = value

_ ForeColor = value

}

}

Private string _ Text = "radio button"

[Description ("text"), Category ("custom")]

Public string TextValue

{

Get {return _ Text;}

Set

{

Label1.Text = value

_ Text = value

}

}

Private bool _ checked = false

[Description ("selected"), Category ("Custom")]

Public bool Checked

{

Get

{

Return _ checked

}

Set

{

If (_ checked! = value)

{

_ checked = value

If (base.Enabled)

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton1

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton0

}

}

Else

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton10

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton00

}

}

SetCheck (value)

If (CheckedChangeEvent! = null)

{

CheckedChangeEvent (this, null)

}

}

}

}

Private string _ groupName

[Description ("Group name"), Category ("Custom")]

Public string GroupName

{

Get {return _ groupName;}

Set {_ groupName = value;}

}

Public new bool Enabled

{

Get

{

Return base.Enabled

}

Set

{

Base.Enabled = value

If (value)

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton1

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton0

}

}

Else

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton10

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton00

}

}

}

}

When the status change is selected, the corresponding processing needs to be done according to the group name.

Private void SetCheck (bool bln)

{

If (! bln)

Return

If (this.Parent! = null)

{

Foreach (Control c in this.Parent.Controls)

{

If (c is UCRadioButton & & c! = this)

{

UCRadioButton uc = (UCRadioButton) c

If (_ groupName = = uc.GroupName & & uc.Checked)

{

Uc.Checked = false

Return

}

}

}

}

}

Change the selected state when you click

Private void Radio_MouseDown (object sender, MouseEventArgs e)

{

This.Checked = true

}

Do some processing when loading to prevent multiple selections.

Private void UCRadioButton_Load (object sender, EventArgs e)

{

If (this.Parent! = null & & this._checked)

{

Foreach (Control c in this.Parent.Controls)

{

If (c is UCRadioButton & & c! = this)

{

UCRadioButton uc = (UCRadioButton) c

If (_ groupName = = uc.GroupName & & uc.Checked)

{

Checked = false

Return

}

}

}

}

}

Let's take a look at the complete code

/ / copyright Huang Zhenghui Communication Group: 568015492 QQ:623128629

/ / File name: UCRadioButton.cs

/ / creation date: 2019-08-15 16:03:13

/ / function description: RadioButton

/ / Project address: https://gitee.com/kwwwvagaa/net_winform_custom_control

Using System

Using System.Collections.Generic

Using System.ComponentModel

Using System.Drawing

Using System.Data

Using System.Linq

Using System.Text

Using System.Windows.Forms

Namespace HZH_Controls.Controls

{

[DefaultEvent ("CheckedChangeEvent")]

Public partial class UCRadioButton: UserControl

{

[Description ("check change event"), Category ("Custom")]

Public event EventHandler CheckedChangeEvent

Private Font _ Font = new Font ("Microsoft Acer", 12)

[Description ("font"), Category ("custom")]

Public new Font Font

{

Get {return _ Font;}

Set

{

_ Font = value

Label1.Font = value

}

}

Private Color _ ForeColor = Color.FromArgb (62, 62, 62)

[Description ("font color"), Category ("custom")]

Public new Color ForeColor

{

Get {return _ ForeColor;}

Set

{

Label1.ForeColor = value

_ ForeColor = value

}

}

Private string _ Text = "radio button"

[Description ("text"), Category ("custom")]

Public string TextValue

{

Get {return _ Text;}

Set

{

Label1.Text = value

_ Text = value

}

}

Private bool _ checked = false

[Description ("selected"), Category ("Custom")]

Public bool Checked

{

Get

{

Return _ checked

}

Set

{

If (_ checked! = value)

{

_ checked = value

If (base.Enabled)

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton1

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton0

}

}

Else

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton10

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton00

}

}

SetCheck (value)

If (CheckedChangeEvent! = null)

{

CheckedChangeEvent (this, null)

}

}

}

}

Private string _ groupName

[Description ("Group name"), Category ("Custom")]

Public string GroupName

{

Get {return _ groupName;}

Set {_ groupName = value;}

}

Public new bool Enabled

{

Get

{

Return base.Enabled

}

Set

{

Base.Enabled = value

If (value)

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton1

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton0

}

}

Else

{

If (_ checked)

{

Panel1.BackgroundImage = Properties.Resources.radioButton10

}

Else

{

Panel1.BackgroundImage = Properties.Resources.radioButton00

}

}

}

}

Public UCRadioButton ()

{

InitializeComponent ()

}

Private void SetCheck (bool bln)

{

If (! bln)

Return

If (this.Parent! = null)

{

Foreach (Control c in this.Parent.Controls)

{

If (c is UCRadioButton & & c! = this)

{

UCRadioButton uc = (UCRadioButton) c

If (_ groupName = = uc.GroupName & & uc.Checked)

{

Uc.Checked = false

Return

}

}

}

}

}

Private void Radio_MouseDown (object sender, MouseEventArgs e)

{

This.Checked = true

}

Private void UCRadioButton_Load (object sender, EventArgs e)

{

If (this.Parent! = null & & this._checked)

{

Foreach (Control c in this.Parent.Controls)

{

If (c is UCRadioButton & & c! = this)

{

UCRadioButton uc = (UCRadioButton) c

If (_ groupName = = uc.GroupName & & uc.Checked)

{

Checked = false

Return

}

}

}

}

}

}

}

Namespace HZH_Controls.Controls

{

Partial class UCRadioButton

{

/ / /

/ / required designer variables.

/ / /

Private System.ComponentModel.IContainer components = null

/ / /

/ Clean up all resources in use.

/ / /

/ / if the managed resource should be released, true; otherwise false.

Protected override void Dispose (bool disposing)

{

If (disposing & & (components! = null))

{

Components.Dispose ()

}

Base.Dispose (disposing)

}

# Code generated by region component designer

/ / /

/ / the designer supports the required methods-do not

/ / use the code editor to modify the contents of this method.

/ / /

Private void InitializeComponent ()

{

This.label1 = new System.Windows.Forms.Label ()

This.panel1 = new System.Windows.Forms.Panel ()

This.SuspendLayout ()

/ /

/ / label1

/ /

This.label1.Dock = System.Windows.Forms.DockStyle.Fill

This.label1.Font = new System.Drawing.Font ("Microsoft Acer", 12F)

This.label1.ForeColor = System.Drawing.Color.FromArgb (int) ((byte) (62), ((int) ((byte) (62), ((int) ((byte) (62))

This.label1.Location = new System.Drawing.Point (18,0)

This.label1.Name = "label1"

This.label1.Padding = new System.Windows.Forms.Padding (5,0,0,0)

This.label1.Size = new System.Drawing.Size (215,30)

This.label1.TabIndex = 3

This.label1.Text = "radio button"

This.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft

This.label1.MouseDown + = new System.Windows.Forms.MouseEventHandler (this.Radio_MouseDown)

/ /

/ / panel1

/ /

This.panel1.BackgroundImage = global::HZH_Controls.Properties.Resources.radioButton0

This.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom

This.panel1.Dock = System.Windows.Forms.DockStyle.Left

This.panel1.Location = new System.Drawing.Point (0,0)

This.panel1.Name = "panel1"

This.panel1.Size = new System.Drawing.Size (18,30)

This.panel1.TabIndex = 2

This.panel1.MouseDown + = new System.Windows.Forms.MouseEventHandler (this.Radio_MouseDown)

/ /

/ / UCRadioButton

/ /

This.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None

This.Controls.Add (this.label1)

This.Controls.Add (this.panel1)

This.Name = "UCRadioButton"

This.Size = new System.Drawing.Size (233,30)

This.Load + = new System.EventHandler (this.UCRadioButton_Load)

This.MouseDown + = new System.Windows.Forms.MouseEventHandler (this.Radio_MouseDown)

This.ResumeLayout (false)

}

# endregion

Private System.Windows.Forms.Label label1

Private System.Windows.Forms.Panel panel1

}

}

The above is how to customize a checkbox with Winform in C#. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report