FontNamesConverter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在包含字体名称列表的字符串和表示个别名称的字符串数组之间进行转换。
public ref class FontNamesConverter : System::ComponentModel::TypeConverter
public class FontNamesConverter : System.ComponentModel.TypeConverter
type FontNamesConverter = class
inherit TypeConverter
Public Class FontNamesConverter
Inherits TypeConverter
- 继承
示例
下面的代码示例演示如何使用 FontNamesConverter 类将具有字体名称列表的字符串转换为包含单个名称的字符串数组。 然后,字符串数组将转换回单个字符串并显示。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>FontNamesConverter Example</title>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Declare local variables.
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en");
System.ComponentModel.ITypeDescriptorContext context = null;
Object names;
Object name_string;
// Create FontNamesConverter object.
FontNamesConverter fontconverter = new FontNamesConverter();
// Create original list of fonts.
string font_list = "arial, times new roman, verdana";
// Check for type compatibility.
if (fontconverter.CanConvertFrom(context, typeof(string)))
{
// Display original string.
Label1.Text = "Original String :" + "<br /><br />" + font_list;
// Convert string to array to strings and display results.
names = fontconverter.ConvertFrom(context, culture, font_list);
Label2.Text = "Converted to Array of Strings : " + "<br /><br />";
foreach (string name_element in (string[])names)
{
Label2.Text += name_element + "<br />";
}
// Convert array of strings back to a string and display results.
name_string = fontconverter.ConvertTo(context, culture, names, typeof(string));
Label3.Text = "Converted back to String :" + "<br /><br />" + (string)name_string;
}
}
</script>
</head>
<body>
<h3>FontNamesConverter Example</h3>
<br />
<form id="form1" runat="server">
<asp:Label id="Label1" runat="server"/>
<br /><hr />
<asp:Label id="Label2" runat="server"/>
<br /><hr />
<asp:Label id="Label3" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>FontNamesConverter Example</title>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Declare local variables.
Dim culture As New System.Globalization.CultureInfo("en")
Dim context As System.ComponentModel.ITypeDescriptorContext = Nothing
Dim names As Object
Dim name_string As Object
' Create FontNamesConverter object.
Dim fontconverter As New FontNamesConverter()
' Create original list of fonts.
Dim font_list As String = "arial, times new roman, verdana"
' Check for type compatibility.
If fontconverter.CanConvertFrom(context, GetType(String)) Then
' Display original string.
Label1.Text = "Original String :" & "<br /><br />" & font_list
' Convert string to array to strings and display results.
names = fontconverter.ConvertFrom(context, culture, font_list)
Label2.Text = "Converted to Array of Strings : " & "<br /><br />"
Dim name_element As String
For Each name_element In CType(names, String())
Label2.Text &= name_element & "<br />"
Next name_element
' Convert array of strings back to a string and display results.
name_string = fontconverter.ConvertTo(context, culture, names, _
GetType(String))
Label3.Text = "Converted back to String :" & "<br /><br />" & _
CType(name_string, String)
End If
End Sub 'Page_Load
</script>
</head>
<body>
<h3>FontNamesConverter Example</h3>
<br />
<form id="form1" runat="server">
<asp:Label id="Label1" runat="server"/>
<br /><hr />
<asp:Label id="Label2" runat="server"/>
<br /><hr />
<asp:Label id="Label3" runat="server"/>
</form>
</body>
</html>
注解
ConvertFrom使用此类的 方法可将包含字体名称列表的单个字符串转换为包含各个名称的字符串数组。 字符串中的每个字体名称都必须用逗号分隔。 例如,字符串“arial, times new roman, verdana”将转换为包含字符串“arial”、“times new roman”和“verdana”的数组。 请注意,将删除逗号以及字体名称开头或末尾的任何空格。 不会删除字体名称中间的空格。
方法 ConvertTo 执行反向操作。 它将包含单个字体名称的字符串数组转换为包含名称列表的单个字符串。 例如,包含字符串“arial”、“times new roman”和“verdana”的数组将转换为字符串“arial,times new roman,verdana”。 请注意,在字体名称之间自动插入逗号,没有任何空格。
CanConvertFrom调用 方法以验证是否可以在调用 ConvertFrom 方法之前进行转换。
构造函数
FontNamesConverter() |
初始化 FontNamesConverter 类的新实例。 |