Link to home
Start Free TrialLog in
Avatar of Rama Tito
Rama TitoFlag for Malaysia

asked on

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

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

At line #60, how is Form1 getting a reference to "mnu", which is declared in a completely different class (mouse)?
Avatar of Rama Tito

ASKER

Cheers, but i didn't get how to do please do assist further. TQ
Why are you using the separate class 'mouse'?...just put those declarations and code directly into Form1.
Hi I did that, its working fine, But i am just trying out how to do with class method.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial