View previous topic :: View next topic |
Author |
Message |
AB
Joined: 20 Nov 2007 Posts: 23 Location: Netherlands
|
Posted: Tue Nov 20, 2007 9:08 am Post subject: File browse button in column |
|
|
Dear Support,
in my treeview, there is a column named "filename". Is it possible to add a browse button in each cell of this column? When user clicks this button, an OpenFileDialog or something similar should open.
Thanks a lot,
Anja |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Nov 20, 2007 10:55 pm Post subject: |
|
|
If the browse button will be the only thing in the control then this is relatively simple. Create a CellEditor using a Button control as the editor control. Set this to be the CellEditor for the column.
Because the editor control (in this case a Button) is just a template for the editor controls actually created at runtime you can't add event handlers to it (actually you can - but they won't get called). To attach event handlers to the runtime controls you need to handle the CellEditor.InitializeControl event and add the event handlers there eg
Code: | private void OnInitializeButton(object sender, CellEditorInitializeEventArgs e)
{
ComboBox comboBox = (ComboBox)e.Control;
if (e.NewControl)
{
e.Control.Click += new System.EventHandler(OnButtonClick);
}
} |
If you want to display both text (say the filename) and a button in the same column then you could create a custom user control which combines a label (to display the text) and a button and use this as an Editor Control. However perhaps a better way to achieve this would be to define a custom TypeEditor for editing filenames eg
Code: | public class FileNameEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
return ofd.FileName;
return value;
}
} |
You then associate this TypeEditor with property on your object that returns the FileName using the Editor attribute:
Code: | [Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string FileName
{
get { return _filename; }
set { _filename = value; }
} |
Now if you use an Infralution UniversalEditBox as the Editor control it will automatically use your custom TypeEditor to edit the cell values. It provides a button to the right which when clicked will now display the FileDialog. I would probably set the the CellEditor.DisplayMode to OnEdit to avoid having the edit boxes displayed when you don't need them. _________________ Infralution Support |
|
Back to top |
|
|
AB
Joined: 20 Nov 2007 Posts: 23 Location: Netherlands
|
Posted: Wed Nov 21, 2007 9:34 am Post subject: |
|
|
Thanks a lot for your answer, it works great, very good support!
Anja |
|
Back to top |
|
|
|