【问题标题】:Create ToolTip for ComboBox为组合框创建工具提示
【发布时间】:2018-05-11 05:44:49
【问题描述】:

由于我的屏幕设计无法扩展 ComboBox 的宽度,所以在 C# 表单应用程序中看不到数据的全名。

在进行选择之前,我想在 ComboBox 中显示它,如下所示。如何在组合框中执行此操作?

【问题讨论】:

  • 我想你想说的是工具提示
  • 所以你想要一个工具提示? Here 很好地解释了如何做到这一点
  • 我没有看到 ComboBox 只有一个 DataGridView。 - 还有:你知道ComboBox.DropDownWidth ?? (就像一个工具提示(这是你显示的)它会将下拉菜单扩展到表单边框之外)
  • 是的,一个选项,但几乎没有比简单地使内置的 DropDownwidth 更宽更好。另外,请更改误导性标题; MessageBox 必须始终靠近 ser;肯定不是你真正想要的..

标签: c# combobox


【解决方案1】:

我不知道是否有可能在突出显示更改时触发一个事件,但如果它足以在选择更改时进行更新,则应该这样做

public partial class Form1 : Form
{
    private ToolTip tt;

    public Form1()
    {
        InitializeComponent();

        this.comboBox1.Items.Add("item1");
        this.comboBox1.Items.Add("item2");
        this.comboBox1.Items.Add("item3");
        this.comboBox1.Items.Add("item4");
        this.comboBox1.Items.Add("item5");
        this.comboBox1.Items.Add("item6");

        this.comboBox1.MouseEnter += this.ComboBox1_MouseEnter;
        this.comboBox1.SelectedValueChanged += this.ComboBox1_SelectedValueChanged;

        this.tt = new ToolTip
        {
            AutoPopDelay = 0,
            InitialDelay = 0,
            ReshowDelay = 500,
            ShowAlways = true
        };

        this.tt.SetToolTip(this.comboBox1, "");
    }

    private void ComboBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        this.tt.SetToolTip(this.comboBox1, this.comboBox1.SelectedText);
    }

    private void ComboBox1_MouseEnter(object sender, EventArgs e)
    {
        this.tt.SetToolTip(this.comboBox1, this.comboBox1.SelectedText);
    }
}

编辑: 通过在运行时检查组合框,我认为您甚至无法获得突出显示的文本,也无法获得在其更改时触发的事件。此外,没有可直接访问的 subControls 或任何可能对您在问题中描述的行为有用的东西。我没有研究过使用反射的可能性,但大多数时候这些解决方案比实际解决方案更像是一个问题。实现该行为的另一种方法很可能是使用 WPF 或制作您自己的组合框控件。

【讨论】:

猜你喜欢
  • 2012-04-19
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多