Unit 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示长度度量。
public value class Unit
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
public struct Unit
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
[System.Serializable]
public struct Unit
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))>]
type Unit = struct
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))>]
[<System.Serializable>]
type Unit = struct
Public Structure Unit
- 继承
- 属性
示例
下面的代码示例演示如何使用对象 Style 一次性更改多个控件的样式属性。 每次某个属性值发生更改时 Style ,每个控件都必须调用其 ApplyStyle 方法。 请注意,并非所有控件都支持演示的所有属性。 如果控件不支持特定属性,则更改属性值时控件的外观不会更改。
<!-- -->
<!-- -->
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private Style primaryStyle = new Style();
void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
// Add data to the borderColorList,
// backColorList, and foreColorList controls.
ListItemCollection colors = new ListItemCollection();
colors.Add(Color.Black.Name);
colors.Add(Color.Blue.Name);
colors.Add(Color.Green.Name);
colors.Add(Color.Orange.Name);
colors.Add(Color.Purple.Name);
colors.Add(Color.Red.Name);
colors.Add(Color.White.Name);
colors.Add(Color.Yellow.Name);
borderColorList.DataSource = colors;
borderColorList.DataBind();
backColorList.DataSource = colors;
backColorList.DataBind();
foreColorList.DataSource = colors;
foreColorList.DataBind();
//<Snippet4>
// Add data to the borderStyleList control.
ListItemCollection styles = new ListItemCollection();
Type styleType = typeof(BorderStyle);
foreach (string s in Enum.GetNames(styleType))
{
styles.Add(s);
}
borderStyleList.DataSource = styles;
borderStyleList.DataBind();
//</Snippet4>
// Add data to the borderWidthList control.
ListItemCollection widths = new ListItemCollection();
for (int i = 0; i < 11; i++)
{
widths.Add(i.ToString() + "px");
}
borderWidthList.DataSource = widths;
borderWidthList.DataBind();
// Add data to the fontNameList control.
ListItemCollection names = new ListItemCollection();
names.Add("Arial");
names.Add("Courier");
names.Add("Garamond");
names.Add("Times New Roman");
names.Add("Verdana");
fontNameList.DataSource = names;
fontNameList.DataBind();
// Add data to the fontSizeList control.
ListItemCollection fontSizes = new ListItemCollection();
fontSizes.Add("Small");
fontSizes.Add("Medium");
fontSizes.Add("Large");
fontSizes.Add("10pt");
fontSizes.Add("14pt");
fontSizes.Add("20pt");
fontSizeList.DataSource = fontSizes;
fontSizeList.DataBind();
//Set primaryStyle as the style for each control.
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
}
//<Snippet5>
void ChangeBorderColor(object sender, System.EventArgs e)
{
primaryStyle.BorderColor =
Color.FromName(borderColorList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet5>
//<Snippet6>
void ChangeBackColor(object sender, System.EventArgs e)
{
primaryStyle.BackColor =
Color.FromName(backColorList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet6>
//<Snippet7>
void ChangeForeColor(object sender, System.EventArgs e)
{
primaryStyle.ForeColor =
Color.FromName(foreColorList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet7>
//<Snippet8>
void ChangeBorderStyle(object sender, System.EventArgs e)
{
primaryStyle.BorderStyle =
(BorderStyle)Enum.Parse(typeof(BorderStyle),
borderStyleList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet8>
//<Snippet9>
void ChangeBorderWidth(object sender, System.EventArgs e)
{
primaryStyle.BorderWidth =
Unit.Parse(borderWidthList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet9>
//<Snippet10>
void ChangeFont(object sender, System.EventArgs e)
{
primaryStyle.Font.Name =
fontNameList.SelectedItem.Text;
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet10>
//<Snippet11>
void ChangeFontSize(object sender, System.EventArgs e)
{
primaryStyle.Font.Size =
FontUnit.Parse(fontSizeList.SelectedItem.Text);
Label1.ApplyStyle(primaryStyle);
ListBox1.ApplyStyle(primaryStyle);
Button1.ApplyStyle(primaryStyle);
Table1.ApplyStyle(primaryStyle);
TextBox1.ApplyStyle(primaryStyle);
}
//</Snippet11>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="6" border="0">
<tr>
<td rowspan="10" style="border:solid 1px Gray">
<p>
<asp:label id="Label1"
Text="Border Properties Example" Runat="server">
Label Styles
</asp:label>
</p>
<p>
<asp:button id="Button1" runat="server"
Text="Button Styles">
</asp:button>
</p>
<p>
<asp:listbox id="ListBox1" Runat="server">
<asp:ListItem Value="0" Text="List Item 0">
</asp:ListItem>
<asp:ListItem Value="1" Text="List Item 1">
</asp:ListItem>
<asp:ListItem Value="2" Text="List Item 2">
</asp:ListItem>
</asp:listbox>
</p>
<p>
<asp:textbox id="TextBox1"
Text="TextBox Styles" Runat="server">
</asp:textbox>
</p>
<p>
<asp:table id="Table1" Runat="server">
<asp:TableRow>
<asp:TableCell Text="(0,0)"></asp:TableCell>
<asp:TableCell Text="(0,1)"></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="(1,0)"></asp:TableCell>
<asp:TableCell Text="(1,1)"></asp:TableCell>
</asp:TableRow>
</asp:table>
</p>
</td>
<td align="right">
<asp:Label ID="Label2" runat="server"
AssociatedControlID="borderColorList"
Text="Border Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label3" Runat="server"
AssociatedControlID="borderStyleList"
Text="Border Style:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderStyleList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderStyle">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label4" Runat="server"
AssociatedControlID="borderWidthList"
Text="Border Width">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderWidthList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderWidth">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label5" Runat="server"
AssociatedControlID="backColorList"
Text="Back Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="backColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBackColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label6" Runat="server"
AssociatedControlID="foreColorList"
Text="Foreground Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="foreColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeForeColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label7" Runat="server"
AssociatedControlID="fontNameList"
Text="Font Name:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="fontNameList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeFont">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label8" Runat="server"
AssociatedControlID="fontSizeList"
Text="Font Size:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="fontSizeList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeFontSize">
</asp:dropdownlist>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private primaryStyle As New Style()
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
' Add data to the borderColorList,
' backColorList, and foreColorList controls.
Dim colors As New ListItemCollection()
colors.Add(Color.Black.Name)
colors.Add(Color.Blue.Name)
colors.Add(Color.Green.Name)
colors.Add(Color.Orange.Name)
colors.Add(Color.Purple.Name)
colors.Add(Color.Red.Name)
colors.Add(Color.White.Name)
colors.Add(Color.Yellow.Name)
borderColorList.DataSource = colors
borderColorList.DataBind()
backColorList.DataSource = colors
backColorList.DataBind()
foreColorList.DataSource = colors
foreColorList.DataBind()
'<Snippet4>
' Add data to the borderStyleList control.
Dim styles As New ListItemCollection()
Dim styleType As Type = GetType(BorderStyle)
Dim s As String
For Each s In [Enum].GetNames(styleType)
styles.Add(s)
Next s
borderStyleList.DataSource = styles
borderStyleList.DataBind()
'</Snippet4>
' Add data to the borderWidthList control.
Dim widths As New ListItemCollection()
Dim i As Integer
For i = 0 To 10
widths.Add(i.ToString() & "px")
Next i
borderWidthList.DataSource = widths
borderWidthList.DataBind()
' Add data to the fontNameList control.
Dim names As New ListItemCollection()
names.Add("Arial")
names.Add("Courier")
names.Add("Garamond")
names.Add("Times New Roman")
names.Add("Verdana")
fontNameList.DataSource = names
fontNameList.DataBind()
' Add data to the fontSizeList control.
Dim fontSizes As New ListItemCollection()
fontSizes.Add("Small")
fontSizes.Add("Medium")
fontSizes.Add("Large")
fontSizes.Add("10pt")
fontSizes.Add("14pt")
fontSizes.Add("20pt")
fontSizeList.DataSource = fontSizes
fontSizeList.DataBind()
' Set primaryStyle as the style for each control.
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End If
End Sub
'<Snippet5>
Sub ChangeBorderColor(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.BorderColor = _
Color.FromName(borderColorList.SelectedItem.Text)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet5>
'<Snippet6>
Sub ChangeBackColor(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.BackColor = _
Color.FromName(backColorList.SelectedItem.Text)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet6>
'<Snippet7>
Sub ChangeForeColor(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.ForeColor = _
Color.FromName(foreColorList.SelectedItem.Text)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet7>
'<Snippet8>
Sub ChangeBorderStyle(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.BorderStyle = _
CType([Enum].Parse(GetType(BorderStyle), _
borderStyleList.SelectedItem.Text), BorderStyle)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet8>
'<Snippet9>
Sub ChangeBorderWidth(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.BorderWidth = _
Unit.Parse(borderWidthList.SelectedItem.Text)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet9>
'<Snippet10>
Sub ChangeFont(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.Font.Name = _
fontNameList.SelectedItem.Text
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet10>
'<Snippet11>
Sub ChangeFontSize(ByVal sender As Object, ByVal e As System.EventArgs)
primaryStyle.Font.Size = _
FontUnit.Parse(fontSizeList.SelectedItem.Text)
Label1.ApplyStyle(primaryStyle)
ListBox1.ApplyStyle(primaryStyle)
Button1.ApplyStyle(primaryStyle)
Table1.ApplyStyle(primaryStyle)
TextBox1.ApplyStyle(primaryStyle)
End Sub
'</Snippet11>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Applied Style Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="6" border="0">
<tr>
<td rowspan="10" style="border:solid 1px Gray">
<p>
<asp:label id="Label1"
Text="Border Properties Example" Runat="server">
Label Styles
</asp:label>
</p>
<p>
<asp:button id="Button1" runat="server"
Text="Button Styles">
</asp:button>
</p>
<p>
<asp:listbox id="ListBox1" Runat="server">
<asp:ListItem Value="0" Text="List Item 0">
</asp:ListItem>
<asp:ListItem Value="1" Text="List Item 1">
</asp:ListItem>
<asp:ListItem Value="2" Text="List Item 2">
</asp:ListItem>
</asp:listbox>
</p>
<p>
<asp:textbox id="TextBox1"
Text="TextBox Styles" Runat="server">
</asp:textbox>
</p>
<p>
<asp:table id="Table1" Runat="server">
<asp:TableRow>
<asp:TableCell Text="(0,0)"></asp:TableCell>
<asp:TableCell Text="(0,1)"></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Text="(1,0)"></asp:TableCell>
<asp:TableCell Text="(1,1)"></asp:TableCell>
</asp:TableRow>
</asp:table>
</p>
</td>
<td align="right">
<asp:Label ID="Label2" runat="server"
AssociatedControlID="borderColorList"
Text="Border Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label3" Runat="server"
AssociatedControlID="borderStyleList"
Text="Border Style:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderStyleList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderStyle">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label4" Runat="server"
AssociatedControlID="borderWidthList"
Text="Border Width">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="borderWidthList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBorderWidth">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label5" Runat="server"
AssociatedControlID="backColorList"
Text="Back Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="backColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeBackColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label6" Runat="server"
AssociatedControlID="foreColorList"
Text="Foreground Color:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="foreColorList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeForeColor">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label7" Runat="server"
AssociatedControlID="fontNameList"
Text="Font Name:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="fontNameList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeFont">
</asp:dropdownlist>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label8" Runat="server"
AssociatedControlID="fontSizeList"
Text="Font Size:">
</asp:Label>
</td>
<td>
<asp:dropdownlist id="fontSizeList"
Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ChangeFontSize">
</asp:dropdownlist>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
注解
结构 Unit 表示可以以任何 HTML 兼容的大小单位表示的长度度量。 UnitType 枚举列出可以表示的度量单位。 属性使用此属性Unit,例如BorderWidth``Height
并表示属性的长度或宽度。 例如,可以将 Web 服务器控件的属性设置为 BorderWidth 表示五像素的控件 Unit 。
备注
类 Unit 只能表示介于 -32768 和 32767 之间的值。
使用 Value 属性确定度量的长度。 度量单位是使用属性确定的 Type 。 You can convert other data types to a Unit by using the Parse, Percentage, Pixel, and Point methods.
有关实例 Unit的初始属性值的列表,请参阅 Unit 构造函数。
构造函数
Unit(Double) |
用指定的双精度浮点数字初始化 Unit 结构的新实例。 |
Unit(Double, UnitType) | |
Unit(Int32) |
用指定的 32 位有符号整数初始化 Unit 结构的新实例。 |
Unit(String) |
用指定的长度初始化 Unit 结构的新实例。 |
Unit(String, CultureInfo) |
用指定的长度和 Unit 初始化 CultureInfo 结构的新实例。 |
字段
Empty |
表示空的 Unit。 此字段为只读。 |
属性
IsEmpty |
获取一个值,该值指示 Unit 是否为空。 |
Type |
获取 Unit 的单位类型。 |
Value |
获取 Unit 的长度。 |
方法
Equals(Object) |
将此 Unit 与指定的对象进行比较。 |
GetHashCode() |
返回此实例的哈希代码。 |
Parse(String) |
将指定字符串转换为 Unit。 |
Parse(String, CultureInfo) |
将指定的字符串和 CultureInfo 转换为 Unit。 |
Percentage(Double) |
从指定的双精度浮点数创建 Unit 类型的 Percentage。 |
Pixel(Int32) | |
Point(Int32) | |
ToString() | |
ToString(CultureInfo) |
将 Unit 转换为指定区域性中的等效字符串。 |
ToString(IFormatProvider) |
使用指定的格式提供程序将 Unit 转换为等效字符串。 |
运算符
Equality(Unit, Unit) |
比较两个 Unit 对象以确定它们是否相等。 |
Implicit(Int32 to Unit) | |
Inequality(Unit, Unit) |
比较两个 Unit 对象以确定它们是否不相等。 |