我對C#新的,我有很多difulties,但現在我仍然試圖添加一個事件處理程序,以像變量聲明的控制,但如果我使用的委託指令,我不能把這個事件,因爲當我使用的聲明:出現在我陷入了這個論壇的程序錯誤與委託指令C#
private delegate void listView1_MouseUp(object sender, MouseEventArgs e)
許多錯誤,但我會把所有的程序給你看。
private void Form1_Load(object sender, EventArgs e)
{
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = false;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
//Hide Column Header
listView1.HeaderStyle = ColumnHeaderStyle.None;
// Create three items and three sets of subitems for each item.
ListViewItem[] ItemsView = new ListViewItem[Quant_Items];
while (Item_Number <= (Quant_Items - 1))
{
ItemsView[Item_Number] = new ListViewItem(Item_name + Item_Number);
while (Sub_Item <= (Quant_SubItems - 1))
{
ItemsView[Item_Number].SubItems.Add("SubItem" + Sub_Item);
Sub_Item++;
}
Item_Number++;
}
Sub_Item = 0;
while (Sub_Item <= (Quant_SubItems - 1))
{
listView1.Columns.Add("Coluna" + Sub_Item);
Sub_Item++;
}
//Add the items to the ListView.
listView1.Items.AddRange(ItemsView);
//Autosize ListView
listView1.Bounds = new Rectangle(new Point(10, 10), new Size(Quant_SubItems * 70, Quant_Items * 18));
// Add the ListView to the control collection.
this.Controls.Add(listView1);
listView1.MouseUp += new EventHandler(listView1_MouseUp);
}
//____________________________________________________________________
private delegate void listView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewHitTestInfo i = listView1.HitTest(e.X, e.Y);
SelectedLSI = i.SubItem;
if (SelectedLSI == null)
return;
int border = 0;
switch (listView1.BorderStyle)
{
case BorderStyle.FixedSingle:
border = 1;
break;
case BorderStyle.Fixed3D:
border = 2;
break;
}
int CellWidth = SelectedLSI.Bounds.Width;
int CellHeight = SelectedLSI.Bounds.Height;
int CellLeft = border + listView1.Left + i.SubItem.Bounds.Left;
int CellTop = listView1.Top + i.SubItem.Bounds.Top;
// First Column
if (i.SubItem == i.Item.SubItems[0])
CellWidth = listView1.Columns[0].Width;
TxtEdit.Location = new Point(CellLeft, CellTop);
TxtEdit.Size = new Size(CellWidth, CellHeight);
TxtEdit.Visible = true;
TxtEdit.BringToFront();
TxtEdit.Text = i.SubItem.Text;
TxtEdit.Select();
TxtEdit.SelectAll();
}
private void listView2_MouseDown(object sender, MouseEventArgs e)
{
HideTextEditor();
}
private void listView2_Scroll(object sender, EventArgs e)
{
HideTextEditor();
}
private void TxtEdit_Leave(object sender, EventArgs e)
{
HideTextEditor();
}
private void TxtEdit_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
HideTextEditor();
}
private void HideTextEditor()
{
TxtEdit.Visible = false;
if (SelectedLSI != null)
SelectedLSI.Text = TxtEdit.Text;
SelectedLSI = null;
TxtEdit.Text = "";
}
}
}
感謝您的幫助!
爲什麼你是否在該方法定義中放置了'delegate'?你期望做什麼? – CodeCaster
你的方法應該被定義爲'私人無效listView1_MouseUp(對象發件人,發送MouseEventArgs E)'和你一樣聯播事件'listView1.MouseUp + =新MouseEventHandler(listView1_MouseUp);' – Rob
CodeCaster:我希望創建一個事件控制listView1_MouseUp但如果我不使用委託此錯誤出現: 「沒有超載'listView1_MouseUp'匹配委託'EventHandler'\t WinFormsDemoApp」 所以,我在這個論壇看到解決這個錯誤的一種方法把委託在聲明事件。 –