我该如何禁用组合框项目 [英] how can i disable combo-box items

查看:59
本文介绍了我该如何禁用组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用已保存在数据库中的下拉项目

我有两个表。

第一个表有名称列,其中包含一些值,第二个表也有有名称列和两个名称列连接在一起作为主键和外键。那么如何禁用组合框中的项目(从第一个表名列绑定)已保存在第二个表中?

How can I disable a dropdown item which is already saved in database
I have two tables.
first table has name column which contains some value and second table also has name column And both name columns are joined together as primary key and foreign key.So how can I disable items in a combobox (which are bound from first table name column) that are already saved in second table?

推荐答案

你可以尝试这个链接我希望你根据自己的问题充分利用这个 c#c​​ombobox selectedindexchanged events [ ^ ]
you can try this link i hope very use full for you according your problem try this c# combobox selectedindexchanged events[^]


如果结果不是关于Wi的问题nForms,我将删除这篇文章。



这个响应基于这样的假设:这里的问题是控制UI的运行时行为。



提供了两个想法:第一个保留完整的ComboBox项目集;第二个要求ComboBox项目与底层数据库源的任何绑定都是单向的,并允许修改ComboBox的项目。



WinForms ComboBox是一个非常好的愚蠢的野兽!它的Items是字符串的集合。你不能选择一些项目并设置他们的BackColor;你不能禁用单个项目。



要在Win Forms ComboBox中做出某些选择,我们会想到两种策略:



1.快速而肮脏......但其优点是整套ComboBox项目仍在那里。



a。定义/编辑您要排除的项目列表
If this turn out not to be a question about WinForms, I will delete this post.

This response is based on the assumption that the problem here is control of the run-time behavior of the UI.

Two ideas are offered: the first preserves the complete set of ComboBox Items; the second requires that any binding of the ComboBox Items to an underlying DB source is one-way, and permits modification of the ComboBox's Items.

The WinForms ComboBox is a very stupid beast ! Its Items are a collection of strings. You can't choose some Items and set their BackColor; you can't "disable" individual Items.

To make certain choices in a Win Forms ComboBox unavailable, there are two strategies come to mind:

1. quick and dirty ... but has the advantage that the complete set of ComboBox Items is "still there."

a. define/edit a List of Items you wish to exclude
List<string> ComboItemsExclude = new List<string>();

b。在代码中的某个地方粘贴了一些出现在ComboBox项目中的字符串:

b. somewhere in code stick some strings in it which appear in the ComboBox's Items:

ComboItemsExclude.AddRange(new List<string> {"Choice1", "Choice2"});

c。在ComboBox SelectedIndexChanged EventHandler中:

c. in the ComboBox SelectedIndexChanged EventHandler:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ComboItemsExclude.Contains(comboBox1.SelectedItem))
    {
        comboBox1.SelectedIndex = -1;
        return;
    }

    // code to handle valid ComboBox selection here
}

2。实际上隐藏不可用的项目(更多工作)。并且,此解决方案要求您可以更改ComboBox项目,而不会对DataBase产生任何副作用以及与数据库的绑定。



a。你有'RemoveAt'和'插入,ComboBox项目集合的方法。



b。您需要定义数据库交给您的所有可能的ComboBox项的副本,您可以在需要时将其还原。或者,也许刷新到原始状态可以通过DB上的一些刷新来完成?



c。根据需要:删除要隐藏的项目,并在必要时恢复它们。

2. Actually hide the Items that are not available (more work). And, this solution requires that you can change the ComboBox Items without any side-effects on the DataBase and the binding to the DB.

a. You do have 'RemoveAt, and 'Insert, methods for the ComboBox Items collection.

b. You need to define a copy of of all possible ComboBox Items "handed you" by the DB, which you can restore when you need to. Or, perhaps that refresh to original state can be done by some refresh on the DB ?

c. as you need to: remove Items you want to hide, and restore them when necessary.


这篇关于我该如何禁用组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆