Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

0
Rama Tito
Asked:
Rama Tito
  • 3
  • 2
1 Solution
 
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Why are you using the separate class 'mouse'?...just put those declarations and code directly into Form1.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Rama TitoProgrammerAuthor Commented:
Hi I did that, its working fine, But i am just trying out how to do with class method.
0
 
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
 
nishant joshiTechnology Development ConsultantCommented:
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now