I have created a picture viewer in C# but it's giving 2 errors, i don't know how to deal with them. I'm at beginner level in C#. So please help me in this, I have written the code and errors below, you can look at that.

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

namespace WindowsApplication1 
    public partial class Myform : Form 
        private System.Windows.Forms.MenuItem menuFile; 
        private System.Windows.Forms.MenuItem menuLoad; 
        private System.Windows.Forms.MenuItem menuImage; 
        private System.Windows.Forms.MenuItem menuExit; 
        private System.Windows.Forms.MenuItem menuStretch; 
        private System.Windows.Forms.MenuItem menuView; 

        private PictureBox pboxPhoto; 

        private void InitializeComponent() 
            this.menuFile = new System.Windows.Forms.MenuItem(); 
            this.menuLoad = new System.Windows.Forms.MenuItem(); 
            this.menuImage = new System.Windows.Forms.MenuItem(); 
            this.menuExit = new System.Windows.Forms.MenuItem(); 
            this.menuStretch = new System.Windows.Forms.MenuItem(); 
            this.menuExit = new System.Windows.Forms.MenuItem(); 
            this.menuView = new System.Windows.Forms.MenuItem(); 

            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuFile }); 
            this.menuFile.Index = 0; 
            this.menuFile.Text = "&File"; 
            this.menuLoad.Index = 0; 
            this.menuLoad.Shortcut = System.Windows.Forms.Shortcut.Ctrl; 
            this.menuLoad.Text = "&Load"; 
            this.menuExit.Index = 1; 
            this.menuExit.Text = "&Exit"; 

            menuView.Index = 1; 
            menuView.Text = "&View"; 


        public Myform() 
            this.Text = "Picture viewer"; 
            this.MinimumSize = new Size(200, 200); 

            // Create and configure the PictureBox 
            pboxPhoto = new PictureBox(); 
            pboxPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
            pboxPhoto.Width = this.Width; 
            pboxPhoto.Height = this.Height; 
            pboxPhoto.Left = (this.Width - pboxPhoto.Width) / 2; 
            pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2; 
            pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage; 
            pboxPhoto.Anchor = AnchorStyles.Top | AnchorStyles.Bottom 
            | AnchorStyles.Left | AnchorStyles.Right; 
            // Add our new controls to the Form 


        private void Form1_Load(object sender, EventArgs e) 

        protected void menuLoad_Click(object sender, System.EventArgs e) 
            menuLoad.Click += new System.EventHandler(this.menuLoad_Click); 

            OpenFileDialog dlg = new OpenFileDialog(); 
            dlg.Title = "Open Photo"; 
            dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*"; 
            if (dlg.ShowDialog() == DialogResult.OK) 
                    pbxPhoto.Image = new Bitmap(dlg.OpenFile()); 
                catch (Exception ex) 
                    MessageBox.Show("Unable to load file: " + ex.Message); 


        protected void menuExit_Click(object sender, System.EventArgs e) 
        private PictureBoxSizeMode[] modeMenuArray = 

Error Message 1: Type 'WindowsApplication1.Myform' already defines a member called 'InitializeComponent' with the same parameter types
Error message 2: The type 'WindowsApplication1.Myform' already contains a definition for 'menuFile'

Can anyone help me in this code:(

Thanks a lot ..

9 Years
Discussion Span
Last Post by f1 fan

i tried to remove my errors by myself and finally i am at one last error as i replaced this code

private PictureBoxSizeMode[] modeMenuArray = 

by private PictureBoxSizeMode[] modeMenuArray=null; and finally added this also in the myform class protected override void Dispose(bool dispose){base.Dispose(dispose);}

'WindowsApplication1.Myform.Dispose(bool)' hides inherited member 'System.Windows.Forms.Form.Dispose(bool)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Thanks, as i am trying to get it solve it very quickly as i can.


post your current code for the entire file that is throwing the errors.

please wrap using

[ code="csharp" ]
[ /code ]



It seems you have copied (or used) a VS 2003 application in VS2005. There were some subtle changes when VS2005 was introduced which included Partial Classes.

Previously when creating a windows form in VS2003 you had 2 files created - the designer and the code file (plus resx but we will leave that as it didnt change). When you dropped a component on the VS2003 it added all the code to create that component in the InitialiseComponent() method in the cs code file. There used to be a region which was called "Windows Generated Code - Do not edit" where this was all done. But people used to mess in there and have their forms broken. So in VS2005 MS broke it out into a 3rd form (the designer file) and used Partial Classes (a partial class allows you to define a class in multiple files - which makes following your code much easier and I recommend you break all your classes down in this way).

You have copied the VS2003 c# code (myform.cs) (including the initialisecomponent() code) in your VS2005 c# code (myform.cs) and you already have a file (myform.designer.cs) with that in it. So the errors are because you have 2 InitialiseComponent methods (Which you are allowed - thats called overriding) but they have the same signature (ie same parameters - none in this case) which is not allowed in overriding.
The second error is the same as there are two identical instances of FileMenu trying to be created - one in your code and one in the designer code (both in the Initialise component).

If you are using VS2005 I think you can get rid of your InitialiseCompnent code (not the call to it in the constructor but the code itself) in your myform.cs file.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.