#region _DataGridViewFilePathColumn
public class DataGridViewFilePathColumn : System.Windows.Forms.DataGridViewColumn
{
public DataGridViewFilePathColumn()
: base(new DataGridViewFilePathCell())
{
}
public override object Clone()
{
var clm = base.Clone() as DataGridViewFilePathColumn;
DataGridViewFilePathColumn xxx = base.Clone() as DataGridViewFilePathColumn;
if (clm != null)
{
}
return clm;
}
public override System.Windows.Forms.DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set
{
if ((value != null) && !value.GetType().IsAssignableFrom(typeof(DataGridViewFilePathCell)))
{
throw new InvalidCastException("Must be a DataGridViewFilePathCell");
}
base.CellTemplate = value;
}
}
}
public class DataGridViewFilePathCell : System.Windows.Forms.DataGridViewTextBoxCell
{
public DataGridViewFilePathCell()
{
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
DataGridViewFilePathEdit ctl = (DataGridViewFilePathEdit)DataGridView.EditingControl;
if ((!object.ReferenceEquals(this.Value, DBNull.Value)))
{
if ((this.Value != null))
{
ctl.Text = Convert.ToString(this.Value);
}
}
/* DataGridViewFilePathColumn fileColumn = (DataGridViewFilePathColumn)this.DataGridView.Columns[ColumnIndex];
ctl.Format = fileColumn.PickerFormat;*/
}
public override Type EditType
{
// Return the type of the editing contol that DataGridViewFilePathCell uses.
get { return typeof(DataGridViewFilePathEdit); }
}
public override Type ValueType
{
// Return the type of the value that DataGridViewFilePathCell contains.
get { return typeof(string); }
}
}
class DataGridViewFilePathEdit : WindowsFormsApplication2.TextboxDialog, System.Windows.Forms.IDataGridViewEditingControl
{
private System.Windows.Forms.DataGridView dataGridViewControl;
private bool valueIsChanged = false;
private int rowIndexNum;
public DataGridViewFilePathEdit()
{
// this.Format = System.Windows.Forms.DateTimePickerFormat.Short;
}
public object EditingControlFormattedValue
{
get { return this.Text; }
set
{
if (value is String)
{
this.Text = Convert.ToString(value);
}
}
}
public object GetEditingControlFormattedValue(System.Windows.Forms.DataGridViewDataErrorContexts context)
{
return this.Text;
}
public void ApplyCellStyleToEditingControl(System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get { return rowIndexNum; }
set { rowIndexNum = value; }
}
public bool EditingControlWantsInputKey(System.Windows.Forms.Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & System.Windows.Forms.Keys.KeyCode)
{
case System.Windows.Forms.Keys.Left:
case System.Windows.Forms.Keys.Up:
case System.Windows.Forms.Keys.Down:
case System.Windows.Forms.Keys.Right:
case System.Windows.Forms.Keys.Home:
case System.Windows.Forms.Keys.End:
case System.Windows.Forms.Keys.PageDown:
case System.Windows.Forms.Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
public System.Windows.Forms.DataGridView EditingControlDataGridView
{
get { return dataGridViewControl; }
set { dataGridViewControl = value; }
}
public bool EditingControlValueChanged
{
get { return valueIsChanged; }
set { valueIsChanged = value; }
}
public System.Windows.Forms.Cursor EditingControlCursor
{
get { return base.Cursor; }
}
System.Windows.Forms.Cursor System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
{
get { return EditingControlCursor; }
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
}
}
#endregion
Code (C#)
[ToolboxBitmap(typeof(TextBox))]
public partial class TextboxDialog : TextBox
{
private bool bLinkClicked;
public TextboxDialog()
{
InitializeComponent();
_lnk.Text = this.Text;
}
private System.Windows.Forms.LinkLabel _lnk;
private System.Windows.Forms.Button _btn;
// private System.Windows.Forms.TextBox _txt;
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this._lnk = new System.Windows.Forms.LinkLabel();
this._btn = new System.Windows.Forms.Button();
Font fnt = new System.Drawing.Font("Microsoft Sans Serif", 10.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
//
// _lnk
//
this._lnk.AutoSize = true;
this._lnk.BackColor = System.Drawing.SystemColors.ControlLight;
this._lnk.Font = fnt;
this._lnk.Location = new System.Drawing.Point(-1, 1);
this._lnk.Name = "_lnk";
this._lnk.Size = new System.Drawing.Size(79, 16);
this._lnk.TabIndex = 1;
this._lnk.TabStop = true;
this._lnk.Text = "linkLabel1";
this._lnk.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this._lnk_LinkClicked);
//
// _btn
//
this._btn.Dock = System.Windows.Forms.DockStyle.Right;
this._btn.Image = Properties.Resources.finds;// global::TORServices.FormsTor.Properties.Resources.search32x32;
this._btn.Font = fnt;
this._btn.FlatStyle = FlatStyle.Flat;
this._btn.Location = new System.Drawing.Point(478, 0);
this._btn.Margin = new System.Windows.Forms.Padding(4);
this._btn.Cursor = Cursors.Hand;
this._btn.Name = "_btn";
// this._btn.Text = "...";
this._btn.Size = new System.Drawing.Size(32, 24);
this._btn.TabIndex = 2;
this._btn.UseVisualStyleBackColor = true;
this._btn.Click += new System.EventHandler(this._btn_Click);
//
// TextboxDialog
//
this.Controls.Add(this._lnk);
this.Controls.Add(this._btn);
this.Font = fnt;
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "TextboxDialog";
this.Size = new System.Drawing.Size(300, 24);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
#region _Property
private bool oponly = true;
[System.ComponentModel.Description("Set OpenOnly on Text"),
System.ComponentModel.Browsable(true),
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always),
System.ComponentModel.Category("TOR Setting")]
public bool OpenOnly
{
get { return oponly; }
set { oponly = value; }
}
// public string FullPath { get { return _Path + "\\" + this.Text; } }
private string _Path;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.DefaultValue(@"C:")]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path.")]
public string Directory
{
get
{
/* string p = (string.IsNullOrEmpty(_Path)) ? Application.StartupPath : _Path.Replace("AppPath", Application.StartupPath);
p = (!string.IsNullOrEmpty(System.IO.Path.GetPathRoot(p))) ? p : Application.StartupPath + "\\" + p;
p = (p.EndsWith("\\")) ? p : p + "\\";
return p;*/
return _Path;
}
set
{
_Path = value;
}
}
private bool _showfullpath;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Full path directory")]
public bool Showfullpath
{
get { return _showfullpath; }
set
{
_showfullpath = value;
if (_showfullpath == true)
Text = FullPath;
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Set SizeButton")]
public Size SizeButton
{
get
{
return _btn.Size;
}
set
{
_btn.Size = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Set ImageButton")]
public Image ImageButton
{
get
{
return _btn.Image;
}
set
{
_btn.Image = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Set FontButton")]
public Font FontButton
{
get
{
return _btn.Font;
}
set
{
_btn.Font = value; Invalidate();
}
}
[Localizable(true)]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Set TextButton")]
public string TextButton
{
get
{
return _btn.Text;
}
set
{
_btn.Text = value; Invalidate();
}
}
// private string pathDirectory = "";
public string FullPath
{
get
{
string directory = (string.IsNullOrEmpty(Directory)) ? Application.StartupPath : Directory.Replace("AppPath", Application.StartupPath);
directory = (!string.IsNullOrEmpty(System.IO.Path.GetPathRoot(directory))) ? directory : Application.StartupPath + "\\" + directory;
directory = (directory.EndsWith("\\")) ? directory : directory + "\\";
string path = (Showfullpath == true) ? Convert.ToString(this.Text) : directory + Convert.ToString(this.Text);
return (string.IsNullOrEmpty(path)) ? Application.StartupPath : path;
}
}
#endregion
private void _btn_Click(object sender, EventArgs e)
{
if (!System.IO.Directory.Exists(_Path))
try
{
System.IO.Directory.CreateDirectory(_Path);
}
catch { }
string fileSave = "";
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
if (!oponly)
{
using (SaveFileDialog sf = new SaveFileDialog())
{
if (!string.IsNullOrEmpty(_Path))
sf.InitialDirectory = _Path;
sf.RestoreDirectory = true;
sf.FileName = System.IO.Path.GetFileName(dialog.FileName);
if (sf.ShowDialog() == DialogResult.OK)
{
fileSave = (string.IsNullOrEmpty(System.IO.Path.GetExtension(sf.FileName)) || System.IO.Path.GetExtension(sf.FileName).Length <= 0) ? sf.FileName + System.IO.Path.GetExtension(dialog.FileName) : sf.FileName;
if (!System.IO.File.Exists(fileSave))
try
{
System.IO.File.Copy(dialog.FileName, fileSave);
}
catch { }
}
else
{
fileSave = dialog.FileName;
}
}
base.Text = (System.IO.Path.GetDirectoryName(fileSave) != _Path) ? fileSave : System.IO.Path.GetFileName(fileSave);
}
else
{
base.Text = dialog.FileName;
}
}
}
}
#region Focus overrides
protected override void OnFontChanged(EventArgs e)
{
_btn.Font = Font;
_lnk.Font = Font;
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e); this.SwitchToEditMode(false);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e); this.SwitchToEditMode(true);
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
_lnk.Text = this.Text;
_lnk.Links.Clear();
_lnk.Links.Add(0, _lnk.Text.Length, FullPath);
}
private void _txt_TextChanged(object sender, EventArgs e)
{
}
private void _txt_BackColorChanged(object sender, EventArgs e)
{
_lnk.BackColor = this.BackColor;
}
public event EventHandler ButtonClick
{
add
{
_btn.Click += value;
}
remove
{
_btn.Click -= value;
}
}
public event EventHandler ButtonDoubleClick
{
add
{
_btn.DoubleClick += value;
}
remove
{
_btn.DoubleClick -= value;
}
}
protected void SwitchToEditMode(bool _bEditMode)
{
_lnk.Visible = !_bEditMode;
}
#endregion
#region Link Activation
private void _lnk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
/* try
{*/
if (_lnk.Links.Count > 0)
{
if (System.IO.File.Exists(FullPath))
{
System.Diagnostics.Process.Start(FullPath);
}
else
{
MessageBox.Show("File Not Found"); return;
}
}
/* }
catch (Exception ex)
{
throw new ArgumentException("Link error!", ex);
}*/
}
#endregion
#region Focus Handling
private void _lnk_GotFocus(object sender, EventArgs e)
{
if (!bLinkClicked)
{
this.Focus();
bLinkClicked = false;
}
}
private void _lnk_MouseDown(object sender, MouseEventArgs e)
{
bLinkClicked = true;
}
#endregion
}