Why isn't my semi-editable ComboBox drawing the text in gray when the dropdown list isn't showing?
-
I work with many Com-ports and to make them easier to keep track of I've created a combobox that has a fixed prefix (the information from the Device Manager, e.g. "STMicroelectronics STLink Virtual COM Port (COM21)") and an editable suffix where I can type in e.g. "My Product A". The code works great, but now I would like to also be able to disable (make the text gray) items in my combobox and it works fine in the dropdown list, but as soon as I close the dropdown list and only the selected item is shown, then the text is always enabled (black, not gray). Does anybody know what I should correct in my code to get the desired behavior?
partial class SemiEditableComboBox
{
/// /// Required designer variable.
///
private System.ComponentModel.IContainer components = null;/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.comboBox = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox // this.comboBox.FormattingEnabled = true; this.comboBox.Location = new System.Drawing.Point(0, 0); this.comboBox.Name = "comboBox"; this.comboBox.Size = new System.Drawing.Size(454, 24); this.comboBox.TabIndex = 0; // // SemiEditableComboBox // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.comboBox); this.Name = "SemiEditableComboBox"; this.Size = new System.Drawing.Size(454, 24); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox;
}
public delegate void DelegateItemAndString(object item, string newString);
class SemiEditableComboBoxItem
{
public string lastString;
public string fixedPrefix;
publi -
I work with many Com-ports and to make them easier to keep track of I've created a combobox that has a fixed prefix (the information from the Device Manager, e.g. "STMicroelectronics STLink Virtual COM Port (COM21)") and an editable suffix where I can type in e.g. "My Product A". The code works great, but now I would like to also be able to disable (make the text gray) items in my combobox and it works fine in the dropdown list, but as soon as I close the dropdown list and only the selected item is shown, then the text is always enabled (black, not gray). Does anybody know what I should correct in my code to get the desired behavior?
partial class SemiEditableComboBox
{
/// /// Required designer variable.
///
private System.ComponentModel.IContainer components = null;/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.comboBox = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox // this.comboBox.FormattingEnabled = true; this.comboBox.Location = new System.Drawing.Point(0, 0); this.comboBox.Name = "comboBox"; this.comboBox.Size = new System.Drawing.Size(454, 24); this.comboBox.TabIndex = 0; // // SemiEditableComboBox // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.comboBox); this.Name = "SemiEditableComboBox"; this.Size = new System.Drawing.Size(454, 24); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox;
}
public delegate void DelegateItemAndString(object item, string newString);
class SemiEditableComboBoxItem
{
public string lastString;
public string fixedPrefix;
publiYou manually draw the items yourself in the
comboBox_DrawItem
event handler - and that means you select the Brush in which they are drawn. In order to "grey out" selections, you'd need to change the enable code you have there."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
You manually draw the items yourself in the
comboBox_DrawItem
event handler - and that means you select the Brush in which they are drawn. In order to "grey out" selections, you'd need to change the enable code you have there."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I'm not sure I understand what you mean. Even if I do a global replace Color.Black -> Color.Gray then the selected item still has black color, not gray, when the dropdown list isn't shown.
are you sure that enableItem is really false when DrawItem is called ?
-
are you sure that enableItem is really false when DrawItem is called ?
-
I added Debug.WriteLine(enableItem ? "Enabled" : "Disabled"); inside comboBox_DrawItem and it always prints "Disabled".
another try : I would assign the color depending on the enableItem outside the DrawString-call. Use this variable for DrawString and also for Debug.WriteLine. In the Moment I can't see a mistake ...
-
I work with many Com-ports and to make them easier to keep track of I've created a combobox that has a fixed prefix (the information from the Device Manager, e.g. "STMicroelectronics STLink Virtual COM Port (COM21)") and an editable suffix where I can type in e.g. "My Product A". The code works great, but now I would like to also be able to disable (make the text gray) items in my combobox and it works fine in the dropdown list, but as soon as I close the dropdown list and only the selected item is shown, then the text is always enabled (black, not gray). Does anybody know what I should correct in my code to get the desired behavior?
partial class SemiEditableComboBox
{
/// /// Required designer variable.
///
private System.ComponentModel.IContainer components = null;/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.comboBox = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox // this.comboBox.FormattingEnabled = true; this.comboBox.Location = new System.Drawing.Point(0, 0); this.comboBox.Name = "comboBox"; this.comboBox.Size = new System.Drawing.Size(454, 24); this.comboBox.TabIndex = 0; // // SemiEditableComboBox // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.comboBox); this.Name = "SemiEditableComboBox"; this.Size = new System.Drawing.Size(454, 24); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox;
}
public delegate void DelegateItemAndString(object item, string newString);
class SemiEditableComboBoxItem
{
public string lastString;
public string fixedPrefix;
publiSo, the
DrawItem
event is used to draw the items that appear in the dropdown list and not the actual selected value. The colour of the selected text is set byForeColor
. It seems to me that the simple solution would be to change theForeColor
, depending on whether the selection was enabled or not. Something like this:private void comboBox_TextChanged(object sender, EventArgs e)
{
bool enableItem = (SelectedIndex < 0) ? false : comboBox.Items[SelectedIndex]).enabled;
comboBox.ForeColor = enableItem ? Color.Black : Color.Gray;
} -
So, the
DrawItem
event is used to draw the items that appear in the dropdown list and not the actual selected value. The colour of the selected text is set byForeColor
. It seems to me that the simple solution would be to change theForeColor
, depending on whether the selection was enabled or not. Something like this:private void comboBox_TextChanged(object sender, EventArgs e)
{
bool enableItem = (SelectedIndex < 0) ? false : comboBox.Items[SelectedIndex]).enabled;
comboBox.ForeColor = enableItem ? Color.Black : Color.Gray;
}Yes, that was it!!! Thank you. The beginning of my comboBox_TextChanged method now looks like this:
private void comboBox\_TextChanged(object sender, EventArgs e) { int selectedIndex = comboBox.SelectedIndex; // When we edit the currently shown text, then comboBox.SelectedIndex becomes -1! if (0 <= selectedIndex) { currentlySelectedIndex = selectedIndex; } bool enableItem = (selectedIndex < 0) ? false : items\[selectedIndex\].enabled; comboBox.ForeColor = enableItem ? Color.Black : Color.Gray;
-
Yes, that was it!!! Thank you. The beginning of my comboBox_TextChanged method now looks like this:
private void comboBox\_TextChanged(object sender, EventArgs e) { int selectedIndex = comboBox.SelectedIndex; // When we edit the currently shown text, then comboBox.SelectedIndex becomes -1! if (0 <= selectedIndex) { currentlySelectedIndex = selectedIndex; } bool enableItem = (selectedIndex < 0) ? false : items\[selectedIndex\].enabled; comboBox.ForeColor = enableItem ? Color.Black : Color.Gray;
I'm glad it's working now. Sorry I only just got the chance to look at this.