How to dynamically change / set checkedListBox flag of front color

I have the code below. How can I check the box for the checkedListBox element, depending on whether the element is checked or not?

private void FindSelectedUserRoles() { lblSelectedUser.Text = Code.CommonUtilities.getDgvStringColValue(dataGridViewUserList, "UserName").Trim(); //iterate all roles selected user is member of for (int i = 0; i < checkedListRoles.Items.Count; i++) { string roleName = checkedListRoles.Items[i].ToString(); string selectedUserRoles = Code.MemberShipManager.GetSpecificUsersRoles(lblSelectedUser.Text.Trim()); if (selectedUserRoles.Contains(roleName)) { checkedListRoles.SetItemChecked(i, true); //here i want to set item fore colour to green } else if (selectedUserRoles.Contains(roleName) == false) { checkedListRoles.SetItemChecked(i, false); //and here, i want item fore colour to remain black } } } 
+4
source share
3 answers

I think you need to draw your own CheckedListBox item as follows:

 public class CustomCheckedListBox : CheckedListBox { public CustomCheckedListBox() { DoubleBuffered = true; } protected override void OnDrawItem(DrawItemEventArgs e) { Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal); int dx = (e.Bounds.Height - checkSize.Width)/2; e.DrawBackground(); bool isChecked = GetItemChecked(e.Index);//For some reason e.State doesn't work so we have to do this instead. CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), isChecked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal); using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center }) { using (Brush brush = new SolidBrush(isChecked ? CheckedItemColor : ForeColor)) { e.Graphics.DrawString(Items[e.Index].ToString(), Font, brush, new Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height), sf); } } } Color checkedItemColor = Color.Green; public Color CheckedItemColor { get { return checkedItemColor; } set { checkedItemColor = value; Invalidate(); } } } 

If you want to set CheckedColor differently for each element, you need to save the CheckedColor parameter for each element (for example, in a collection) and specify CheckedColor using Index . However, I think this will work a little. Therefore, if you have such a requirement, it is better to choose a ListView .

+4
source

I think you should try ListView instead of checkedListBox. It has the necessary properties and can be customized as you wish. Just set the Checkboxes property to true , and then add forecolor in the code as follows:

 listView1.Items[i].ForeColor = Color.Red; 
+2
source

Since this is quite difficult to do yourself, you can let the original control draw itself - just adjust the color. This is my suggestion:

 public class CustomCheckedListBox : CheckedListBox { protected override void OnDrawItem(DrawItemEventArgs e) { Color foreColor; if (e.Index >= 0) { foreColor = GetItemChecked(e.Index) ? Color.Green : Color.Red; } else { foreColor = e.ForeColor; } // Copy the original event args, just tweaking the fore color. var tweakedEventArgs = new DrawItemEventArgs( e.Graphics, e.Font, e.Bounds, e.Index, e.State, foreColor, e.BackColor); // Call the original OnDrawItem, but supply the tweaked color. base.OnDrawItem(tweakedEventArgs); } } 
+1
source

Source: https://habr.com/ru/post/1490854/


All Articles