如何实现带文件浏览功能的控件属性 用C#写一个控件,如何能实现一个属性,后面带有三个小点的按钮.点击后出现浏览文件的对话框可以选择文件最好有过滤器只能选择*.XML 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 三个小点的按钮用普通按钮代替即可,设置Text为“...”浏览文件的对话框使用OpenFileDialog组件,在按钮的Click事件中,添加下面的代码:openFileDialog1.Filter = "XML文件(*.xml)|*.XML";openFileDialog1.ShowDialog(); 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 }} 使用时写SelectFileChanged事件代码就可以了private void fileSelectButton1_SelectFileChanged() { this.textBox1.Text = this.fileSelectButton1.SelectFileName; } 大家注意是自定义控件,不是WindowsApplication 还是应该研究一下System.ComponentModel类的成员和方法 就目前给的需求,用UserControl就足以应付了,还需要自己从头写一个Component吗? 这样的话在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编辑类. http://www.cnblogs.com/haoxiaobo/archive/2005/06/02/166805.html 开启windows服务怎么整 EntityDataModel创建ObjectContext对象的问题 对calendar组建了解的吗? ListView列中如何显示Float?,我试验了很久只能显示string的值 一个简单问题。 C# .net中用CrystalReport打印报表如何自定义图片。 又是截取字符串问题...高人看看... 如何更好理解类成员访问修饰符? 怎样用c#或API编程实现对本机收发数据包数和字节数进行读取?高手帮忙呀! 急得不得了:在C#中如何把字符串转换成数值型 C#中有什么类型可以如此.....? 求一正则表达式,判断一个字符串为正整数
浏览文件的对话框使用OpenFileDialog组件,
在按钮的Click事件中,添加下面的代码:
openFileDialog1.Filter = "XML文件(*.xml)|*.XML";
openFileDialog1.ShowDialog();
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
}
}
private void fileSelectButton1_SelectFileChanged()
{
this.textBox1.Text = this.fileSelectButton1.SelectFileName;
}
是自定义控件,不是WindowsApplication
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编辑类.