Disable CheckedListBox Items
-
Wednesday, February 28, 2007 2:13 AM
i'm using vs 2003 and vb.net to development a window application. i have a checkboxlist that contains few items. i need to disable some of the items, meaning the checked could not be modified after being saved into the database.
the purpose of doing this because i want to show user the item they redeemed.
please tell me how to do that.
thanks for any help.
All Replies
-
Wednesday, February 28, 2007 3:05 AM
Hi,engloon
your question has been asked before,please refer this thread
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=173121&SiteID=1
I think a better solution would be to not include these items in the list.
Hope this helps. -
Wednesday, February 28, 2007 7:40 AMhi Gavin.
i saw that thread before, but i was still trying to make it happen.
i've decided to use 2 different checkedlistboxes to display enabled and disabled items.
thanks for your advise. -
Wednesday, February 28, 2007 9:55 AM
In fact, the checkListBox is kind of ListBox, so make a ListBox to work as a CheckListBox, take my example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Sample2
{
public partial class CheckListBoxTest : Form
{
public CheckListBoxTest()
{
InitializeComponent();
}
private void CheckListBoxTest_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("status",typeof(bool));
for (int j = 0; j < 20; j++)
{
dt.Rows.Add("checkbox"+j.ToString(), j%2);
}
CheckBox[] cks = new CheckBox[dt.Rows.Count];
for (int j = 0; j < dt.Rows.Count; j++)
{
cks[j] = new CheckBox();
cks[j].Text = "";
cks[j].Checked = (bool)dt.Rows[j]["status"];
cks[j].Enabled = !cks[j].Checked;
cks[j].Height = this.listBox1.ItemHeight;
cks[j].Width = 15;
cks[j].Location = new Point(0, this.listBox1.ItemHeight * j);
this.listBox1.Controls.Add(cks[j]);
this.listBox1.Items.Add(dt.Rows[j]["name"].ToString());
}
this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Rectangle contentRect = e.Bounds;
contentRect.X = 16;
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(),
e.Font,
new SolidBrush(e.ForeColor),
contentRect);
}
}
}
-
Wednesday, February 28, 2007 10:31 AM
For those who use VB.NET, I translate my sample above to VB.NET for you:
Public Class CheckListBoxTest
Inherits Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Private Sub CheckListBoxTest_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable
dt.Columns.Add("name")
dt.Columns.Add("status", GetType(System.Boolean))
Dim j As Integer = 0
Do While (j < 20)
dt.Rows.Add(("checkbox" + j.ToString), (j Mod 2))
j = (j + 1)
Loop
j = 0
Dim n = dt.Rows.Count
Dim cks(n) As CheckBox
Do While (j < dt.Rows.Count)
cks(j) = New CheckBox
cks(j).Text = ""
cks(j).Checked = CType(dt.Rows(j)("status"), Boolean)
cks(j).Enabled = Not cks(j).Checked
cks(j).Height = Me.ListBox1.ItemHeight
cks(j).Width = 15
cks(j).Location = New Point(0, (Me.ListBox1.ItemHeight * j))
Me.ListBox1.Controls.Add(cks(j))
Me.ListBox1.Items.Add(dt.Rows(j)("name"))
j = (j + 1)
Loop
Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
AddHandler ListBox1.DrawItem, AddressOf Me.listBox1_DrawItem
End Sub
Private Sub listBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
e.DrawBackground()
Dim contentRect As Rectangle = e.Bounds
contentRect.X = 16
e.Graphics.DrawString(Me.listBox1.Items(e.Index).ToString, e.Font, New SolidBrush(e.ForeColor), contentRect)
End Sub
End Class
-
Wednesday, November 26, 2008 10:32 AMIn current example you can use specific wrapper - CheckedListBoxItem, which has Enabled property.
Visual Studio 2008
.net framework 2.0
C#
/// <summary>
/// CheckedListBox control with enabling.
/// </summary>
public class CheckedListBoxEx : CheckedListBox
{
/// <summary>
/// CheckedListBoxItem item.
/// </summary>
public class CheckedListBoxItem
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="text"></param>
public CheckedListBoxItem(string text)
: this(text, true)
{ }
/// <summary>
/// Constructor
/// </summary>
/// <param name="text"></param>
/// <param name="enabled"></param>
public CheckedListBoxItem(string text, bool enabled)
{
this.Text = text;
this.Enabled = enabled;
}
/// <summary>
/// Text
/// </summary>
public string Text
{
get;
private set;
}
/// <summary>
/// Enabling
/// </summary>
public bool Enabled
{
get;
private set;
}
/// <summary>
/// To string.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.Text;
}
}
/// <summary>
/// On drawn item override.
/// </summary>
/// <param name="e"></param>
protected override void OnDrawItem(DrawItemEventArgs e)
{
CheckedListBoxItem item = null;
if (e.Index >= 0 && e.Index < this.Items.Count)
item = this.Items[e.Index] as CheckedListBoxItem;
DrawItemEventArgs ne = e;
if (item != null && !item.Enabled)
{
ne = new DrawItemEventArgs(
e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State,
System.Drawing.SystemColors.InactiveCaptionText,
e.BackColor);
}
base.OnDrawItem(ne);
}
/// <summary>
/// On Item Check override.
/// </summary>
/// <param name="ice"></param>
protected override void OnItemCheck(ItemCheckEventArgs ice)
{
CheckedListBoxItem item = this.Items[ice.Index] as CheckedListBoxItem;
if (item != null && !item.Enabled)
ice.NewValue = ice.CurrentValue;
base.OnItemCheck(ice);
}
} -
Wednesday, July 29, 2009 9:17 AM
Hi Stepan Chu,
Even i am looking to enable/disable a particular CheckedListBox item in a given CheckedListBox control.
I saw the wrapper class implementation. Please share some working sample based on this wrapper class implemenation.
regards,
Karvee -
Thursday, November 05, 2009 2:27 AM
Stepan,
Don't know if anyone has come up with a better solution, but your CheckedListBoxEx worked nicely for me. Took all of 3 minutes to incorporate into my project. Would be nice to show the checkbox itself dimmed, but the behavior of your class fits the bill. Thanks for sharing this.
-kirk


