How to create Cut,Copy,Paste in contextmenu of lisview in Winform c#

sddd1

Member
Joined
Jul 22, 2021
Messages
14
Programming Experience
1-3
I want to create dual context menu in c# , And Click the item I want to add Cut, Copy, Paste

  1. private void listView1_MouseDown(object sender, MouseEventArgs e)
  2. {
  3. bool match = false;

  4. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  5. {
  6. foreach (ListViewItem item in listView1.Items)
  7. {
  8. if (item.Bounds.Contains(new Point(e.X, e.Y)))
  9. {
  10. MenuItem[] mi = new MenuItem[] { new MenuItem("Hello"), new MenuItem("World"), new MenuItem(item.Name) };
  11. listView1.ContextMenu = new ContextMenu(mi);
  12. match = true;
  13. break;
  14. }
  15. }
  16. if (match)
  17. {
  18. listView1.ContextMenu.Show(listView1, new Point(e.X, e.Y));
  19. }
  20. else
  21. {
  22. //Show listViews context menu
  23. }

  24. }
 

Attachments

  • mXPex.jpg
    mXPex.jpg
    14.3 KB · Views: 12
In the future, please post your code in code tags, not as numbered list. It makes reading your code very hard, and I'm loathed to edit your post just to fix it so that it uses code tags properly.
 
What's the actual problem here? You have referred to two unrelated things. Creating a context menu and acting when a menu item is clicked has nothing specifically to do with copy and paste so if you want to create a context menu then learn how to create a context menu. Copying and pasting has nothing to do with context menus so if you want to copy and paste then learn how to copy and paste. Once you can do both, you simply put it all together and you have one solution that does both. If you encounter a specific issue along the way then by all means, ask about that specific issue. It needs to be specific though, which this is not. Your question is like asking how to drive to someone's house and then bake them a cake. Driving and baking cakes are unrelated so do not belong in the same question. Same here.
 
For instance, you could practice using context menus by displaying a message box or changing the form colour when the user clicks an item, so nothing to do with copy and paste. You could copy and paste when clicking a button, so nothing to do with context menus. This is how you need to start thinking to be an effective developer. You need to break a problem down into the smallest parts possible, address each part individually and then combine the results. If you encounter an issue along then way then you only need to ask about the specific part you're working on. It can be good to describe the bigger problem for context but the smaller, specific problem is what you and we should be able to concentrate on.
 
Back
Top Bottom