 |
|
C# WinApp Class DataGridViewTextBoxCell ติดปัญหาว่า ปุ่มใน DataGridViewTextBoxCell ไม่หายไปครับ |
|
 |
|
|
 |
 |
|
ปัญหาคือ
ถ้าเราคลิก แถวอื่นใน คอลัมน์ ปุ่มจะหายครับ
แต่ถ้าเราคลิกที่ columnheader หรือ คอลัมน์อื่นจะไม่หาย
อย่างในรูป

ถ้าเราคลิก แถวอื่นใน คอลัมน์ หรือในกรอบสี่เหลี่ยม ปุ่มจะหายครับ
แต่ถ้าเราคลิกที่ columnheader หรือ คอลัมน์อื่น หรือนอกกรอบสี่เหลี่ยม จะไม่หาย
ตามในรูปนี้ครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
#region _DataGridViewFilePathColumn1 1
/// <summary>
/// Hosts a collection of DataGridViewTextBoxCell cells.
/// </summary>
public class DataGridViewFilePathColumn1 : DataGridViewColumn
{
// public enum PathType { directory, file };
//public PathType pathtype = PathType.file;
public DataGridViewFilePathColumn1()
: base(new DataGridViewFilePathCell1())
{
}
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
{
return (System.IO.Directory.Exists(_Path) || !string.IsNullOrEmpty(_Path)) ? _Path : Application.StartupPath;
}
set
{
_Path = value;
}
}
public override object Clone()
{
var clm = base.Clone() as DataGridViewFilePathColumn1;
DataGridViewFilePathColumn1 xxx = base.Clone() as DataGridViewFilePathColumn1;
if (clm != null)
{
clm.Directory = _Path;
}
return clm;
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (null != value &&
!value.GetType().IsAssignableFrom(typeof(DataGridViewFilePathCell1)))
{
throw new InvalidCastException("must be a DataGridViewFilePathCell1");
}
base.CellTemplate = value;
}
}
}
/// <summary>
/// Displays editable text information in a DataGridView control. Uses
/// PathEllipsis formatting if the column is smaller than the width of a
/// displayed filesystem path.
/// </summary>
public class DataGridViewFilePathCell1 : DataGridViewTextBoxCell
{
Button browseButton;
Dictionary<Color, SolidBrush> brushes = new Dictionary<Color, SolidBrush>();
protected virtual SolidBrush GetCachedBrush(Color color)
{
if (this.brushes.ContainsKey(color))
return this.brushes[color];
SolidBrush brush = new SolidBrush(color);
this.brushes.Add(color, brush);
return brush;
}
protected virtual bool RightToLeftInternal
{
get
{
return this.DataGridView.RightToLeft == RightToLeft.Yes;
}
}
protected override void OnContentClick(DataGridViewCellEventArgs e)
{
base.OnContentClick(e);
if (this.RowIndex < 0) return;
if (this.DataGridView.CurrentCell == this)
{
DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
string file = Convert.ToString(filePathColumn.Directory + "\\" + base.Value);
if (!System.IO.File.Exists(file))
{
MessageBox.Show("ไม่พบไฟล์ " + file);
}
else
{
System.Diagnostics.Process.Start(file);
}
}
}
protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
base.OnLeave(rowIndex, throughMouseClick);
browseButton.Hide();
}
protected override void OnEnter(int rowIndex, bool throughMouseClick)
{
browseButton.Hide();
base.OnEnter(rowIndex, throughMouseClick);
if (rowIndex < 0) return;
Rectangle Loc;
int Wid;
Loc = this.DataGridView.GetCellDisplayRectangle(ColumnIndex, rowIndex, false);
Wid = this.DataGridView.CurrentCell.Size.Width;
browseButton.Location = new Point(Loc.X - 25 + Wid, Loc.Y - 4);
browseButton.Height = this.DataGridView.CurrentCell.Size.Height + 4;
browseButton.Width = browseButton.Height;
if (!this.DataGridView.Controls.Contains(browseButton))
this.DataGridView.Controls.Add(browseButton);
browseButton.Show();
}
public bool ShowFocusCues
{
get { return true; }
}
protected bool ApplyVisualStylesToHeaders
{
get
{
if (Application.RenderWithVisualStyles)
{
return this.DataGridView.EnableHeadersVisualStyles;
}
return false;
}
}
void browseButton_Click(object sender, EventArgs e)
{
DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
SaveFileDialog dialog1 = new SaveFileDialog();
dialog1.InitialDirectory = (!string.IsNullOrEmpty(filePathColumn.Directory)) ? filePathColumn.Directory : "C:\\";
dialog1.FileName = System.IO.Path.GetFileName(dialog.FileName);
DialogResult dl = dialog1.ShowDialog();
if (dl == DialogResult.OK)
{
string f = (string.IsNullOrEmpty(System.IO.Path.GetExtension(dialog1.FileName))) ? dialog1.FileName + System.IO.Path.GetExtension(dialog.FileName) : dialog1.FileName;
base.Value = f;
if (dialog.FileName != dialog1.FileName)
System.IO.File.Copy(dialog.FileName, f, true);
}
}
}
public DataGridViewFilePathCell1()
: base()
{
browseButton = new Button();
// browseButton.FlatStyle = FlatStyle.Flat;
browseButton.FlatAppearance.BorderSize = 0;
browseButton.Size = new Size(30, 25);
browseButton.ImageAlign = ContentAlignment.MiddleCenter;
browseButton.FlatAppearance.MouseDownBackColor = Color.Transparent;
browseButton.FlatAppearance.MouseOverBackColor = Color.Transparent;
browseButton.BackColor = Color.Transparent;
browseButton.Image = Properties.Resources.finds;
browseButton.Hide();
browseButton.Click += new EventHandler(browseButton_Click);
}
}
#endregion
}
Tag : .NET, Win (Windows App), C#
|
|
 |
 |
 |
 |
Date :
2018-04-18 16:30:26 |
By :
lamaka.tor |
View :
1378 |
Reply :
11 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค้ดมีรันตรง
Code (C#)
protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
base.OnLeave(rowIndex, throughMouseClick);
browseButton.Hide();
}
แต่ปุ่มก็ยังไม่หาย ครับ
|
 |
 |
 |
 |
Date :
2018-04-18 16:33:09 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ได้แล้วครับ
เปลี่ยนจาก
Code (C#)
if (rowIndex < 0) return;
ใน
Code (C#)
protected override void OnEnter
เป็น
Code (C#)
if (RowIndex < 0) return;
จากที่ดู เหมือนว่า ถ้าเราคลิก columnheader ตัว rowIndex จะยังจำค่าเดิมก่อนหน้านั้นไว้อยู่ เช่น แถวที่ 2 แถวที่ 3
แต่ RowIndex จะใช้ค่าจริงคือ -1 นั่นเอง
เป็นความรู้ใหม่เลยครับ
|
 |
 |
 |
 |
Date :
2018-04-19 09:20:58 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปัญหาที่ว่า เวลาเรา ลากเมาเพื่อขยายขนาดของ คอลัมน์ แล้ว ปุ่มไม่ย้ายตามไปด้วย(มันยังอยู่ตำแน่งเดิม) ก็ยังคงอยู่
ไม่ทราบว่ามีวิธีแก้ไม๊ครับ
|
 |
 |
 |
 |
Date :
2018-04-19 11:13:06 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บ่แน่ใจว่า ถ้าดักตอนนี้เพิ่มแล้วสิหายบ่เด้อครับผม
AllowUserToResizeColumnsChanged
|
 |
 |
 |
 |
Date :
2018-04-19 13:33:31 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังมีอีกจุดที่มีปัญหาคือ เวลาเราคลิกที่ปุ่ม
จะไม่เป็นการ Edit เหมือนที่เราพิมพ์ที่ cell ทำให้ไม่มีการเพิ่มแถว

อยากให้หลังจากที่เรากดปุ่มเพื่อเลือกไฟล์เสร็จแล้วให้เป็นเหมือนการ Edit อ่าครับ

|
 |
 |
 |
 |
Date :
2018-04-19 14:30:21 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูอันนี้ไหมครับ
มี source code ให้ด้วย
https://stackoverflow.com/questions/22045297/how-to-add-ellipse-button-and-textbox-in-current-cell-of-datagridview-in-winform
|
 |
 |
 |
 |
Date :
2018-04-19 17:35:02 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Edit เหมือนที่เราพิมพ์ที่ cell ทำให้มีการเพิ่มแถวนั้นทำได้โดยการเพิ่ม คลาส IDataGridViewEditingControl ขึ้นมาครับ
และจะต้องเพิ่ม คลาส TextButton เพื่อมารองรับ IDataGridViewEditingControl ตามที่ท่าน fonfire ให้มาใน No. 9
Code (C#)
#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
}
แต่ ครับแต่
พอผมเอามาทำ ปุ่มเล็กยังกะ....มดเลยครับ

จากปกติที่มันจะใหญ่กว่า cell แบบนี้

ผมเกรงว่าถ้าเอาเจ้า ... มดไปให้ user ใช้เขาจะด่าเอา
ไม่ทราบว่า พอมีวิธี ทำให้มันใหญ่กว่า cell ได้ไม๊ครับ
|
 |
 |
 |
 |
Date :
2018-04-20 18:35:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค้ดล่าสุด สำหรับ DataGridViewFilePathColumn ครับ
ยังติดปัญหาอยู่ว่า เวลาเราขยายขนาดคอลัมน์ ปุ่มก็ยังไม่หายซักที(นั่งแก้แล้วแก้อีก 5555)
Code (C#)
#region _DataGridViewFilePathColumn1 1
/// <summary>
/// Hosts a collection of DataGridViewTextBoxCell cells.
/// </summary>
public class DataGridViewFilePathColumn1 : DataGridViewColumn
{
// public enum PathType { directory, file };
//public PathType pathtype = PathType.file;
public DataGridViewFilePathColumn1()
: base(new DataGridViewFilePathCell1())
{
}
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
{
return (System.IO.Directory.Exists(_Path) || !string.IsNullOrEmpty(_Path)) ? _Path : Application.StartupPath;
}
set
{
_Path = value;
}
}
public override object Clone()
{
var clm = base.Clone() as DataGridViewFilePathColumn1;
DataGridViewFilePathColumn1 xxx = base.Clone() as DataGridViewFilePathColumn1;
if (clm != null)
{
clm.Directory = _Path;
}
return clm;
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (null != value &&
!value.GetType().IsAssignableFrom(typeof(DataGridViewFilePathCell1)))
{
throw new InvalidCastException("must be a DataGridViewFilePathCell1");
}
base.CellTemplate = value;
}
}
}
/// <summary>
/// Displays editable text information in a DataGridView control. Uses
/// PathEllipsis formatting if the column is smaller than the width of a
/// displayed filesystem path.
/// </summary>
public class DataGridViewFilePathCell1 : DataGridViewLinkCell
{
Button browseButton;
Dictionary<Color, SolidBrush> brushes = new Dictionary<Color, SolidBrush>();
protected virtual SolidBrush GetCachedBrush(Color color)
{
if (this.brushes.ContainsKey(color))
return this.brushes[color];
SolidBrush brush = new SolidBrush(color);
this.brushes.Add(color, brush);
return brush;
}
protected virtual bool RightToLeftInternal
{
get
{
return this.DataGridView.RightToLeft == RightToLeft.Yes;
}
}
protected override void OnContentClick(DataGridViewCellEventArgs e)
{
base.OnContentClick(e);
browseButton.Hide();
if (this.RowIndex < 0) return;
if (this.DataGridView.CurrentCell == this)
{
DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
string file = Convert.ToString(filePathColumn.Directory + "\\" + base.Value);
if (!System.IO.File.Exists(file))
{
MessageBox.Show("ไม่พบไฟล์ " + file);
}
else
{
System.Diagnostics.Process.Start(file);
}
}
}
protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
base.OnLeave(rowIndex, throughMouseClick);
browseButton.Hide();
}
protected override void OnEnter(int rowIndex, bool throughMouseClick)
{
browseButton.Hide();
base.OnEnter(rowIndex, throughMouseClick);
if (RowIndex < 0) return;
Rectangle Loc;
int Wid;
Loc = this.DataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
Wid = this.DataGridView.CurrentCell.Size.Width;
browseButton.Location = new Point(Loc.X - 25 + Wid, Loc.Y - 4);
browseButton.Height = this.DataGridView.CurrentCell.Size.Height + 4;
browseButton.Width = browseButton.Height;
if (!this.DataGridView.Controls.Contains(browseButton))
this.DataGridView.Controls.Add(browseButton);
browseButton.Show();
}
public bool ShowFocusCues
{
get { return true; }
}
protected bool ApplyVisualStylesToHeaders
{
get
{
if (Application.RenderWithVisualStyles)
{
return this.DataGridView.EnableHeadersVisualStyles;
}
return false;
}
}
void browseButton_Click(object sender, EventArgs e)
{
DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = (!string.IsNullOrEmpty(filePathColumn.Directory)) ? filePathColumn.Directory : "C:\\";
if (dialog.ShowDialog() == DialogResult.OK)
{
SaveFileDialog dialog1 = new SaveFileDialog();
dialog1.InitialDirectory = (!string.IsNullOrEmpty(filePathColumn.Directory)) ? filePathColumn.Directory : "C:\\";
dialog1.FileName = System.IO.Path.GetFileName(dialog.FileName);
DialogResult dl = dialog1.ShowDialog();
if (dl == DialogResult.OK)
{
// string f = (string.IsNullOrEmpty(System.IO.Path.GetExtension(dialog1.FileName))) ? dialog1.FileName + System.IO.Path.GetExtension(dialog.FileName) : dialog1.FileName;
string f = System.IO.Path.GetFileName(dialog1.FileName);
base.Value = f;
this.DataGridView.NotifyCurrentCellDirty(true);
if (dialog.FileName != dialog1.FileName)
System.IO.File.Copy(dialog.FileName, f, true);
}
}
}
public DataGridViewFilePathCell1()
: base()
{
browseButton = new Button();
// browseButton.FlatStyle = FlatStyle.Flat;
browseButton.FlatAppearance.BorderSize = 0;
browseButton.Size = new Size(30, 25);
browseButton.ImageAlign = ContentAlignment.MiddleCenter;
browseButton.FlatAppearance.MouseDownBackColor = Color.Transparent;
browseButton.FlatAppearance.MouseOverBackColor = Color.Transparent;
browseButton.BackColor = Color.Transparent;
browseButton.Image = Properties.Resources.finds;
browseButton.Hide();
browseButton.Click += new EventHandler(browseButton_Click);
}
}
#endregion
|
 |
 |
 |
 |
Date :
2018-04-27 20:48:49 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|