日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
探尋C#WinForm控件開發(fā)實現(xiàn)下拉式屬性編輯器

這篇文章我將介紹如何編寫下拉式屬性編輯器。下拉式(DropDown)屬性編輯器和模態(tài)對話框?qū)傩跃庉嬈鞯牟煌幘褪?,?dāng)你點擊屬性值修改的時候,模態(tài)對話框編輯器是彈出一個模態(tài)對話框,而下拉式屬性編輯器卻是在緊貼著屬性值的地方顯示一個下拉的控件。

在成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)過程中,需要針對客戶的行業(yè)特點、產(chǎn)品特性、目標受眾和市場情況進行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計方向。創(chuàng)新互聯(lián)建站還需要根據(jù)客戶的需求進行功能模塊的開發(fā)和設(shè)計,包括內(nèi)容管理、前臺展示、用戶權(quán)限管理、數(shù)據(jù)統(tǒng)計和安全保護等功能。

不知道大家注意到了沒有,這里我說的是顯示一個下拉的控件,而這個控件也是需要你去C# WinForm控件開發(fā)的,接下來我還是以Scope屬性為例,介紹一下具體的實現(xiàn)。

首先我們要創(chuàng)建一個用于編輯屬性的控件,在本系列文章的開始,我們介紹了自定義控件有三種類型:復(fù)合控件,擴展控件,自定義控件。在本例中我們制作一個復(fù)合控件(Compsite control),復(fù)合控件的C# WinForm控件開發(fā)比較簡單,不在本系列文章的講解范圍。

我簡單做個介紹,在Solution 瀏覽器里右鍵點擊CustomControlSample工程選擇Add->User Control…,輸入文件名ScopeEditorControl.cs。我們做的這個復(fù)合控件上一篇文章介紹的模態(tài)對話框所包含子控件基本一樣,除了用于確認和取消的按鈕,如下圖:

C# WinForm控件開發(fā)下拉式屬性編輯器

由于我們?nèi)∠擞糜诖_認和取消的按鈕,并且是一個下拉的編輯器控件,在出現(xiàn)下面三種情況的時候下拉的編輯器控件會關(guān)閉:用戶敲了回車,用戶敲了ESC鍵,用戶點擊了編輯器以外的地方。當(dāng)下拉編輯器控件關(guān)閉的時候我們就需要更新屬性的值。下邊是這個控件的代碼:

 
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. namespace CustomControlSample  
  9. {  
  10.     public partial class ScopeEditorControl : UserControl  
  11.     {  
  12.         private Scope _oldScope;  
  13.         private Scope _newScope;  
  14.         private Boolean canceling;  
  15.         public ScopeEditorControl(Scope scope)  
  16.         {  
  17.             _oldScope = scope;  
  18.             _newScope = scope;  
  19.             InitializeComponent();  
  20.         }  
  21.         public Scope Scope  
  22.         {  
  23.             get 
  24.             {  
  25.                 return _newScope;  
  26.             }  
  27.         }  
  28.         private void textBox1_Validating(object sender, CancelEventArgs e)  
  29.         {  
  30.             try 
  31.             {  
  32.                 Int32.Parse(textBox1.Text);  
  33.             }  
  34.             catch (FormatException)  
  35.             {  
  36.                 e.Cancel = true;  
  37.                 MessageBox.Show("無效的值", "驗證錯誤",
     MessageBoxButtons.OK, MessageBoxIcon.Error);  
  38.             }  
  39.         }  
  40.         private void textBox2_Validating(object sender, CancelEventArgs e)  
  41.         {  
  42.             try 
  43.             {  
  44.                 Int32.Parse(textBox2.Text);  
  45.             }  
  46.             catch (FormatException)  
  47.             {  
  48.                 e.Cancel = true;  
  49.                 MessageBox.Show("無效的值", "驗證錯誤",
     MessageBoxButtons.OK, MessageBoxIcon.Error);  
  50.             }  
  51.         }  
  52.        protected override bool ProcessDialogKey(Keys keyData)  
  53.         {  
  54.             if (keyData == Keys.Escape)  
  55.             {  
  56.                 _oldScope = _newScope;  
  57.                 canceling = true;  
  58.             }  
  59.             return base.ProcessDialogKey(keyData);  
  60.         }  
  61.         private void ScopeEditorControl_Leave(object sender, EventArgs e)  
  62.         {  
  63.             if (!canceling)  
  64.             {  
  65.                 _newScope.Max = Convert.ToInt32(textBox1.Text);  
  66.                 _newScope.Min = Convert.ToInt32(textBox2.Text);  
  67.             }  
  68.         }  
  69.         private void ScopeEditorControl_Load(object sender, EventArgs e)  
  70.         {  
  71.             textBox1.Text = _oldScope.Max.ToString();  
  72.             textBox2.Text = _oldScope.Min.ToString();  
  73.         }  
  74.     }  

和模態(tài)對話框編輯器一樣,C# WinForm控件開發(fā)環(huán)境并不會直接調(diào)用我們的編輯器控件,而是用過UITypeEditor類的派生來實現(xiàn)編輯器的調(diào)用,所以我們必須實現(xiàn)一個下拉式編輯器。代碼如下:

 
 
 
 
  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Drawing.Design;  
  4. using System.Windows.Forms.Design;  
  5. using System.Windows.Forms;  
  6. namespace CustomControlSample  
  7. {  
  8.     public class ScopeDropDownEditor : UITypeEditor  
  9.     {  
  10.         public override UITypeEditorEditStyle GetEditStyle
    (ITypeDescriptorContext context)  
  11.         {  
  12.             if (context != null && context.Instance != null)  
  13.             {  
  14.                 return UITypeEditorEditStyle.DropDown;  
  15.             }  
  16.             return base.GetEditStyle(context);  
  17.         }  
  18.         public override object EditValue(ITypeDescriptorContext context, 
    IServiceProvider provider, object value)  
  19.         {  
  20.             IWindowsFormsEditorService editorService = null;  
  21.             if (context !=
     null && context.Instance != null && provider != null)  
  22.             {  
  23.                 editorService = 
    (IWindowsFormsEditorService)provider.GetService
    (typeof(IWindowsFormsEditorService));  
  24.                 if (editorService != null)  
  25.                 {  
  26.                     MyListControl control = 
    (MyListControl)context.Instance;  
  27.                     ScopeEditorControl editorControl = 
    new ScopeEditorControl(control.Scope);  
  28.                     editorService.DropDownControl(editorControl);  
  29.                     value = editorControl.Scope;  
  30.                     return value;  
  31.                 }  
  32.             }  
  33.             return value;  
  34.         }  
  35.     }  
  36. }  
  37.  

 看過上一篇文章的朋友應(yīng)該對這段代碼很熟悉,是的,這兩個編輯器的代碼只有幾行不同之處,在GetEditStyle方法中,我們返回的是UITypeEditorEditStyle.DropDown,而不是UITypeEditorEditStyle.Modal,表明我們的編輯器是一個下拉式的編輯器。

在EditValue中的不同之處是,我們使用DropDownControl方法來顯示編輯器。編輯器制作完畢,我們把Scope以前的編輯器替換成下拉式編輯器,如下:

 
 
 
 
  1. [Browsable(true)]  
  2.         [Editor(typeof(ScopeDropDownEditor), typeof(UITypeEditor))]  
  3.         public Scope Scope  
  4.         {  
  5.             get 
  6.             {  
  7.                 return _scope;  
  8.             }  
  9.             set 
  10.             {  
  11.                 _scope = value;  
  12.             }  
  13.         } 

 現(xiàn)在build CustomControlSample工程,然后切換到測試工程查看Scope屬性。當(dāng)我們點擊屬性的值,在屬性值的后邊出現(xiàn)了一個按鈕:

C# WinForm控件開發(fā)下拉式屬性編輯器

當(dāng)點擊這個按鈕的時候,下拉的屬性編輯器出現(xiàn)了:

好了,C# WinForm控件開發(fā)的下拉式屬性編輯器的編輯到這里就講完了。

 
C# WinForm控件開發(fā)下拉式屬性編輯器


網(wǎng)站題目:探尋C#WinForm控件開發(fā)實現(xiàn)下拉式屬性編輯器
標題URL:http://www.5511xx.com/article/dhdjehh.html