国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

充分利用 .NET 框架的 PropertyGrid 控件(微軟)之七

2024-07-10 13:04:00
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

使用此版本的選項窗口后,您會注意到以下幾點:

顯示窗口時,將首先突出顯示 saveonclose 屬性。
選中 maxrepeatrate 屬性時,說明幫助窗格中將顯示“以毫秒表示的文本重復率”。
saveonclose 屬性顯示在“文檔設置”類別下。其他屬性分別顯示在“全局設置”和“版本”類別下。
settingschanged 屬性將不再顯示。
appversion 屬性為只讀。只讀屬性以灰顯文本顯示。
如果 saveonclose 屬性包含的值不是 true,該值將以粗體顯示。propertygrid 使用粗體文本表示包含非默認值的屬性。
顯示復雜屬性
到現在為止,選項窗口顯示的都是簡單的類型,如整數、布爾值和字符串。那么,如何顯示更復雜的類型呢?如果應用程序需要跟蹤窗口大小、文檔字體或工具欄顏色等信息,該如何處理呢?.net 框架提供的某些數據類型具有特殊的顯示功能,能使這些類型在 propertygrid 中更具可用性。

對所提供類型的支持
首先,請更新 appsettings 類,為窗口大小(size 類型)、窗口字體(font 類型)和工具欄顏色(color 類型)添加新屬性。

' visual basic

<defaultpropertyattribute("saveonclose")> _
public class appsettings
    private _saveonclose as boolean = true
    private _greetingtext as string = "歡迎使用應用程序!"
    private _maxrepeatrate as integer = 10
    private _itemsinmru as integer = 4

    private _settingschanged as boolean = false
    private _appversion as string = "1.0"

    private _windowsize as size = new size(100, 100)
    private _windowfont as font = new font("宋體", 9, fontstyle.regular)
    private _toolbarcolor as color = systemcolors.control

    <categoryattribute("文檔設置"), _
     defaultvalueattribute(true)> _
    public property saveonclose() as boolean
        get
            return _saveonclose
        end get
        set(byval value as boolean)
            saveonclose = value
        end set
    end property

    <categoryattribute("文檔設置")> _
    public property windowsize() as size
        get
            return _windowsize
        end get
        set(byval value as size)
            _windowsize = value
        end set
    end property

    <categoryattribute("文檔設置")> _
    public property windowfont() as font
        get
            return _windowfont
        end get
        set(byval value as font)
            _windowfont = value
        end set
    end property

    <categoryattribute("全局設置")> _
    public property toolbarcolor() as color
        get
            return _toolbarcolor
        end get
        set(byval value as color)
            _toolbarcolor = value
        end set
    end property

    <categoryattribute("全局設置"), _
    readonlyattribute(true), _
    defaultvalueattribute("歡迎使用應用程序!")> _
    public property greetingtext() as string
        get
            return _greetingtext
        end get
        set(byval value as string)
            _greetingtext = value
        end set
    end property

    <categoryattribute("全局設置"), _
    defaultvalueattribute(4)> _
    public property itemsinmrulist() as integer
        get
            return _itemsinmru
        end get
        set(byval value as integer)
            _itemsinmru = value
        end set
    end property

    <descriptionattribute("以毫秒表示的文本重復率。"), _
    categoryattribute("全局設置"), _
    defaultvalueattribute(10)> _
    public property maxrepeatrate() as integer
        get
            return _maxrepeatrate
        end get
        set(byval value as integer)
            _maxrepeatrate = value
        end set
    end property

    <browsableattribute(false),
     defaultvalueattribute(false)> _
    public property settingschanged() as boolean
        get
            return _settingschanged
        end get
        set(byval value as boolean)
            _settingschanged = value
        end set
    end property

    <categoryattribute("版本"), _
    defaultvalueattribute("1.0"), _
    readonlyattribute(true)> _
    public property appversion() as string
        get
            return _appversion
        end get
        set(byval value as string)
            _appversion = value
        end set
    end property
end class

請注意,windowfont 屬性帶有一個省略號 (...) 按鈕,按下該按鈕將顯示字體選擇對話框。此外,還可以展開該屬性以顯示更多的 font 屬性。某些 font 屬性提供有關字體的值和詳細信息的下拉列表。您可以展開 windowsize 屬性以顯示 size 類型的更多屬性。最后,請注意,toolbarcolor 屬性包含一個選定顏色的樣本,以及一個用于選擇不同顏色的自定義下拉列表。對于這些以及其他數據類型,.net 框架提供了其他的類,可以使在 propertygrid 中的編輯更加容易。

對自定義類型的支持
現在,您需要在 appsettings 類中添加另外兩個屬性,即 defaultfilename 和 spellcheckoptions。 defaultfilename 屬性用于獲取或設置字符串;spellcheckoptions 屬性用于獲取或設置 spellingoptions 類的實例。

spellingoptions 類是一個新類,用于管理應用程序的拼寫檢查屬性。對于何時創建單獨的類以管理對象的屬性,并沒有嚴格的規定,而取決于您的整個類設計。將 spellingoptions 類定義添加到應用程序項目中 - 可以添加到新文件中,也可以添加到窗體源代碼的下方。

' visual basic

<descriptionattribute("展開以查看應用程序的拼寫選項。")> _
public class spellingoptions
    private _spellcheckwhiletyping as boolean = true
    private _spellcheckcaps as boolean = false
    private _suggestcorrections as boolean = true

    <defaultvalueattribute(true)> _
    public property spellcheckwhiletyping() as boolean
        get
            return _spellcheckwhiletyping
        end get
        set(byval value as boolean)
            _spellcheckwhiletyping = value
        end set
    end property

    <defaultvalueattribute(false)> _
    public property spellcheckcaps() as boolean
        get
            return _spellcheckcaps
        end get
        set(byval value as boolean)
            _spellcheckcaps = value
        end set
    end property

    <defaultvalueattribute(true)> _
    public property suggestcorrections() as boolean
        get
            return _suggestcorrections
        end get
        set(byval value as boolean)
            _suggestcorrections = value
        end set
    end property
end class

請注意 spellcheckoptions 屬性的外觀。與 .net 框架類型不同,它不展開或顯示自定義的字符串表示。如果要在自己的復雜類型中提供與 .net 框架類型相同的編輯體驗,該如何處理呢?.net 框架類型使用 typeconverter 和 uitypeeditor 類提供大部分 propertygrid 編輯支持,您也可以使用這些類。

添加可展開屬性支持
要使 propertygrid 能夠展開 spellingoptions 屬性,您需要創建 typeconverter。typeconverter 提供了從一種類型轉換為另一種類型的方法。propertygrid 使用 typeconverter 將對象類型轉換為 string,并使用該 string 在網格中顯示對象值。在編輯過程中,typeconverter 會將 string 轉換回對象類型。.net 框架提供的 expandableobjectconverter 類可以簡化這一過程。

提供可展開對象支持
創建一個從 expandableobjectconverter 繼承而來的類。
' visual basic

public class spellingoptionsconverter
    inherits expandableobjectconverter
end class

如果 destinationtype 參數與使用此類型轉換器的類(示例中的 spellingoptions 類)的類型相同,則覆蓋 canconvertto 方法并返回 true;否則返回基類 canconvertto 方法的值。
' visual basic

public overloads overrides function canconvertto( _
                              byval context as itypedescriptorcontext, _
                              byval destinationtype as type) as boolean
    if (destinationtype is gettype(spellingoptions)) then
        return true
    end if
    return mybase.canconvertto(context, destinationtype)
end function

覆蓋 convertto 方法,并確保 destinationtype 參數是一個 string,并且值的類型與使用此類型轉換器的類(示例中的 spellingoptions 類)相同。如果其中任一情況為 false,都將返回基類 convertto 方法的值;否則,返回值對象的字符串表示。字符串表示需要使用唯一分隔符將類的每個屬性隔開。由于整個字符串都將顯示在 propertygrid 中,因此需要選擇一個不會影響可讀性的分隔符,逗號的效果通常比較好。
' visual basic

public overloads overrides function convertto( _
                              byval context as itypedescriptorcontext, _
                              byval culture as cultureinfo, _
                              byval value as object, _
                              byval destinationtype as system.type) _
                     as object
    if (destinationtype is gettype(system.string) _
        andalso typeof value is spellingoptions) then

        dim so as spellingoptions = ctype(value, spellingoptions)

        return "在鍵入時檢查: " & so.spellcheckwhiletyping & _
               ",檢查大小寫: " & so.spellcheckcaps & _
               ",建議更正: " & so.suggestcorrections
    end if
    return mybase.convertto(context, culture, value, destinationtype)
end function

(可選)通過指定類型轉換器可以從字符串進行轉換,您可以啟用網格中對象字符串表示的編輯。要執行此操作,首先需要覆蓋 canconvertfrom 方法并返回 true(如果源 type 參數為 string 類型);否則,返回基類 canconvertfrom 方法的值。
' visual basic

public overloads overrides function canconvertfrom( _
                           byval context as itypedescriptorcontext, _
                           byval sourcetype as system.type) as boolean
    if (sourcetype is gettype(string)) then
        return true
    end if
    return mybase.canconvertfrom(context, sourcetype)
end function

要啟用對象基類的編輯,同樣需要覆蓋 convertfrom 方法并確保值參數是一個 string。如果不是 string,將返回基類 convertfrom 方法的值;否則,返回基于值參數的類(示例中的 spellingoptions 類)的新實例。您需要根據值參數解析類的每個屬性的值。了解在 convertto 方法中創建的分隔字符串的格式將有助于您的解析。
' visual basic

public overloads overrides function convertfrom( _
                              byval context as itypedescriptorcontext, _
                              byval culture as cultureinfo, _
                              byval value as object) as object

    if (typeof value is string) then
        try
            dim s as string = cstr(value)
            dim colon as integer = s.indexof(":")
            dim comma as integer = s.indexof(",")

            if (colon <> -1 andalso comma <> -1) then
                dim checkwhiletyping as string = s.substring(colon + 1, _
                                                (comma - colon - 1))

                colon = s.indexof(":", comma + 1)
                comma = s.indexof(",", comma + 1)

                dim checkcaps as string = s.substring(colon + 1, _
                                                (comma - colon - 1))

                colon = s.indexof(":", comma + 1)

                dim suggcorr as string = s.substring(colon + 1)

                dim so as spellingoptions = new spellingoptions()

                so.spellcheckwhiletyping = boolean.parse(checkwhiletyping)
                so.spellcheckcaps = boolean.parse(checkcaps)
                so.suggestcorrections = boolean.parse(suggcorr)

                return so
            end if
        catch
            throw new argumentexception( _
                "無法將“" & cstr(value) & _
                                  "”轉換為 spellingoptions 類型")

        end try
    end if
    return mybase.convertfrom(context, culture, value)
end function

現在已經有了一個類型轉換器類,下面您需要確定使用該類的目標類。您可以通過將 typeconverterattribute 應用到目標類(示例中的 spellingoptions 類)來執行此操作。
' visual basic

' 應用于 spellingoptions 類的 typeconverter 特性。
<typeconverter(gettype(spellingoptionsconverter)), _
descriptionattribute("展開以查看應用程序的拼寫選項。")> _
public class spellingoptions
   ...
end class

// 應用于 spellingoptions 類的 typeconverter 特性。
[typeconverterattribute(typeof(spellingoptionsconverter)),
descriptionattribute("展開以查看應用程序的拼寫選項。")]
public class spellingoptions{ ... }
再次編譯并運行選項窗口應用程序。下面的屏幕快照顯示了選項窗口目前的外觀。

圖 6:在 propertygrid 中顯示的帶有類型轉換器的自定義數據類型

注意: 如果只需要可展開對象支持,而不需要自定義字符串表示,則只需將 typeconverterattribute 應用到類中。將 expandableobjectconverter 指定為類型轉換器類型。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴中市| 化德县| 开鲁县| 海兴县| 高尔夫| 富裕县| 广安市| 新巴尔虎右旗| 应用必备| 山西省| 祁阳县| 苍山县| 申扎县| 庆云县| 和田市| 上犹县| 自治县| 靖西县| 大竹县| 黄山市| 淮阳县| 泸西县| 秭归县| 红河县| 建平县| 沁源县| 潞城市| 丰城市| 鄱阳县| 工布江达县| 无为县| 龙州县| 泽州县| 临安市| 宁化县| 鄯善县| 玛沁县| 宝坻区| 东乡| 乐都县| 仪征市|