📄 grid.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace bs
{
/// <summary>
/// Grid 的摘要说明。
/// </summary>
public class Grid : System.Windows.Forms.UserControl
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.TextBox textBox1;
private System.Collections.Hashtable ColumnMap = new Hashtable();
//public int NumColumns=10;
//public int NumRows=4;
private int CurrentRow = 0;
private int CurrentColumn = 0;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Grid()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
//NumColumns=8;
//NumColumns=4;
// TODO: Add any initialization after the InitForm call
InitializeGrid(100,6);
}
public Grid(int Rows,int Columns,string[] columnsNames)
{
int numRows=Rows;
int numColumns=Columns;
InitializeComponent();
InitializeGrid(numRows,numColumns);
}
public void InitializeGrid(int NumRows,int NumColumns)
{
for (int i = 0; i < NumRows; i++)
{
ListViewItem anItem = new ListViewItem(" ");
listView1.Items.Add(anItem);
for (int j = 0; j < NumColumns; j++)
{
anItem.SubItems.Add(" ");
}
}
for (int nCol=0; nCol < NumColumns; nCol++)
{
listView1.Columns.Add(" ", 25, HorizontalAlignment.Center);
}
PerformCellMoveByRow(CurrentRow, CurrentColumn);
textBox1.Visible = true;
}
/* public int numRows
{
get
{
return NumRows;
}
set
{
NumRows=value;
}
}
public int numColumns
{
get
{
return NumColumns;
}
set
{
NumColumns=value;
}
}*/
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.GridLines = true;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(248, 88);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
this.listView1.SizeChanged += new System.EventHandler(this.listView1_SizeChanged);
this.listView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseUp);
this.listView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listView1_KeyDown);
this.listView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseDown);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.textBox1.SizeChanged += new System.EventHandler(this.textBox1_SizeChanged);
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// Grid
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listView1);
this.Name = "Grid";
this.Size = new System.Drawing.Size(248, 88);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
[DispId(1)]
public void SetColumnName(int index, string aName)
{
if (index > listView1.Columns.Count+1)
{
MessageBox.Show("SetColumnName:Column out of range");
return; // precondition
}
listView1.Columns[index - 1].Text = aName;
ColumnMap[aName] = index;
}
[DispId(2)]
public void SetColumnWidth(int index, int theWidth)
{
if (index - 1 > listView1.Columns.Count)
{
MessageBox.Show("SetColumnWidth:Column out of range");
return; // precondition
}
listView1.Columns[index - 1].Width = theWidth * (int)listView1.Font.SizeInPoints;
PerformCellMoveByRow(CurrentRow, CurrentColumn);
}
public void SetColumnWidthAndIgnoreFont(int index, int theWidth)
{
if (index - 1 > listView1.Columns.Count)
{
MessageBox.Show("SetColumnWidth:Column out of range");
return; // precondition
}
listView1.Columns[index - 1].Width = theWidth;
PerformCellMoveByRow(CurrentRow, CurrentColumn);
}
[DispId(3)]
public void SetColumnNames(string[] columnNames)
{
for (int i = 0; i < columnNames.Length ; i++)
{
if (i >= listView1.Columns.Count)
{
listView1.Columns.Add(columnNames[i], columnNames[i].Length * (int)listView1.Font.SizeInPoints, HorizontalAlignment.Center);
}
else
{
listView1.Columns[i].Text = columnNames[i];
//listView1.Columns[i].t
listView1.Columns[i].Width = columnNames[i].Length * (int)Font.SizeInPoints;
}
ColumnMap[columnNames[i]] = i + 1;
}
}
[DispId(4)]
public void SetColumnWidths(int[] columnWidths)
{
for (int i = 0; i < columnWidths.Length ; i++)
{
if (i >= listView1.Columns.Count)
{
listView1.Columns.Add(" ", columnWidths[i] * (int)listView1.Font.SizeInPoints, HorizontalAlignment.Center);
}
else
{
listView1.Columns[i].Width = columnWidths[i] * (int)Font.SizeInPoints;
}
}
PerformCellMoveByRow(CurrentRow, CurrentColumn);
}
[DispId(5)]
public void SetCell(int aRow, int aColumn, string text)
{
if (aRow > listView1.Items.Count) // precondition
{
MessageBox.Show("SetCell:Row out of range");
return;
}
if (aColumn > listView1.Columns.Count) // precondition
{
MessageBox.Show("SetCell:Column out of range");
return;
}
if (aColumn - 1 == 0)
{
listView1.Items[aRow - 1].Text = text;
}
else
{
listView1.Items[aRow-1].SubItems[aColumn - 1].Text = text;
}
}
[DispId(6)]
public string GetCell(int aRow, int aColumn)
{
try
{
if (aColumn == 1)
{
return listView1.Items[aRow - 1].Text;
//listView1.Items.
}
return listView1.Items[aRow - 1].SubItems[aColumn - 1].Text;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return " ";
}
}
[DispId(7)]
public void SetFullRow(int nRow, string[] rowData)
{
for (int i = 0; i <rowData.Length ; i++)
{
SetCell(nRow, i+1, rowData[i]);
}
}
[DispId(8)]
public void SetFullColumn(int nColumn, string[] columnData)
{
for (int i = 0; i < columnData.Length ; i++)
{
SetCell(i, nColumn, columnData[i]);
}
}
private Rectangle RetrieveCellBounds(int x, int y, ref ListViewItem rowItem, ref int index)
{
Rectangle r = new Rectangle(0, 0, 10, 10);
Point p = new Point(x, y);
// p = listView1.PointToScreen(p);
ListViewItem theCellRow = listView1.GetItemAt(3, p.Y);
rowItem = theCellRow;
if (theCellRow != null)
{
Rectangle rowRect = theCellRow.GetBounds(ItemBoundsPortion.ItemOnly);
int sum = 0;
for (int i = 0; i < listView1.Columns.Count; i++)
{
ColumnHeader ch = listView1.Columns[i];
r = new Rectangle(rowRect.Left + sum, rowRect.Top, ch.Width, rowRect.Height);
if (r.Contains(p))
{
index = i;
return r;
}
sum += listView1.Columns[i].Width;
}
}
return r;
}//节点的矩阵
void PopulateTextBox(ListViewItem theRow, int rowNum)
{
ListViewItem.ListViewSubItem theCell = theRow.SubItems[rowNum];
textBox1.Text = theCell.Text;
textBox1.BackColor = theCell.BackColor;
textBox1.ForeColor = theCell.ForeColor;
}
void PopulateCell(int rowNum, int colNum)
{
if ((rowNum > -1) && (colNum > -1))
{
ListViewItem theRow = listView1.Items[rowNum];
ListViewItem.ListViewSubItem theCell = theRow.SubItems[colNum];
theCell.Text = textBox1.Text;
//MessageBox.Show(textBox1.Text);
}
}
private void PerformCellMove(int x, int y)
{
textBox1.Visible = true;
ListViewItem theRow = null;
int index = 0;
Rectangle r = RetrieveCellBounds(x, y, ref theRow, ref index);
textBox1.SetBounds(r.X + 2, r.Y, r.Width, r.Height - 5);
if (theRow != null)
{
CurrentColumn = index;
CurrentRow = theRow.Index;
PopulateTextBox(theRow, index);
}
}
int GetLeftPosition(int col)
{
int sum = 0;
for (int i = 0; i < col; i++)
{
sum += listView1.Columns[i].Width;
}
return sum;
}
private void PerformCellMoveByRow(int row, int col)
{
textBox1.Visible = true;
listView1.Focus();
ListViewItem theRow = listView1.Items[row];
int index = col;
Rectangle r = listView1.Items[row].Bounds;
r.X = GetLeftPosition(col);
r.Width = listView1.Columns[col].Width;
textBox1.SetBounds(r.X + 2, r.Y, r.Width, r.Height - 5);
if (theRow != null)
{
CurrentColumn = index;
CurrentRow = theRow.Index;
PopulateTextBox(theRow, index);
}
}
private void MoveCell(KeyEventArgs result)
{
string c = result.KeyData.ToString();
switch (c)
{
case "Left":
if (CurrentColumn - 1 >= 0)
{
PerformCellMoveByRow(CurrentRow, CurrentColumn - 1);
}
break;
case "Right":
if (CurrentColumn + 1 < listView1.Columns.Count)
{
PerformCellMoveByRow(CurrentRow, CurrentColumn + 1);
}
break;
case "Up":
if (CurrentRow - 1 >= 0)
{
PerformCellMoveByRow(CurrentRow - 1, CurrentColumn);
}
break;
case "Down":
if (CurrentRow + 1 < listView1.Items.Count)
{
PerformCellMoveByRow(CurrentRow + 1, CurrentColumn);
}
break;
default:
// MessageBox.Show(result.KeyValue.ToString());
if (textBox1.Focused == false)
{
if (result.KeyValue >=(int)'A' && result.KeyValue <= (int)'Z')
{
if (result.Shift == false)
{
textBox1.Text = ((char) ((int)c[0] - (int)'A' + (int)'a')).ToString();
}
else
{
textBox1.Text = c;
}
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 1);
}
break;
}
}
[DispId(9)]
public void SetCell(int aRow, string strColumn, string text)
{
if (ColumnMap[strColumn] == null)
return;
int aColumn = (int)ColumnMap[strColumn];
SetCell(aRow, aColumn, text);
}
[DispId(10)]
public string GetCell(int aRow, string strColumn)
{
if (ColumnMap[strColumn] == null)
return "";
int aColumn = (int)ColumnMap[strColumn];
return GetCell(aRow, aColumn);
}
[DispId(11)]
public void SetCellColor(int rowNum, int colNum, Color aColor)
{
ListViewItem theRow = listView1.Items[rowNum - 1];
theRow.UseItemStyleForSubItems = false;
ListViewItem.ListViewSubItem theCell = theRow.SubItems[colNum - 1];
theCell.BackColor = aColor;
}
[DispId(12)]
public void SetCellTextColor(int rowNum, int colNum, Color aColor)
{
ListViewItem theRow = listView1.Items[rowNum - 1];
theRow.UseItemStyleForSubItems = false;
ListViewItem.ListViewSubItem theCell = theRow.SubItems[colNum - 1];
theCell.ForeColor = aColor;
}
[DispId(13)]
public void SetColumnWidth(string strColumn, int theWidth)
{
if (ColumnMap[strColumn] == null)
return;
int index = (int)ColumnMap[strColumn];
SetColumnWidth(index, theWidth);
}
[DispId(14)]
public void SetCellColor(int rowNum, string strColumn, Color aColor)
{
if (ColumnMap[strColumn] == null)
return;
int colNum = (int)ColumnMap[strColumn];
SetCellColor(rowNum, colNum, aColor);
}
[DispId(15)]
public void SetCellTextColor(int rowNum, string strColumn, Color aColor)
{
if (ColumnMap[strColumn] == null)
return;
int colNum = (int)ColumnMap[strColumn];
SetCellTextColor(rowNum, colNum, aColor);
}
private int SumColumnWidths()
{
int sum = 0;
for (int i = 0; i < listView1.Columns.Count; i++)
{
sum += listView1.Columns[i].Width;
}
return sum;
}
[DispId(16)]
public void SizeToFitGrid()
{
Rectangle rtList = listView1.Bounds;
Rectangle rect = listView1.Items[0].GetBounds(ItemBoundsPortion.Entire );
rect.Width = SumColumnWidths() + 20;
rect.Height = (listView1.Items.Count * rect.Height) + rect.Height + 10;
SetBounds(this.Bounds.X, this.Bounds.Y, rect.Width, rect.Height);
listView1.SetBounds(0, 0, rect.Width, rect.Height);
}
private void listView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
string result = e.KeyData.ToString();
MoveCell(e);
}
private void listView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
PerformCellMove(e.X, e.Y);
}
private void listView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void listView1_SizeChanged(object sender, System.EventArgs e)
{
listView1.Width = this.Width;
listView1.Height = this.Height;
}
private void textBox1_SizeChanged(object sender, System.EventArgs e)
{
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
MoveCell(e);
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
PopulateCell(CurrentRow, CurrentColumn);
//this.SetCellColor(CurrentRow+1,CurrentColumn+1,Color.Red);
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -