【问题标题】:Issue with ComboBox - DropDownStyles + Disable Text Edit组合框问题 - DropDownStyles + 禁用文本编辑
【发布时间】:2013-11-19 06:36:16
【问题描述】:

所以我查看了一些 stackoverflow 帖子,但似乎没有任何问题可以解决我的问题。

试过了: How to show text in combobox when no item selected?

还有一些其他的,现在找不到链接。

应用: http://puu.sh/5mQtX.png

因此,对于底部的下拉菜单,我试图使文本显示“选择电子邮件使用”,但每当使用 DropDownStyle 菜单中的 DropDownList 添加文本时,文本就会消失。但我想让它让用户不能只编辑文本。

目前没有该程序的任何代码。

从我上面链接的 SOF 帖子中,我尝试了该帖子中的所有方法来解决问题,但没有任何帮助。

我正在使用 Visual C# 2010 Windows 窗体应用程序

【问题讨论】:

  • 在表单加载中,通过硬编码comboBox1.Text = "Select Email Use"; 将文本写入comboBox1.Text
  • @ManojNaik 似乎也不起作用。

标签: c# winforms combobox


【解决方案1】:

您可以使用ComboBox控件的Text属性来显示默认文本

试试:

ComboBox1.Text="Select Email Use";

默认显示

【讨论】:

    【解决方案2】:

    我认为你必须自己绘制字符串,这是你的工作代码,闪烁有一个小问题,当鼠标悬停在组合框上时,字符串有点闪烁,甚至启用@987654321 @ 没有帮助,但我认为这是可以接受的:

    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);
                        }
                    }                                         
                }                
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 2012-01-09
      • 2018-01-08
      • 2011-02-14
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多