Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of binding non-bool data CheckBox Control in Windows Form

Shulou Source: shulou.com Published: 2022-06-01 06:52:28 09月18日 Update

Today, I will talk to you about the example analysis of binding non-bool type data CheckBox controls in Windows Form. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The checked property of CheckBox cannot be directly bound to non-Boolean data, so the following extension is made.

When using it, you need to set two properties first

CheckedValue-the value represented when selected

UnCheckedValue-the value represented if it is not selected

For example

CheckedValue = "male"

UnCheckedValue = "female"

Then bind the data to the bindText property

For example:

EnjoyCheckBox1.DataBindings.Add ("BindText", ds, "cname")

Source code:

Using System

Using System.Collections

Using System.ComponentModel

Using System.Drawing

Using System.Data

Using System.Windows.Forms

Namespace Enjoy.Interface.Control

{

/ / /

/ / A summary description of the EnjoyCheckBox.

/ / /

Public class EnjoyCheckBox: System.Windows.Forms.CheckBox

{

/ / /

/ / required designer variables.

/ / /

Private System.ComponentModel.Container components = null

Public EnjoyCheckBox ()

{

/ / this call is required by the Windows.Forms forms designer.

InitializeComponent ()

/ / TODO: add any initialization after the InitializeComponent call

}

/ / /

/ Clean up all resources in use.

/ / /

Protected override void Dispose (bool disposing)

{

If (disposing)

{

If (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

/ / modify the contents of this method.

/ / /

Private void InitializeComponent ()

{

Components = new System.ComponentModel.Container ()

}

# endregion

Private object m_BindText

Private object m_CheckedValue

Private object m_UnCheckedValue

/ / /

/ / bind properties of the database

/ / /

Public object BindText

{

Get

{

Return m_BindText

}

Set

{

M_BindText = value

If ((masked BindTextents null) & & (CheckedValueVerification null) & & (UnCheckedValueVerification null)

{

If (m_BindText.Equals (CheckedValue))

{

If (! this.Checked)

{

This.Checked = true

}

}

If (m_BindText.Equals (UnCheckedValue))

{

If (this.Checked)

{

This.Checked = false

}

}

}

}

}

/ / /

/ / the value (in the database) represented when selected

/ / /

Public object CheckedValue

{

Get

{

Return m_CheckedValue

}

Set

{

M_CheckedValue = value

}

}

/ / /

/ / the value (in the database) represented if it is not selected

/ / /

Public object UnCheckedValue

{

Get

{

Return m_UnCheckedValue

}

Set

{

M_UnCheckedValue = value

}

}

Protected override void OnCheckedChanged (EventArgs e)

{

Base.OnCheckedChanged (e)

If (this.Checked)

{

BindText = CheckedValue

}

If (! this.Checked)

{

BindText = UnCheckedValue

}

}

}

}

After reading the above, do you have any further understanding of the example analysis of binding a non-bool type data CheckBox control in Windows Form? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Data representation content properties time design database control example type analysis code support two variables summary method time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Apple MySQL Docker