用C#写一个控件,如何能实现一个属性,后面带有三个小点的按钮.
点击后出现浏览文件的对话框可以选择文件
最好有过滤器只能选择*.XML

解决方案 »

  1.   

    三个小点的按钮用普通按钮代替即可,设置Text为“...”
    浏览文件的对话框使用OpenFileDialog组件,
    在按钮的Click事件中,添加下面的代码:
    openFileDialog1.Filter = "XML文件(*.xml)|*.XML";
    openFileDialog1.ShowDialog();
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;namespace WindowsApplication2
    {
    /// <summary>
    /// FileSelectButton 的摘要说明。
    /// </summary>
    public class FileSelectButton : System.Windows.Forms.Button 
    {
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; private string selectFileName;
            private string[] selectFileNames ; public string SelectFileName
    {
    get
    {
    return selectFileName;
    }
    } public string[] SelectFileNames
    {
    get
    {
    return selectFileNames;
    }
    } public delegate void SelectFileChangedHander();
    public event SelectFileChangedHander SelectFileChanged; protected virtual void OnSelectFileChanged()
    {
    if (SelectFileChanged != null)
    {
    SelectFileChanged();
    }
    } protected override void OnClick(EventArgs e)
    {
    base.OnClick (e);
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "XML文件(*.xml)|*.XML";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
    this.selectFileName = dlg.FileName;
    this.selectFileNames = dlg.FileNames;
    OnSelectFileChanged();
    }
    dlg.Dispose();
    }        
    public FileSelectButton()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化
    this.Text = "...";
    this.Width = 32;
    this.Height = 24; } public override string Text
    {
    get
    {
    return "...";
    }
    set
    {
    // base.Text = value;
    }
    }
    /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region 组件设计器生成的代码
    /// <summary> 
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }
      

  3.   

    使用时写SelectFileChanged事件代码就可以了
    private void fileSelectButton1_SelectFileChanged()
    {
    this.textBox1.Text = this.fileSelectButton1.SelectFileName;
    }
      

  4.   

    大家注意
    是自定义控件,不是WindowsApplication
      

  5.   

    还是应该研究一下System.ComponentModel类的成员和方法
      

  6.   

    就目前给的需求,用UserControl就足以应付了,还需要自己从头写一个Component吗?
      

  7.   

    这样的话在PropertyGrid里s属性的后面只有一个简简单单的编辑框, 为了使s的编辑区可以出现一个"..." 按钮, 并且可以用文件打开对话框选择文件, 可以使用EditorAttribute属性对这个属性进行标记:[Editor(typeof(System.Windows.Forms.Design.FileNameEditor), 
            typeof(System.Drawing.Design.UITypeEditor))]
    public string S
    {
        get{return this._s;}
        set{this._s = value;}
    }
    FileNameEditor类提供了一个打开文件的对话框, 以编辑属性值, 这个类派生于UITypeEditor类, UITypeEditor是所有设计时界面的基类. 这样就可以达到有文件对话框的目的了, 但是如果想要指定只选择某一种文件类型, 如"WAV"文件, 那就得派生FileNameEditor, 重写有关的方法.public class SoundFileEditor:System.Windows.Forms.Design.FileNameEditor
    {
        protected override void InitializeDialog(OpenFileDialog openFileDialog)
        {
            base.InitializeDialog (openFileDialog);
            // 在基类初始代完对话框之后, 可以对这个对话框做一些手脚.
            openFileDialog.Filter = "wav and vox file(*.wav, *.vox)|*.wav;*.vox|wav files (*.wav)|*.wav|vox files (*.vox)|*.vox|All files (*.*)|*.*";
        }
    }
    再把Class1中的S的EditorAttribute改为派生的这个子类, 现在对话框里的文件过滤器改为你想要的了.你甚至可以从UITypeEditor自己重新派生一个子类出来, 自己写一个能弹出"保存文件"对话框的UI编辑类.
      

  8.   

    http://www.cnblogs.com/haoxiaobo/archive/2005/06/02/166805.html