0

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!

4
  • 1
    Why did you put delegate in that method definition? What do you expect that to do? Commented Nov 22, 2016 at 23:13
  • 1
    Your method should be defined as private void listView1_MouseUp(object sender, MouseEventArgs e) and you hookup the event like listView1.MouseUp += new MouseEventHandler(listView1_MouseUp); Commented Nov 22, 2016 at 23:14
  • CodeCaster: I expect create a control with a event listView1_MouseUp but if i not use Delegate this error appears: "No overload for 'listView1_MouseUp' matches delegate 'EventHandler' WinFormsDemoApp" So, i see in this forum one way of solve this error put delegate in a declaration event. Commented Nov 22, 2016 at 23:26
  • Rob, i had try this way but how i say previosly the error, CS0123 "No overload for 'listView1_MouseUp' matches delegate 'EventHandler' WinFormsDemoApp", appears. Commented Nov 22, 2016 at 23:31

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, i had try this way but how i say previosly the error, CS0123 "No overload for 'listView1_MouseUp' matches delegate 'EventHandler' WinFormsDemoApp", appears.
1

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) { /*..*/}

2 Comments

Hello, i had try this way but how i say previosly the error, CS0123 "No overload for 'listView1_MouseUp' matches delegate 'EventHandler' WinFormsDemoApp", appears.
Did you do the first change I mentioned in the answer ? += new MouseEventHandler..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.