0

So i've looked at a few stackoverflow posts and nothing seems to be solving my issue.

Tried: How to show text in combobox when no item selected?

And some others, can't find link now.

Application: http://puu.sh/5mQtX.png

So for the drop down menu at the bottom, I was trying to make the text display "Select Email Use" but whenever adding the text using the DropDownList in the DropDownStyle menu, the text disappears. But I want to make it so the user cannot just edit the text.

Don't have any code exactly for the program right now.

From the SOF post I linked above, I tried everything in that post to fix the issue but nothing exactly helps.

i am using Visual C# 2010 Windows Form Application

Community
  • 1
  • 1

2 Answers2

0

You can use Text Property of ComboBox Control to show Default Text

Try:

ComboBox1.Text="Select Email Use";

It will be shown ByDefault

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

I think you have to draw the string yourself, here is the working code for you, there is a small issue with the flicker, the string is a little flickering when the mouse is hovered on the combobox, even enabling the DoubleBuffered doesn't help, however it's acceptable I think:

public partial class Form1 : Form {
   public Form1(){
      InitializeComponent();
      comboBox1.HandleCreated += (s,e) => {
         new NativeComboBox{StaticText = "Select Email Use"}
                           .AssignHandle(comboBox1.Handle);
      };
   }
   public class NativeComboBox : NativeWindow {
        public string StaticText { get; set; }

        protected override void WndProc(ref Message m)
        {                                                
            base.WndProc(ref m);
            if (m.Msg == 0xf)//WM_PAINT = 0xf
            {                    
                var combo = Control.FromHandle(Handle) as ComboBox;
                if (combo != null && combo.SelectedIndex == -1)
                {
                    using (Graphics g = combo.CreateGraphics())
                    using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center })
                    using (Brush brush = new SolidBrush(combo.ForeColor))
                    {
                        g.DrawString(StaticText, combo.Font, brush, combo.ClientRectangle, sf);
                    }
                }                                         
            }                
        }
    }
}
King King
  • 61,710
  • 16
  • 105
  • 130