【问题标题】:ComboBox placeholder text (hint, cue banner, default text)为 ComboBox 设置提示或水印或默认文本而不将其添加为项目
【发布时间】:2018-11-30 20:40:44
【问题描述】:

所以,我试图在用户做出选择之前在ComboBox 上显示一些文本。

该文本不应是用户可选择的项目。 ComboBox 开启了 DropDownList 样式,因此用户无法对其进行编辑。

这是我目前所拥有的:

在负载上:

this.comboBox1.Items.AddRange(new object[] {"Caixilharia em PVC", "Caixilharia em Alumínio" });

在组合框上:

switch (comboBox1.SelectedIndex)
{
    case 0:
        Form2 newForm = new Form2();
        newForm.Show();
        this.Hide();
        break;

    case 1:
        Form3 anothernewForm = new Form3();
        anothernewForm.Show();
        this.Hide();
        break;
} 

现在,我想显示文本,如下图所示:

我不希望文本显示在这里,作为一个项目:

我设法做到这一点的唯一方法是,文本也显示为一个项目。

我会等待你的帮助。

【问题讨论】:

  • 只需设置 text 属性即可。
  • 我无法设置它,因为我正在使用并且我想继续在组合框上使用 DropDownList 样式。
  • 设置文字没用。对于DropDownList 模式,您应该绘制文本,对于其他模式,您应该向编辑区域发送CB_SETCUEBANNER 消息,就像我在this post 中所做的那样。

标签: c# .net winforms combobox


【解决方案1】:

您可以简单地将字符串设置为该组合框的Text property。这将为您提供所需的行为。请记住,您的代码必须考虑到此组合可能没有选择任何项目。您可以通过SelectedIndex property 进行检查,该值将是-1。

【讨论】:

  • 好答案,我还建议从 Microsoft 文档中正确添加指向文本的链接,以便他可以阅读。 msdn.microsoft.com/en-us/library/…
  • 完成。还为 SelectedIndex 属性添加。谢谢!
  • 但是我可以将 DropDownStyle 设置为 DropDownList 吗?
  • 不幸的是,当 DropDownStyle 为 SimpleDropDown 时,您可以在 ComboBox 中设置文本。请记住,如果用户编辑 ComboBox 的文本,SelectedIndex 也将是 -1。因此,您可以正确使用 DropDown 中的 DropDownStyle。另一种方法可能是在开头插入一个带有占位符文本的对象,用户点击 ComboBox 后,您可以将其删除。
【解决方案2】:

啊,下拉列表。通过只允许从项目列表中的项目设置它来忽略 .Text 属性。

但是。您可以通过处理项目的添加和删除来操纵该列表,以便在下拉列表中看不到它

    private void Form1_Load(object sender, EventArgs e)
    {
        //add an item to the combobox at the top
            comboBox1.Items.Insert(0, "Please select an item");
        //set the text
            comboBox1.Text = "Please select an item";
    }

    private void comboBox1_DropDown(object sender, EventArgs e)
    {
        //when we click to open the dropdown, we remove that item
        if (comboBox1.Items.Contains("Please select an item"))
            comboBox1.Items.RemoveAt(0);
    }

    private void comboBox1_DropDownClosed(object sender, EventArgs e)
    {
        //when we close the dropdown, if we select an item the dropdown
        //displays that item, if now we set back to our text.
        if (comboBox1.SelectedIndex == -1)
        {
            comboBox1.Items.Insert(0, "Please select an item");
            comboBox1.Text = "Please select an item";

        }

    }

并且根据您的代码是否会在没有调用 form_load 的情况下重置表单,如果您在某处将组合框的 selectedindex 设置回 -1,您可能需要在 SelectedIndexchanged 事件中进行覆盖,因为此代码不会涵盖该内容。

【讨论】:

    【解决方案3】:

    ComboBox 的提示、提示横幅、水印或默认文本

    您可以在不添加项目的情况下为ComboBox 设置提示。为此,如果ComboBox 具有DropDownSimple 模式,则可以向其内部编辑控件发送CB_SETCUEBANNER 消息以设置提示。如果combo有DropDownListMode,可以处理WM_PAINT或者设置为owner draw来绘制提示:

    下载

    您可以克隆或下载工作示例:

    代码

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    public class MyComboBox : ComboBox
    {
        public MyComboBox()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
        }
        const int CB_SETCUEBANNER = 0x1703;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
        string hint;
        public string Hint
        {
            get { return hint; }
            set { hint = value; UpdateHint(); }
        }
        private void UpdateHint()
        {
            if (!this.IsHandleCreated)
                return;
            if (DropDownStyle == ComboBoxStyle.DropDownList)
                this.Invalidate();
            else
                SendMessage(Handle, CB_SETCUEBANNER, 0, Hint);
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            if (!string.IsNullOrEmpty(Hint))
                UpdateHint();
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            string text = Hint;
            base.OnDrawItem(e);
            if (e.Index > -1)
                text = this.GetItemText(this.Items[e.Index]);
            e.DrawBackground();
            TextRenderer.DrawText(e.Graphics, text, Font,
                e.Bounds, e.ForeColor, TextFormatFlags.TextBoxControl);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2016-04-25
      • 1970-01-01
      • 2018-05-19
      相关资源
      最近更新 更多