Error with delegate instruction C#

I'm new on C# and i have many difulties, but now i'm still trying add an eventhandler to declared control like a variable, but if i use the delegate instruction i cant put this events because when i use the declaration:

  private delegate void  listView1_MouseUp(object sender, MouseEventArgs e)     

Many error appears in a procedure that i caught in this forum, but i will put all procedure to you see.

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

}

}

Thanks for your help!

Upvotes: 0

Views: 85

Answers (2)

KMoussa
KMoussa

Reputation: 1578

When you handle an event, you must supply a handler that matches the delegate type expected by this event. Loosley speaking, you need a method with the same signature expected by the handler.

For MouseUp, you need to supply a MouseEventHandler, see https://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventhandler(v=vs.110).aspx

You need to change the event subscription to

listView1.MouseUp += new MouseEventHandler(listView1_MouseUp);

Then change the signature of your handler to

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

Upvotes: 1

Claudiu Cojocaru
Claudiu Cojocaru

Reputation: 57

Just use this:

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

The delegate keyword doesn't belong in the method declaration, it's not valid in that context.

Upvotes: 1

Related Questions