vb.net中的多种字体样式组合(Multiple Font Style Combinations in vb.net)

其他开发 IT屋
问 题

If I want to create a font with multiple style combinations, like bold AND underline, I have to place the 'or' statement between it, like in the example below:

  lblArt.Font = New Font("Tahoma", 18, FontStyle.Bold Or FontStyle.Underline)

If you place bold 'and' underline, it won't work, and you only get 1 of the 2 (like how the or statement should be working), while that would be the logically way to do it. What is the reason behind this?

解决方案

Boolean logic works a bit differently than the way we use the terms in English. What's happening here is that the enumerated FontStyle values are actually bit flags, and in order to manipulate bit flags, you use bitwise operations.

To combine two bit flags, you OR them together. An OR operation combines the two values. So imagine that FontStyle.Bold was 2 and FontStyle.Underline was 4. When you OR them together, you get 6—you've combined them together. In Boolean logic, you can think of an OR operation as returning "true" (i.e., setting that bit in the result) if either of the bits in the two operands are set, and "false" if neither of the bits in the two operands are set.

You can write a truth table for such an operation as follows:

| A | B | A OR B |
|---|---|--------|
| 0 | 0 |   0    |
| 0 | 1 |   1    |
| 1 | 0 |   1    |
| 1 | 1 |   1    |

Notice that the results more closely mirror what we, in informal English, would call "and". If either one has it set, then the result has it set, too.

In contrast to OR, a bitwise AND operation only returns "true" (i.e., sets that bit in the result) if both of the bits in the two operands are set. Otherwise, the result is "false". Again, a truth table can be written:

| A | B | A AND B |
|---|---|---------|
| 0 | 0 |    0    |
| 0 | 1 |    0    |
| 1 | 0 |    0    |
| 1 | 1 |    1    |

Assuming again that FontStyle.Bold has the value 2 and FontStyle.Underline has the value 4, if you AND them together, you get 0. This is because the values effectively cancel each other out. The net result is that you don't get any font styles—precisely why it doesn't work when you write FontStyle.Bold And FontStyle.Underline.

In VB, a bitwise OR operation is performed using the Or operator. The And operator performs a bitwise AND operation. So in order to do a bitwise inclusion of values, which is how you combine bit flags, you use the Or operator.

本文地址:IT屋 » Multiple Font Style Combinations in vb.net

问 题

如果我想创建一个带有多种样式组合的字体,比如粗体和下划线,我必须在它之间放置'或'语句,如下面的示例所示:



  lblArt.Font =新字体(“Tahoma”,18,FontStyle.Bold或FontStyle.Underline)


如果你放置粗体和下划线,它将不起作用,你只能得到2中的一个(比如说这个或者语句应该如何工作),而这将是在逻辑上做到这一点。这背后的原因是什么?


解决方案

布尔逻辑与我们在英语中使用的术语有所不同。这里发生的事情是,枚举的 FontStyle 值实际上是位标志,为了操作位标志,可以使用按位操作



两个位标志,你或他们在一起。 OR操作组合了这两个值。所以想象 FontStyle.Bold 是2, FontStyle.Underline 是4.当你把它们放在一起的时候,你把他们结合在一起在布尔逻辑中,如果两个操作数中的两个位中的任何一个都可以,则可以将一个OR操作视为返回“true”( ie ,在结果中设置该位)如果两个操作数中的位没有设置,那么设置为“false”。



可以为这样的操作写一个真值表如下所示:

  | A | B | A或B | 
| --- | --- | -------- |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |


请注意,结果更贴切地反映了我们在非正式英语中所称的“和”。如果任何一个设置了,那么结果也会被设置。



与OR不同的是,按位AND操作只返回“true”( ie ,在结果中设置该位),如果两个操作数中的两个位都被设置。否则,结果是“假”。再次,可以写一个真值表:

  | A | B | A AND B | 
| --- | --- | --------- |
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |


假设 FontStyle.Bold 值2和 FontStyle.Underline 的值为4,如果你和他们在一起,你得到0.这是因为这些值有效地相互抵消。最终的结果是,你没有得到任何字体样式 - 正因如此,当你编写 FontStyle.Bold和FontStyle.Underline 。

$时, b
$ b

在VB中,使用或运算符执行按位或运算。 和操作符执行按位与操作。所以为了做一个按位包含的值,就是如何组合位标志,可以使用或者运算符。


本文地址:IT屋 » vb.net中的多种字体样式组合