只读(但可放置)组合框

发布于 2024-10-27 00:01:04 字数 850 浏览 2 评论 0原文

我希望构建一个具有正常颜色的 Windows 窗体组合框,并允许显示下拉列表,但不允许实际更改值。据我所知,这不是 How to make Combobox in 的重复winforms readonly 因为所有建议似乎都是针对禁用组合框的交互性。

我的理由:我有一个表单,其中所有控件都是只读的,并且由于应用程序的性质,我认为当组合框的值不改变时,用户不会感到困惑。我希望用户能够看到组合框绑定的枚举的所有可能值。

到目前为止,我遇到的是一个非常糟糕的黑客:

public partial class ReadOnlyComboBox : ComboBox
{
    int prevIndex = -1;

    public ReadOnlyComboBox()
    {
        InitializeComponent();
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (prevIndex <= 0)
            prevIndex = SelectedIndex;
        else
            SelectedIndex = prevIndex;
    }
}

实际上,这忽略了框架中的虚假“0”值,并采用从绑定源获取的第一个非零值。直接的缺点是该值只能设置一次,并且绑定枚举必须从 1 开始。

欢迎任何有关清理此问题的建议。谢谢。

I'm looking to build a Windows Forms combo box that has normal coloration, and allows the dropdown list to appear, but does not allow the value to actually change. As far as I can tell, this is not a duplicate of How to make Combobox in winforms readonly since all advice there seems to be directed toward disabling the combo box's interactivity.

My rationale: I have a form where all of the controls are read-only, and due to the nature of the application I think that there would be no risk of the user getting confused when the combo box's value does not change. I would like the user to be able to see all of the possible values of the enum to which the combo box is bound.

What I have so far is a pretty bad hack:

public partial class ReadOnlyComboBox : ComboBox
{
    int prevIndex = -1;

    public ReadOnlyComboBox()
    {
        InitializeComponent();
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (prevIndex <= 0)
            prevIndex = SelectedIndex;
        else
            SelectedIndex = prevIndex;
    }
}

In effect, this ignores spurious "0" values from the framework, and takes the first non-zero value acquired from a binding source. Immediate disadvantages are that the value may only be set once, and that the bound enum must start at 1.

Any advice on cleaning this up would be welcome. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-11-03 00:01:04

使用 DropDownClosed 事件

public class ReadOnlyComboBox : ComboBox
{
    bool afterDropDown ;
    int prevIndex;

    public ReadOnlyComboBox()
    {
        this.SelectedIndexChanged+=new EventHandler(ReadOnlyComboBox_SelectedIndexChanged);
        this.DropDownClosed += new EventHandler(ReadOnlyComboBox_DropDownClosed);
    }

    void ReadOnlyComboBox_DropDownClosed(object sender, EventArgs e)
    {
        afterDropDown = true;
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (afterDropDown)
        {
            afterDropDown = false;
            SelectedIndex = prevIndex;
        }
        else
        {
            prevIndex = SelectedIndex;
        }
    }
}

Use DropDownClosed event

public class ReadOnlyComboBox : ComboBox
{
    bool afterDropDown ;
    int prevIndex;

    public ReadOnlyComboBox()
    {
        this.SelectedIndexChanged+=new EventHandler(ReadOnlyComboBox_SelectedIndexChanged);
        this.DropDownClosed += new EventHandler(ReadOnlyComboBox_DropDownClosed);
    }

    void ReadOnlyComboBox_DropDownClosed(object sender, EventArgs e)
    {
        afterDropDown = true;
    }

    private void ReadOnlyComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (afterDropDown)
        {
            afterDropDown = false;
            SelectedIndex = prevIndex;
        }
        else
        {
            prevIndex = SelectedIndex;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文