How do i make that only when select ContextMenu "Copy" option it will do something?

时间:2015-05-12 23:16:52

标签: c# .net winforms

In the constructor i did:

    NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);


    String employeeName;    
    float hoursWorked;          
    float hourlyRate;           
    float basepay;          
    float netpay;
    float overtime = 0;
    int numberofemp= 0;
    final JPanel panel = new JPanel();

        System.out.println( "Assignement 2\n\n");   
            numberofemp = Integer.parseInt(JOptionPane.showInputDialog("Enter No of Employee"));

     int[] arrayList= new int[numberofemp];
       for (int i = 0; i < arrayList.length; i++)

      {
             employeeName = JOptionPane.showInputDialog("Enter the employee name:");

       do
        {
           hourlyRate = Float.parseFloat(JOptionPane.showInputDialog("Enter the number of Hourly Rate :"));

           if (hourlyRate < 8)
                {   
                JOptionPane.showMessageDialog(panel, "The hourly rate must be 8 an hour", "Error", JOptionPane.ERROR_MESSAGE);
                }
              }
       while ( hourlyRate < 8 ); 

           do
             {
               hoursWorked = Float.parseFloat(JOptionPane.showInputDialog("Enter the number of hours worked:"));

            if (hoursWorked >60)
            {
                    JOptionPane.showMessageDialog(panel, "The number of hours worked must be under 60", "Error", JOptionPane.ERROR_MESSAGE);                     
            }
          }
        while ( hoursWorked >60);


                    if (hoursWorked > 40)
                        {
                        overtime = (float) (  (hoursWorked - 40) *(hourlyRate * 1.5));
                        }

                        else
                        { basepay = hoursWorked * hourlyRate;

                        }
        netpay  =  basepay + overtime;          

        System.out.println( "\n\nEmployee Name: " + employeeName);      
         System.out.println( "Hours Worked: " + hoursWorked );
         System.out.println( "Base Pay: " + currency.format(basepay) );
          System.out.println( "Overtime Pay: " + currency.format(overtime)); 
           System.out.println( "Total Pay: " + currency.format(netpay)); 
            System.out.println( );


 try
  {
     PrintWriter prst;
         prst = new PrintWriter( "C://Users/tushar/Desktop/Workspace/Assignment_2/out.txt" );
              prst.println( "\n\nEmployee Name: " + employeeName);
                prst.println( "\n\nBasePay: " + basepay);
                  prst.println( "\n\nOvertime Pay: " + overtime);
                    prst.println( "\n\nTotal pay: " + netpay);
                       prst.close();

                                     }
 catch (Exception e)
 {
         System.err.println ("Error writing to file");
 }                      



       }
     }  }

Then i have a ListView mouse click event:

m = new ContextMenu();
m.MenuItems.Add(new MenuItem("Cut"));
m.MenuItems.Add(new MenuItem("Copy"));
m.MenuItems.Add(new MenuItem("Paste"));

When i click on an item from the ListView and then make right click it's selecting the item subitem text and also copy it to the clipboard.

But when i make a right click it also show the ContextMeny options Cut Copy Paste.

I want to do when i make right click only when i click on the "Copy" option it will copy it to the clipboard.

I know that maybe i need to use ContextMenuStrip and create click event for it but then the click event of the ContextMenuStrip will not be connected with the ListView mouse click event.

1 个答案:

答案 0 :(得分:0)

Every control has a ContextMenu property. Try to just set that property on n to point to your context menu.

lstDisplayHardware

Of course, you want to do that after m = new ContextMenu(); m.MenuItems.Add(new MenuItem("Cut")); m.MenuItems.Add(new MenuItem("Copy")); m.MenuItems.Add(new MenuItem("Paste")); lstDisplayHardware.ContextMenu = m; is called.