📄 ctreeviewcomboboxcell.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace QueryTool_V1
{
public class CDataGridViewTreeViewComboBoxEditingControl : ComboBox, IDataGridViewEditingControl
{
ToolStripControlHost treeViewHost;
System.Windows.Forms.ToolStripDropDown dropDown;
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
#region ImplementingDataGridViewEditingControl
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Text;
}
set
{
if (value is String)
{
this.Text = value.ToString();
}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
//this..CalendarForeColor = dataGridViewCellStyle.ForeColor;
//this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
//protected override void OnValueChanged(EventArgs eventargs)
//{
// // Notify the DataGridView that the contents of the cell
// // have changed.
// valueChanged = true;
// this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
// base.OnValueChanged(eventargs);
//}
#endregion
public CDataGridViewTreeViewComboBoxEditingControl()
{
TreeView treeView = new TreeView();
treeView.BorderStyle = BorderStyle.None;
treeViewHost = new ToolStripControlHost(treeView);
// create drop down and add it
dropDown = new System.Windows.Forms.ToolStripDropDown();
dropDown.Items.Add(treeViewHost);
}
public TreeView TreeView
{
get { return treeViewHost.Control as TreeView; }
}
private void ShowDropDown()
{
if (dropDown != null)
{
treeViewHost.Width = DropDownWidth;
treeViewHost.Height = DropDownHeight;
dropDown.Show(this, 0, this.Height);
}
}
private const int WM_USER = 0x0400,
WM_REFLECT = WM_USER + 0x1C00,
WM_COMMAND = 0x0111,
CBN_DROPDOWN = 7;
public static int HIWORD(int n)
{
return (n >> 16) & 0xffff;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == (WM_REFLECT + WM_COMMAND))
{
if (HIWORD((int)m.WParam) == CBN_DROPDOWN)
{
ShowDropDown();
return;
}
}
base.WndProc(ref m);
}
// Edit: 10:37, remember to dispose the dropdown as it's not in the control collection.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (dropDown != null)
{
dropDown.Dispose();
dropDown = null;
}
}
base.Dispose(disposing);
}
}
public class CDataGridViewTreeViewComboBoxCell : DataGridViewTextBoxCell
{
CDataGridViewTreeViewComboBoxEditingControl ctl;
public CDataGridViewTreeViewComboBoxCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
ctl =
DataGridView.EditingControl as CDataGridViewTreeViewComboBoxEditingControl;
ctl.TreeView.ShowLines = true;
ctl.TreeView.ShowPlusMinus = true;
}
public void Add(string XMLFile)
{
// Use the XMLReader class to read the XML File and populate the
// treeview
try
{
this.ctl.TreeView.Nodes.Clear();
this.ctl.TreeView.ShowLines = true;
this.ctl.TreeView.ShowPlusMinus = true;
//Event Handler
this.ctl.TreeView.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(TreeView_NodeMouseDoubleClick);
TreeNode RootNode = null;
XmlTextReader reader = null;
reader = new XmlTextReader(XMLFile);
reader.WhitespaceHandling = WhitespaceHandling.None;
string readerName = "";
bool start_node = false;
int depth = 0;
TreeNode WORKINGNODE = null;
RootNode = null;
TreeNode AttrNode = null;
TreeNode newNode = null;
bool bIsEmpty = false;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
readerName = reader.Name;
bIsEmpty = reader.IsEmptyElement;
if (!start_node)
{
start_node = true;
RootNode = this.ctl.TreeView.Nodes.Add(readerName);
//AssociateTag(RootNode, reader.LineNumber);
RootNode.SelectedImageIndex = 0;
RootNode.ImageIndex = 0;
continue;
}
depth = reader.Depth;
if (reader.IsStartElement() && depth == 1)
{
WORKINGNODE = RootNode.Nodes.Add(reader.Name);
//AssociateTag(WORKINGNODE, reader.LineNumber);
}
else
{
TreeNode parent = WORKINGNODE;
WORKINGNODE = parent.Nodes.Add(reader.Name);
//AssociateTag(WORKINGNODE, reader.LineNumber);
}
WORKINGNODE.SelectedImageIndex = 1;
WORKINGNODE.ImageIndex = 1;
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
string rValue = reader.Value.Replace("\r\n", " ");
AttrNode = WORKINGNODE.Nodes.Add(reader.Name);
// AttrNode = WORKINGNODE.Nodes.Add(reader.Name +"="+rValue);
//AssociateTag(AttrNode, reader.LineNumber);
AttrNode.SelectedImageIndex = 1;
AttrNode.ImageIndex = 1;
TreeNode tmp = AttrNode.Nodes.Add(rValue);
tmp.SelectedImageIndex = 2;
tmp.ImageIndex = 2;
//AssociateTag(tmp, reader.LineNumber);
AttrNode.SelectedImageIndex = 2;
AttrNode.ImageIndex = 2;
}
if (bIsEmpty)
WORKINGNODE = WORKINGNODE.Parent;
break;
case XmlNodeType.Text:
{
string rValue = reader.Value.Replace("\r\n", " ");
newNode = WORKINGNODE.Nodes.Add(rValue);
//AssociateTag(newNode, reader.LineNumber);
newNode.SelectedImageIndex = 2;
newNode.ImageIndex = 2;
}
break;
case XmlNodeType.EndElement:
WORKINGNODE = WORKINGNODE.Parent;
break;
}
}
reader.Close();
RootNode.Expand();
}
catch (Exception eee)
{
Console.WriteLine(eee.Message);
}
}
//event handler
public void TreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Nodes.Count == 0)
{
string fullPath = e.Node.FullPath;
string resultPath = string.Empty;
string[] chr = fullPath.Split("\\".ToCharArray());
for (int c = 1; c < chr.Length; c++)
resultPath += chr[c];
ctl.Text = resultPath;
ctl.EditingControlDataGridView.NotifyCurrentCellDirty(true);
ctl.EditingControlValueChanged = true;
}
}
public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(CDataGridViewTreeViewComboBoxEditingControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(string);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return string.Empty;
}
}
}
public class CDataGridViewTreeViewComboBoxColumn : DataGridViewColumn
{
public CDataGridViewTreeViewComboBoxColumn()
{
this.CellTemplate = new CDataGridViewTreeViewComboBoxCell();
this.ReadOnly = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -