120 lines
3.6 KiB
C#
120 lines
3.6 KiB
C#
using System.Windows.Forms;
|
|
using System.Drawing;
|
|
using Microsoft.VisualBasic;
|
|
using System;
|
|
|
|
namespace 精工涂胶检测项目
|
|
{
|
|
public class EditableListView : ListView
|
|
{
|
|
public EditableListView()
|
|
{
|
|
View = View.Details;
|
|
FullRowSelect = true;
|
|
GridLines = true;
|
|
LabelEdit = false;
|
|
Scrollable = true;
|
|
Columns.Add("地址", 120);
|
|
Columns.Add("数据类型", 120);
|
|
|
|
DoubleClick -= EditableListView_DoubleClick;
|
|
DoubleClick += EditableListView_DoubleClick;
|
|
}
|
|
|
|
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
|
|
protected override void WndProc(ref Message m)
|
|
{
|
|
const int WM_LBUTTONDBLCLK = 0x0203;
|
|
if (m.Msg == WM_LBUTTONDBLCLK)
|
|
{
|
|
OnDoubleClick(EventArgs.Empty);
|
|
}
|
|
base.WndProc(ref m);
|
|
}
|
|
|
|
// 添加数据的公共方法
|
|
public void AddData(string address, string dataType)
|
|
{
|
|
ListViewItem item = new ListViewItem(address);
|
|
item.SubItems.Add(dataType);
|
|
Items.Add(item);
|
|
}
|
|
|
|
private void EditableListView_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
var mousePos = this.PointToClient(Cursor.Position);
|
|
var hitInfo = this.HitTest(mousePos);
|
|
|
|
bool isRealBlankArea =
|
|
hitInfo.Item == null &&
|
|
hitInfo.SubItem == null &&
|
|
(hitInfo.Location == ListViewHitTestLocations.None ||
|
|
hitInfo.Location == ListViewHitTestLocations.AboveClientArea);
|
|
|
|
if (isRealBlankArea)
|
|
{
|
|
AddNewItem();
|
|
}
|
|
else if (hitInfo.Item != null)
|
|
{
|
|
int colIndex = GetColumnIndex(mousePos.X);
|
|
if (colIndex >= 0 && colIndex < Columns.Count)
|
|
{
|
|
EditItem(hitInfo.Item, colIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int GetColumnIndex(int x)
|
|
{
|
|
int accumulatedWidth = 0;
|
|
for (int i = 0; i < Columns.Count; i++)
|
|
{
|
|
accumulatedWidth += Columns[i].Width;
|
|
if (x < accumulatedWidth)
|
|
return i;
|
|
}
|
|
return Columns.Count - 1;
|
|
}
|
|
|
|
private void EditItem(ListViewItem item, int columnIndex)
|
|
{
|
|
string originalValue = columnIndex == 0 ? item.Text : item.SubItems[columnIndex].Text;
|
|
|
|
string newValue = Interaction.InputBox(
|
|
"请输入新值:",
|
|
"编辑单元格",
|
|
originalValue
|
|
);
|
|
|
|
if (!string.IsNullOrEmpty(newValue))
|
|
{
|
|
if (columnIndex == 0)
|
|
{
|
|
item.Text = newValue;
|
|
}
|
|
else
|
|
{
|
|
if (item.SubItems.Count > columnIndex)
|
|
{
|
|
item.SubItems[columnIndex].Text = newValue;
|
|
}
|
|
else
|
|
{
|
|
item.SubItems.Add(newValue);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddNewItem()
|
|
{
|
|
string address = Interaction.InputBox("请输入地址:", "添加新条目", "");
|
|
if (!string.IsNullOrEmpty(address))
|
|
{
|
|
string dataType = Interaction.InputBox("请输入数据类型:", "添加新条目", "");
|
|
AddData(address, dataType);
|
|
}
|
|
}
|
|
}
|
|
} |