Hi ..
Just getting interested to learn C# and needs help.
I want to write C# application so that the program will execute and draw graphics when the user select the drawing menu from the main menu, SigDraw.
Here is the codes:-
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Drawing. Drawing2D;
namespace myDraw
{
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows. Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.MainMenu mainMenu1;
private System.Windows. Forms.MenuItem menuItem1;
private System.Windows. Forms.MenuItem menuItem2;
private System.Windows. Forms.MenuItem menuItem3;
private System.Windows. Forms.MenuItem menuItem4;
public WinForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
this.Text = "Illustrati ng DrawXXX() methods";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandl er(Draw_Graphic s);
}
public void Draw_Graphics(o bject sender,PaintEve ntArgs e) {
/* Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5 );
Pen penellipse = new Pen(Color.Blue, 5);
Pen penpie = new Pen(Color.Tomat o,3);
Pen penpolygon = new Pen(Color.Maroo n,4);
*/
/*DashStyle Enumeration values are Dash,
DashDot, DashDotDot, Dot, Solid, etc*/
/*
penline.DashSty le = DashStyle.Dash;
g.DrawLine(penl ine,50,50,100,2 00);
//Draws an Ellipse
penellipse.Dash Style = DashStyle.DashD otDot;
g.DrawEllipse(p enellipse,15,15 ,50,50);
//Draws a Pie
penpie.DashStyl e = DashStyle.Dot;
g.DrawPie(penpi e,90,80,140,40, 120,100);
//Draws a Polygon
g.DrawPolygon(p enpolygon,new Point[]{
new Point(30,140),
new Point(270,250),
new Point(110,240),
new Point(200,170),
new Point(70,350),
new Point(50,200)}) ;
*/
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose(di sposing);
}
#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 InitializeCompo nent()
{
this.mainMenu1 = new System.Windows. Forms.MainMenu( );
this.menuItem1 = new System.Windows. Forms.MenuItem( );
this.menuItem4 = new System.Windows. Forms.MenuItem( );
this.menuItem2 = new System.Windows. Forms.MenuItem( );
this.menuItem3 = new System.Windows. Forms.MenuItem( );
//
// mainMenu1
//
this.mainMenu1. MenuItems.AddRa nge(new System.Windows. Forms.MenuItem[] {
this.menuItem1,
this.menuItem2} );
//
// menuItem1
//
this.menuItem1. Index = 0;
this.menuItem1. MenuItems.AddRa nge(new System.Windows. Forms.MenuItem[] {
this.menuItem4} );
this.menuItem1. Text = "&File";
//
// menuItem4
//
this.menuItem4. Index = 0;
this.menuItem4. Text = "E&xit";
this.menuItem4. Click += new System.EventHan dler(this.menuI tem4_Click);
//
// menuItem2
//
this.menuItem2. Index = 1;
this.menuItem2. MenuItems.AddRa nge(new System.Windows. Forms.MenuItem[] {
this.menuItem3} );
this.menuItem2. Text = "&Draw";
//
// menuItem3
//
this.menuItem3. Index = 0;
this.menuItem3. Text = "&SigDraw";
this.menuItem3. Click += new System.Drawing. Graphics(this.m enuItem3_Click) ;
//
// WinForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 268);
this.Menu = this.mainMenu1;
this.Name = "WinForm";
this.Text = "WinForm";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new WinForm());
}
private void menuItem4_Click (object sender, System.EventArg s e)
{
Application.Exi t();
}
private void Draw_myGraphics (object sender, PaintEventArgs e)
//menuItem3_Click (object sender, System.EventArg s e)
{
Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5 );
Pen penellipse = new Pen(Color.Blue, 5);
Pen penpie = new Pen(Color.Tomat o,3);
Pen penpolygon = new Pen(Color.Maroo n,4);
/*DashStyle Enumeration values are Dash,
DashDot, DashDotDot, Dot, Solid, etc*/
penline.DashSty le = DashStyle.Dash;
g.DrawLine(penl ine,50,50,100,2 00);
//Draws an Ellipse
penellipse.Dash Style = DashStyle.DashD otDot;
g.DrawEllipse(p enellipse,15,15 ,50,50);
//Draws a Pie
penpie.DashStyl e = DashStyle.Dot;
g.DrawPie(penpi e,90,80,140,40, 120,100);
//Draws a Polygon
g.DrawPolygon(p enpolygon,new Point[]{
new Point(30,140),
new Point(270,250),
new Point(110,240),
new Point(200,170),
new Point(70,350),
new Point(50,200)}) ;
}
private void menuItem3_Click (object sender, System.Drawing. Graphics e)
{
Graphics g = e.Graphics;
}
}
}
The Problem : The graphic is drawn eventhough the user has not selected menuItem3, 'SigDraw'
Appreciate you helps.
Thanks
Hasim AH, Sept. 21st. 2006