2016-11-22 124 views
0

我對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 = ""; 
    } 

} 

}

感謝您的幫助!

+1

爲什麼你是否在該方法定義中放置了'delegate'?你期望做什麼? – CodeCaster

+1

你的方法應該被定義爲'私人無效listView1_MouseUp(對象發件人,發送MouseEventArgs E)'和你一樣聯播事件'listView1.MouseUp + =新MouseEventHandler(listView1_MouseUp);' – Rob

+0

CodeCaster:我希望創建一個事件控制listView1_MouseUp但如果我不使用委託此錯誤出現: 「沒有超載'listView1_MouseUp'匹配委託'EventHandler'\t WinFormsDemoApp」 所以,我在這個論壇看到解決這個錯誤的一種方法把委託在聲明事件。 –

回答

1

只要使用此:

private void listView1_MouseUp(object sender, MouseEventArgs e) 
{ 
    ... 
} 

delegate關鍵字不會在方法聲明屬於,它不是在這方面有效。

+0

您好,我有嘗試這種方式,但我怎麼previosly說錯誤,CS0123「不超載的‘listView1_MouseUp’匹配下放‘事件處理程序’\t WinFormsDemoApp」出現。 –

1

當你處理一個事件,你必須提供由該事件所期望的委託類型相匹配的處理程序。 Loosley說,你需要一個具有處理程序期望的相同簽名的方法。

對於MouseUp,您需要提供一個MouseEventHandler,看到https://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventhandler(v=vs.110).aspx

您需要將事件訂閱變更爲

listView1.MouseUp += new MouseEventHandler(listView1_MouseUp); 

然後處理程序的簽名更改爲

private void listView1_MouseUp(object sender, MouseEventArgs e) { /*..*/} 
+0

你好,我曾嘗試過這種方式,但我怎麼說previosly錯誤,CS0123「沒有超載'listView1_MouseUp'匹配委託'EventHandler'\t WinFormsDemoApp」,出現。 –

+0

您是否在回答中提到了我提到的第一個變更? '+ =新的MouseEventHandler..'? – KMoussa

+0

非常感謝,它工作! –