Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why isn't my semi-editable ComboBox drawing the text in gray when the dropdown list isn't showing?

Why isn't my semi-editable ComboBox drawing the text in gray when the dropdown list isn't showing?

Scheduled Pinned Locked Moved C#
comgraphicsquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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

    OriginalGriffO P 2 Replies Last reply
    0
    • A arnold_w

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

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

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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!

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        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.

        R 1 Reply Last reply
        0
        • A arnold_w

          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.

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          are you sure that enableItem is really false when DrawItem is called ?

          A 1 Reply Last reply
          0
          • R Ralf Meier

            are you sure that enableItem is really false when DrawItem is called ?

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            I added Debug.WriteLine(enableItem ? "Enabled" : "Disabled"); inside comboBox_DrawItem and it always prints "Disabled".

            R 1 Reply Last reply
            0
            • A arnold_w

              I added Debug.WriteLine(enableItem ? "Enabled" : "Disabled"); inside comboBox_DrawItem and it always prints "Disabled".

              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #6

              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 ...

              1 Reply Last reply
              0
              • A arnold_w

                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

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                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 by ForeColor. It seems to me that the simple solution would be to change the ForeColor, 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;
                }

                Advanced TypeScript Programming Projects

                A 1 Reply Last reply
                0
                • P Pete OHanlon

                  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 by ForeColor. It seems to me that the simple solution would be to change the ForeColor, 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;
                  }

                  Advanced TypeScript Programming Projects

                  A Offline
                  A Offline
                  arnold_w
                  wrote on last edited by
                  #8

                  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;
                  
                  P 1 Reply Last reply
                  0
                  • A arnold_w

                    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;
                    
                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    I'm glad it's working now. Sorry I only just got the chance to look at this.

                    Advanced TypeScript Programming Projects

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups