1. C # crea un proyecto winform;
2. Agregue la herramienta listview1 en la interfaz;
3. Haga clic con el botón derecho en listview1 y cambie las propiedades:
FullRowSelect = True activa el modo de selección de filas, solo cuando está activado puede hacer doble clic en las otras columnas de la primera columna
HeaderStyle = Nonclickable no responde a los clics del mouse en los encabezados de columna
MultiSelect = True permite múltiples selecciones
view = Detalles
4. Inicialice listview1, agregue el encabezado
public void InitList ()
{ ImageList imglist = new ImageList (); imglist.ImageSize = nuevo Tamaño (1, 20); listView1.SmallImageList = imglist;
listView1.View = Ver.Detalles;
this.listView1.Columns.Add ("序号", 40, HorizontalAlignment.Left);
this.listView1.Columns.Add ("列表 1", 120, HorizontalAlignment.Left);
this.listView1.Columns.Add ("列表 2", listView1.Width - 185, HorizontalAlignment.Left);
}
5. Agregar interfaz de función y control de cuadro de texto personalizado
[DllImport ("user32")]
public static extern int GetScrollPos (int hwnd, int nBar);
Private TextBox txtInput;
6. Seleccione el evento listview1:
Agregar evento de doble clic del mouse
private void listView1_MouseDoubleClick (remitente del objeto, MouseEventArgs e)
{ try { ListViewItem item = this.listView1.GetItemAt (eX, eY); // Busque el cuadro de texto Rectangle rect = item.GetBounds (ItemBoundsPortion.Entire); int StartX = rect. Izquierda; // Obtener la coordenada X de la posición del cuadro de texto int ColumnIndex = 0; // El índice del cuadro de texto
// Obtener el índice de la columna // Obtener
la posición del control deslizante
int pos = GetScrollPos (this.listView1.Handle.ToInt32 (), 0);
foreach (ColumnHeader Column in listView1.Columns)
{ if (eX + pos> = StartX + Column.Width) { StartX + = Column.Width; ColumnIndex + = 1; } }
if (ColumnIndex <this.listView1.Columns.Count-2) // La primera columna es el número de serie y no se modifica. Si el doble clic es la primera columna, no puede ingresar para modificar
{ return; }
this.txtInput = nuevo TextBox ();
// localiza el txtinput y lo esconde. txtInput 为 TextBox
this.txtInput.Parent = this.listView1;
// comenzar a editar
if (item! = null)
{ nDClick = this.listView1.Items [listView1.SelectedIndices [0]]. Index; rect.X = StartX; rect.Width = this.listView1.Columns [ColumnIndex] .Width; // 得到 长度 和 ListView 的 列 的 长度 相同 this.txtInput.Bounds = rect; this.txtInput.Multiline = true; // 显示 文本 框 this.txtInput.Text = item.SubItems [ColumnIndex] .Text; this.txtInput.Tag = item.SubItems [ColumnIndex]; this.txtInput.KeyPress + = nuevo KeyPressEventHandler (txtInput_KeyPress);
this.txtInput.Focus ();
}
}
catch (Exception ex)
{
}
}
// 添加 回车 保存 内容
private void txtInput_KeyPress (remitente del objeto, KeyPressEventArgs e)
{ try { if ((int) e.KeyChar == 13) { if (this.txtInput! = Null) { ListViewItem.ListViewSubItem lvst = (ListViewItem .ListViewSubItem) this.txtInput.Tag;
// Verifica si PN existe
int Index = 0;
if (CheckPnIsExist (this.txtInput.Text, ref Index))
{ this.txtInput.Text = ""; MessageBox.Show ("第" + (Index + 1) + " el mismo número de PN ya existe en la línea, entre de nuevo ");! } demás { lvst.Text = this.txtInput.Text; }
this.txtInput.Dispose ();
}
}
}
catch (Exception ex)
{
}
}
// 添加 事件 SelectedIndexChanged , 释放 文本 框 内容
private void listView1_SelectedIndexChanged (objeto remitente, EventArgs e)
{ try { if (this.txtInput! = Null) { if (this.txtInput.Text.Length> 0) { ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem) this.txtInput.Tag; // 检查 PN 是否 存在 int Índice = 0; if (CheckPnIsExist (this.txtInput.Text, índice de referencia)) { this.txtInput.Text = "";
MessageBox.Show (El mismo número PN ya existe en la "primera" + (Índice + 1) + "línea, por favor vuelva a entrar!");
}
Demás
{ lvst.Text = this.txtInput.Text; } }
this.txtInput.Dispose ();
}
}
catch (Exception ex)
{
}
}
// Agregar evento, hacer clic con el mouse MouseClick, el contenido del cuadro de texto se mostrará en listview1
private void listView1_MouseClick (remitente del objeto, MouseEventArgs e)
{ try { if (this.txtInput! = null) { if (this.txtInput.Text.Length> 0) { ListViewItem.ListViewSubItem lvst = (ListViewItem.ListViewSubItem) this.txtInput. Etiqueta;
// Verifica si PN existe
int Index = 0;
if (CheckPnIsExist (this.txtInput.Text, ref Index))
{ this.txtInput.Text = ""; MessageBox.Show ("第" + (Index + 1) + " el mismo número de PN ya existe en la línea, por favor vuelva a entrar ");! } demás { lvst.Text = this.txtInput.Text; } }
this.txtInput.Dispose ();
}
}
catch (Exception ex)
{
}
}