在字段中存储多项选择组框选择 [英] Store multi-choice groupbox selection in a field

查看:65
本文介绍了在字段中存储多项选择组框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个表来包含一个字段来存储一个必须

表示没有,一个或多个选择的值。表格将有一个分组框

带复选框(多选)


您看到此表中的记录包含部分预先配置

记录。这些记录充当主人。稍后将数据插入数据表中的数据。主数据由管理员设置。

管理员为每条记录指示适用于记录的无,一个或多个选项




稍后用户希望将预先配置的记录添加到他/她的数据表中的时间。根据项目类型,他/她将在无,一个或多个选项的组合框中进行选择。然后根据

选择他/她将被提供一个过滤表格,显示

可供选择的预配置记录。

以下是可用作

选项的数据的示例枚举。


厨房1

浴室2

卧室4

杂物间8

车库16


我知道还有其他方法可以实现这一目标,但这种方法似乎是最简单的。我认为我赞成这种方法的原因之一是

它不是那么架构密集型。


有没有理由不使用这种方法?


我打算使用一个包含复选框列表的组合框。当

用户进行选择时,表格会将

所选选项的值加在一起,以得出存储在记录中的值。


我知道这种方法没有任何原创性,但我有

问题......


·1,2是什么,4,8,16种枚举称为。我哈希表?

·如何从总和中解析单个值,以便我可以适当地检查或取消选中该字段的复选框?


和任何建议?

解决方案


< blockquote class =post_quotes>我知道这种方法没有任何原创性,但我有
问题...
·什么是1,2,4,8,16类枚举。哈希表?
·如何从总和中解析单个值,以便我可以适当地检查或取消选中该字段的复选框?




1.这种枚举称为Bitfield枚举。这是一个支持其成员按位组合的
。为了创建这样一个枚举,需要做两件事:

a)用Flags属性标记枚举。 <标志()>在VB.NET中。

b)将对应于2的幂(如你所做的)的值分配给

成员。


2.在值之间使用按位或运算。使用1,2,4,8,16,32等值的

的全部优势是任何

值的总和永远不会是等于任何其他会员。


例如:

--------------------- -------------------------

Flags()> Public Enum TypeOfGirlFriend

无= 1

漂亮= 2

可爱= 4

丰满= 8
结束枚举


Dim myGirl as TypeOfGirlFriend = 14


Msgbox(myGirl.ToString())''我可以告诉这里选择了

我枚举的最后3项。


------------------ ----------------------------


请注意,在您的示例中,它可能是明智的添加一个名为全部的成员

以下车库实际上是(厨房或浴室或卧室或

杂物间或车库)


HTH,


问候,


Cerebrus。


Cerebrus,


谢谢,

我有几个问题;


1.)在示例中,您给出了如何从14号中解析单独的值




美丽

可爱

妖娆





2

4

8





2.)我应该使用什么数据类型将其存储在数据库中?整数?

Byte?二进制文件?


3.).NET中是否有任何专门准备用于处理这些计算的
的类?

dbuchanan





查看Enum类的成员以获取更多信息......


1.首先,请注意我可以用不同的

方式设置myGirl变量:


Dim myGirl As TypeOfGirlFriend = TypeOfGirlFriend.Beautiful Or

TypeOfGirlFriend.Lovely Or TypeOfGirlFriend.Voluptuous


我假设您将存储在数据库中的内容将是总和

值(14)。现在,你的问题是如何从这个总数中解析

的值。


a)一种方法可以像使用ToString一样简单()方法:


Console.WriteLine(myGirl.ToString())''将返回

美丽,可爱,妖娆

然后可以根据需要对其进行解析。


b)其次,你可以使用Enum.Parse()方法:


Console.WriteLine([Enum] .Parse(GetType(TypeOfGirlFriend)," 12"))

''将返回Lovely,Voluptuous,这可以再次被解析。


2.关于数据类型,您可以使用任何您想要的数据类型。整数是

默认值,您可以使用它。数据库应该没有问题

识别int32。


3.>>是否有任何.NET专门准备

处理这类计算?




我不明白你的意思。什么类型的计算?


希望这些信息有所帮助,


问候,


Cerebrus。


I am designing a table to contain a field to stores a value that must
indicate none, one, or many choices. The form will have a group box
with checkboxes (multi-choice)

You see the records in this table contain partially pre-configured
records. The records serve as "master" data that will later be inserted
into a data table. The master data is set up by an adminsitrator. The
administrator indicates for each record the none, one or many options
that apply to the record.

At that later time a user will want to add pre-configured records to
his/her data table. Based on the kind of project he/she will make a
selection in a group box of none, one, or many options. Then based on
that choic he/she will be presented with a filtered table displaying
the choices of pre-configured records to choose from.

Here is sample enumeration of the data that might be used as the
options to choose from.

Kitchen 1
Bathroom 2
Bedroom 4
Utility room 8
Garage 16

I know there are other approaches to achieving this, but this approach
seems the simplest. Among the reasons I think I favor this approach is
that it is not so "schema intensive".

Are there any reasons not to use this approach?

I intend to use a group box containing a list of checkboxes. When the
user makes his selection the the form adds together the values for the
selected choices to arrive at the value to store in the record.

I know there is nothing original about this approach, but I have
questions...

· What is the 1,2,4,8,16 kind of enumeration called. I hash table?
· How do I parse individual values from the sum so that I can
appropriately check or uncheck the checkboxes for the field?

And any recommendations?

解决方案

Hi,

I know there is nothing original about this approach, but I have
questions... · What is the 1,2,4,8,16 kind of enumeration called. I hash table?
· How do I parse individual values from the sum so that I can
appropriately check or uncheck the checkboxes for the field?



1. This kind of enumeration is called a Bitfield enumeration. This is
one that supports a bitwise combination of its members. In order to
create such an enum, 2 things need to be done :
a) Mark the enum with a Flags attribute. <Flags()> in VB.NET.
b) Assign values corresponding to powers of 2 (as you did) to the
members.

2. Use Bitwise OR operation between the values. The whole advantage of
using values such as 1,2,4,8,16,32... is that the sum of any of the
values will never be equal to any other member.

For instance :
----------------------------------------------
Flags()> Public Enum TypeOfGirlFriend
None = 1
Beautiful = 2
Lovely = 4
Voluptuous = 8
End Enum

Dim myGirl as TypeOfGirlFriend = 14

Msgbox(myGirl.ToString()) '' I can tell here that the last 3 items in
my enum were chosen.

----------------------------------------------

Note that In your example, it might be wise to add a member named "All"
below Garage which in effect will be (Kitchen OR Bathroom OR Bedroom OR
Utility room OR Garage)

HTH,

Regards,

Cerebrus.


Cerebrus,

Thank you,

I have a few questions;

1.) In the example you gave how can I parse out the separate values
from the number 14?

Beautiful
Lovely
Voluptuous

or

2
4
8

?

2.) What datatype should I use to store this in the database? Integer?
Byte? Binary?

3.) Are there any classes in .NET that are specifically prepared to
handle these kind of calculations?
dbuchanan


Hi,

Check out the members of the Enum class for more information...

1. First, note that I could have set the myGirl variable in a different
way too :

Dim myGirl As TypeOfGirlFriend = TypeOfGirlFriend.Beautiful Or
TypeOfGirlFriend.Lovely Or TypeOfGirlFriend.Voluptuous

I assume that what you will store in your database will be the sum of
the values (14). Now, your question is how to parse the values that
have been set, from this total.

a) One way could be as simple as using the ToString() method :

Console.WriteLine(myGirl.ToString()) '' Will return
"Beautiful, Lovely, Voluptuous"
This can be then parsed as needed.

b) Secondly, you could use the Enum.Parse() method :

Console.WriteLine([Enum].Parse(GetType(TypeOfGirlFriend), "12"))
''Will return "Lovely, Voluptuous", which can again be parsed.

2. Regarding the datatype, you can use any one you want. Integer is the
default and you can go with that. The database should have no problem
recognizing an int32.

3. >> Are there any classes in .NET that are specifically prepared to

handle these kind of calculations?



I don''t understand what you mean. What type of calculations ?

Hope this information helps,

Regards,

Cerebrus.


这篇关于在字段中存储多项选择组框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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