一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

WinForm控件開發總結(七)-----為復雜屬性的子屬性提供編輯功能

      前面的幾篇文章中,我們給控件添加一個復雜的類型Scope,并且給它的類型提供的一個類型轉換器,現在我們可以在屬性瀏覽器中編輯它的值,并且它的值也被串行化的源代碼里了。但是你有沒有發現,在屬性瀏覽器里編輯這個屬性的值還是不太方便。因為屬性只是“10,200這種形式的,所以,你必須按照這種格式來修改,一旦格式錯誤就會引發異常,比如輸入一個“10200。我們期望這個屬性的每一子屬性都能夠被獨立的編輯就好了,這并非不能實現,而且實現還很簡單。
      為了在屬性瀏覽器里能夠獨立的編輯子屬性,我們還要重寫兩個方法:GetPropertiesSupported()和GetProperties();下面是ScopeConverter的完整代碼:
   public class ScopeConverter : TypeConverter
    
{
        
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        
{
            
if (sourceType == typeof(String)) return true;

            
return base.CanConvertFrom(context, sourceType);
        }


        
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        
{
            
if (destinationType == typeof(String)) return true;

            
if (destinationType == typeof(InstanceDescriptor)) return true;

            
return base.CanConvertTo(context, destinationType);
        }


        
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.
CultureInfo culture, 
object value, Type destinationType)
        
{
            String result 
= "";
            
if (destinationType == typeof(String))
            
{
                Scope scope 
= (Scope)value;
                result 
= scope.Min.ToString()+"," + scope.Max.ToString();
                
return result;

            }


            
if (destinationType == typeof(InstanceDescriptor))
            
{
                ConstructorInfo ci 
= typeof(Scope).GetConstructor(new Type[] {typeof(Int32),
typeof
(Int32) }
);
                Scope scope 
= (Scope)value;
                
return new InstanceDescriptor(ci, new object[] { scope.Min,scope.Max });
            }

            
return base.ConvertTo(context, culture, value, destinationType);
        }


        
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.
CultureInfo culture, 
object value)
        
{
            
if (value is string)
            
{
                String[] v 
= ((String)value).Split(',');
                
if (v.GetLength(0!= 2)
                
{
                    
throw new ArgumentException("Invalid parameter format");
                }


                Scope csf 
= new Scope();
                csf.Min 
= Convert.ToInt32(v[0]);
                csf.Max 
= Convert.ToInt32(v[1]);
                
return csf;
            }

            
return base.ConvertFrom(context, culture, value);
        }


        
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        
{
            
return true;
        }


        
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
object
 value, Attribute[] attributes)
        
{
            
return TypeDescriptor.GetProperties(typeof(Scope), attributes);
        }

}

NET技術WinForm控件開發總結(七)-----為復雜屬性的子屬性提供編輯功能,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 天天色踪合 | 久久久国产一区二区三区丝袜 | 亚洲国产精品自产在线播放 | 91麻豆国产精品91久久久 | 九九热免费在线观看 | 真实国产乱子伦久久 | 国产香蕉久久精品综合网 | 加勒比一区二区三区 | 国产精品免费入口视频 | 国产成人综合在线 | 久久亚洲日本不卡一区二区 | 韩国免费特一级毛片 | 2022年国产精品久久久久 | 国产精品亚洲一区二区三区正片 | 黄色小视频在线观看 | 国产亚洲欧美另类专区 | 久久99精品久久久久久秒播 | 久草小说 | 九九午夜| 91色国产| 亚洲精品高清国产一久久 | 69视频在线观看免费 | 日本高清www色 | 69国产成人综合久久精品 | 最新亚洲精品国自产在线 | 2021久久精品国产99国产 | 永久免费aavv视频播放 | 欧美三级网址 | 欧美成人观看 | 亚洲欧洲日产国码 最新 | 亚洲一区色 | 中文字幕一区二区三区 精品 | 精品欧美日韩一区二区三区 | 成人国产在线视频在线观看 | 看一级毛片国产一级毛片 | 国语对白精品视频在线观看 | 国产精品视频第二区第二页 | 日本高清在线精品一区二区三区 | 国产成人福利美女观看视频 | 国产成人精品福利网站在线观看 | 亚洲伊人激情 |