FontNamesConverter 类

将包含字体名称列表的字符串转换为包含个别名称的字符串数组。它还执行反转功能。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public Class FontNamesConverter
    Inherits TypeConverter
用法
Dim instance As FontNamesConverter
public class FontNamesConverter : TypeConverter
public ref class FontNamesConverter : public TypeConverter
public class FontNamesConverter extends TypeConverter
public class FontNamesConverter extends TypeConverter

备注

使用此类的 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 类将包含字体名称列表的字符串转换为包含个别名称的字符串数组。然后,字符串数组被转换回单个字符串并显示出来。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <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>
   <p>

   <form 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="C#" AutoEventWireup="True" %>

<html>
<head>

   <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>
   <p>

   <form 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>
   

.NET Framework 安全性

继承层次结构

System.Object
   System.ComponentModel.TypeConverter
    System.Web.UI.WebControls.FontNamesConverter

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

FontNamesConverter 成员
System.Web.UI.WebControls 命名空间
System.ComponentModel.TypeConverter

其他资源

如何:实现类型转换器
类型转换器示例