【发布时间】:2019-07-11 10:48:16
【问题描述】:
我有一个场景,我需要根据 CheckBox 的状态禁用 ComboBox 的特定项目。
我在 ComboBox 中有五个 CheckBox 和五个 Item。
如果未选中 CheckBox1,则应禁用 Item1,这同样适用于所有其他 ComboBox 项。
我尝试使用此代码,但没有成功:
private void ComboBox_SelectNode_DrawItem(object sender, DrawItemEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
if (e.Index == 0)
{
if (this.isNode0Disabled)
{
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
if (e.Index == 1)
{
if (this.isNode1Disabled)
{
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
if (e.Index == 2)
{
if (this.isNode2Disabled)
{
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
if (e.Index == 3)
{
if (this.isNode3Disabled)
{
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
if (e.Index == 4)
{
if (this.isNode4Disabled)
{
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(this.ComboBox_SelectNode.Items[e.Index].ToString(), this.myFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
}
private void ComboBox_SelectNode_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ComboBox_SelectNode.SelectedIndex == 0)
{
if (this.isNode0Disabled)
{
this.ComboBox_SelectNode.SelectedIndex = -1;
}
}
if (this.ComboBox_SelectNode.SelectedIndex == 1)
{
if (this.isNode1Disabled)
{
this.ComboBox_SelectNode.SelectedIndex = -1;
}
}
if (this.ComboBox_SelectNode.SelectedIndex == 2)
{
if (this.isNode2Disabled)
{
this.ComboBox_SelectNode.SelectedIndex = -1;
}
}
if (this.ComboBox_SelectNode.SelectedIndex == 3)
{
if (this.isNode3Disabled)
{
this.ComboBox_SelectNode.SelectedIndex = -1;
}
}
if (this.ComboBox_SelectNode.SelectedIndex == 4)
{
if (this.isNode4Disabled)
{
this.ComboBox_SelectNode.SelectedIndex = -1;
}
}
}
private void CheckBox_Node0_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox_Node0.Checked == true)
{
this.isNode0Disabled = false;
}
if (this.checkBox_Node0.Checked == false)
{
this.isNode0Disabled = true;
}
}
修改后的代码:
private void ComboBox_SelectNode_DrawItem(object sender, DrawItemEventArgs e)
{
TextFormatFlags comboTRFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
if (e.Index < 0)
{
return;
}
var combo = sender as ComboBox;
bool isCheckBoxChecked = this.comboCheckBoxes[e.Index].Checked;
if (isCheckBoxChecked)
{
e.DrawBackground();
}
else
{
using (var brush = new SolidBrush(combo.BackColor))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
string itemText = combo.GetItemText(combo.Items[e.Index]);
Color textColor = isCheckBoxChecked ? e.ForeColor : SystemColors.GrayText;
TextRenderer.DrawText(e.Graphics, itemText, combo.Font, e.Bounds, textColor, comboTRFlags);
}
private void ComboBox_SelectNode_MeasureItem(object sender, MeasureItemEventArgs e)
=> e.ItemHeight = this.ComboBox_SelectNode.Font.Height + 4;
并将事件声明如下:
this.ComboBox_SelectNode.DrawItem += new DrawItemEventHandler(ComboBox_SelectNode_DrawItem);
this.ComboBox_SelectNode.MeasureItem += new MeasureItemEventHandler(ComboBox_SelectNode_MeasureItem);
private CheckBox[] comboCheckBoxes;
this.comboCheckBoxes = new[] {this.checkBox_Node0, this.checkBox_Node1, this.checkBox_Node2, this.checkBox_Node3 , this.checkBox_Node4};
但是当我运行应用程序时,这些事件没有触发。
【问题讨论】:
标签: c# winforms checkbox combobox