Last_Angel

This is the code for picture viewer:-

Code Snippet

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 Form1 : 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 menuHelp;
private System.Windows.Forms.MenuItem menuView;
// protected override void Dispose(bool dispose) { base.Dispose(dispose); }
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 Form1()
{
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
this.Controls.Add(pboxPhoto);


}

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)
{
try
{
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
catch (Exception ex)
{
MessageBox.Show("Unable to load file: " + ex.Message);
}

dlg.Dispose();
}
}

protected void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private PictureBoxSizeMode[] modeMenuArray = null;
}
}

and now this is the code for form1.designer.cs

the only error i am getting in the below file is

Error 1 Type 'WindowsApplication1.Form1' already defines a member called 'InitializeComponent' with the same parameter types

i tried different stuff but it ain't removed, i need help.

Code Snippet
namespace WindowsApplication1
{
partial class form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion
}
}

THANKS A LOT .. I really need help Sad




Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Derek Smyth

Hi,

The code in the form1.designer.cs is automatically generated for you as you alter the form so if you delete it Visual Studio will re-add it... what to do is copy the code in the intialize statement of the form1.cs and paste in the designer.cs's initalise method.

Hope that help, short and sweet.






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Last_Angel

yeah i know it will add again automatically, but i am sick of these error i tried all stuff..

i know designer.cs automatically generates. but i need help as i can't face that.. i dunno where to add the code.. can you put that here in reply.

Thanks a lot






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Derek Smyth

Hi,

If you want to add additional stuff create a constructor for the form, not in the designer.cs.

Like this...

public partial class Form1 : Form
{

//additional controls
DataSource data = new DataSource();

//a forms constructor
public Form1()
{
InitializeComponent(); //call first

//set up the additional controls
this.dataGridView1.DataSource = data;
this.dataGridView1.DataMember = "ExampleTable";

}

Thats just an example of how you would create a data source and bind it to some data but the same applies to any control or class or anything you want to create. The idea is you don't need to modify the designer.cs file, you just leave it alone.






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Last_Angel

i am still confused.. as my code is initialized only once and why am i getting that error..

try to compile code if you can..

Thanks a lot






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Derek Smyth

Hi,

Your getting the error because you have two initalise components methods in the same partial class. When you compile your code the form.cs and form.designer.cs are combined into one class. Since there are two initalise components methods the compiler doesn't know which one is being called. So you get the compile error.

You need to manually combine these methods by copying the code in one intialise components methods into the other, and then remove the blank initalise components methods so that there is only one. Its a good idea to copy the code into the designer.cs class.

You might also need to copy across the variables. It might actually be easier re-creating the form. The designer.cs isn't meant to be manually changed because Visual Studio automatically generates the InitaliseComponents method as you draw controls on the form and change the controls properties. You generally just say away from it.

Hope you get it sorted.






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Last_Angel

well i have sorted the errors and all on my own but result is "all or nothing" and i got nothing than a blank form, without menustrip. huh

can anyone check here its working but i dont know where is the problem

Code Snippet

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 Form1 : Form
{
private PictureBox pboxPhoto;
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 menuHelp;
private System.Windows.Forms.MenuItem menuView;
private System.Windows.Forms.MenuItem mainMenu1;
// protected override void Dispose(bool dispose) { base.Dispose(dispose); }

//private void InitializeComponent()
//{
//}

public Form1()
{
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
this.Controls.Add(pboxPhoto);


}

private void Form1_Load(object sender, EventArgs e)
{

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.Ctrl0;
this.menuLoad.Text = "&Load";
this.menuExit.Index = 1;
this.menuExit.Text = "&Exit";

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

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)
{
try
{
pboxPhoto.Image = new Bitmap(dlg.OpenFile());
}
catch (Exception ex)
{
MessageBox.Show("Unable to load file: " + ex.Message);
}

dlg.Dispose();
}
}

protected void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private PictureBoxSizeMode[] modeMenuArray = null;
}
}

Thanks a lot please give a try its working but creating unknown troubles..






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Last_Angel

No one bother to reply or to help






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

JeroGrav

The reason you are getting a blank form is because you removed the call to InitializeComponent() from your Form1 constructor.

When Form1 is created it will call the Form1() constructor. If you look at the constructor that you have in the code sample you supplied, you will see that there is no code actually adding any controls onto the form. Thats why you get a blank form.

The code that does add the controls is in the InitializeComponent() method.

Regards






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Last_Angel

yeah i did got you but i dunno how to arrange the above things as i am bit confused..

can you arrange the code i mean initializecomponent() code.

Thanks and waiting for your reply.






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

JeroGrav

When you use the Forms designer to create your form by placing controls (such as a menustrip) and stuff on the form it actually generates code that will create the form when you run the program.

This code is mostly inside the InitializeComponent() method which lives in the Form1.Designer.cs file (assuming your form is called Form1).

It is a very bad thing to go into Form1.Designer.cs and change the code of InitializeComponent() because the designer has generated that code. So, if you go and change some of it and then go back and change somthing using the designer (drag another control onto the form or something like that) it will completely overwrite the change you made.

That is why visual studio usually hides the Form1.Designer.cs file from you.

Now having said all that, what happens when you don't call InitializeComponent() from your Form1 constructor Well since InitializeComponent() contains all the code that was created by the designer, it means that you won't get any of the controls that you added onto the form using the designer.

So in the second code snippet you supplied, the only control you are actually adding is a picturebox in your constructor and thats all.

If you simply add a call to InitializeComponent() back in to your constructor you should see your menustrip and any other controls you added via the designer come back to the Form.

I hope thats a little more clear now.

Regards






Re: Windows Forms General Help NEEDED IN C# (picture viewer)

Yu Guo – MSFT

Hi, Last_Angel,

Derek and JeroGrav are both right.

At first, you added your InitializeComponent method in your winform class,

that was the same name of a method in Form1.Designer.cs.

So, you got a conflict problem.

And then you just removed the method in your constructor,

that caused the components on your form not initialized.

So, you got a blank winform.

Please just use the codes below, and you will get what you want.

Code Snippet

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 Form1 : Form

{

private PictureBox pboxPhoto;

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 menuHelp;

private System.Windows.Forms.MenuItem menuView;

private System.Windows.Forms.MenuItem mainMenu1;

// protected override void Dispose(bool dispose) { base.Dispose(dispose); }

//private void InitializeComponent()

//{

//}

public Form1()

{

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

this.Controls.Add(pboxPhoto);

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

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.Ctrl0;

this.menuLoad.Text = "&Load";

this.menuExit.Index = 1;

this.menuExit.Text = "&Exit";

menuView.Index = 1;

menuView.Text = "&View";

}

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)

{

try

{

pboxPhoto.Image = new Bitmap(dlg.OpenFile());

}

catch (Exception ex)

{

MessageBox.Show("Unable to load file: " + ex.Message);

}

dlg.Dispose();

}

}

protected void menuExit_Click(object sender, System.EventArgs e)

{

this.Close();

}

private PictureBoxSizeMode[] modeMenuArray = null;

}

}

Hopes this helps,

Regards