Create a class for customize right click

Hi, i am quite new for C# in class oriented program. I am trying to create a class for customize right click for mouse. While trying to run the program the error message pop up. Please do advice.


Error pop ups : Argument Exception was unhandled in Program.cs. As below

namespace Class_Calculator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());   <<<<-------------------------Error pointed
        }
    }
}
////////////////////mouse.cs//////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Class_Calculator
{
    class mouse : Form
    {
        ContextMenuStrip mnu;
        ToolStripMenuItem mnuSwitch;
        ToolStripMenuItem mnuWire;
        ToolStripMenuItem mnuRelay;        

        public mouse()
        {
            this.mnu = new ContextMenuStrip();
            this.mnuSwitch = new ToolStripMenuItem("Switch");
            this.mnuWire = new ToolStripMenuItem("Wire");
            this.mnuRelay = new ToolStripMenuItem("Relay");


            mnuSwitch.Image = Image.FromFile(@"E:\C#\Uthaya Project\ico\ico\Switch.ico");
            mnuWire.Image = Image.FromFile(@"E:\C#\Uthaya Project\ico\ico\Wire.ico");
            mnuRelay.Image = Image.FromFile(@"E:\C#\Uthaya Project\ico\ico\Relay.ico");

            mnu.Items.AddRange(new ToolStripItem[] { mnuSwitch, mnuWire, mnuRelay }); 
        }
    }
}

///////////////////////Form1///////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Class_Calculator
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            this.ContextMenuStrip = mnu;
        }
    }
}

Open in new window

Rama TitoProgrammerAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of deriving from Form, just use a regular class.

Change:

    class mouse : Form

To:

    class mouse

Next, make sure your menu is publicly accessible.

Change:

        ContextMenuStrip mnu;

To:

        public ContextMenuStrip mnu;

Finally, you need to create an instance of mouse in your Form1:
public partial class Form1 : Form
    {
        
        private mouse myMouse = new mouse();

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            this.ContextMenuStrip = myMouse.mnu;
        }

    }

Open in new window


*I don't see any benefit in doing it this way...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
At line #60, how is Form1 getting a reference to "mnu", which is declared in a completely different class (mouse)?
0
 
Rama TitoProgrammerAuthor Commented:
Cheers, but i didn't get how to do please do assist further. TQ
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why are you using the separate class 'mouse'?...just put those declarations and code directly into Form1.
0
 
Rama TitoProgrammerAuthor Commented:
Hi I did that, its working fine, But i am just trying out how to do with class method.
0
 
nishant joshiTechnology Development ConsultantCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.