By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Launching C# Graphics Drawing from Menu

P: 1
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.Collections;
using System.ComponentModel;
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.ComponentModel.Container 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
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.Text = "Illustrating DrawXXX() methods";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandler(Draw_Graphics);

}

public void Draw_Graphics(object sender,PaintEventArgs e) {
/* Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5);
Pen penellipse = new Pen(Color.Blue,5);
Pen penpie = new Pen(Color.Tomato,3);
Pen penpolygon = new Pen(Color.Maroon,4);
*/
/*DashStyle Enumeration values are Dash,
DashDot, DashDotDot, Dot, Solid, etc*/
/*
penline.DashStyle = DashStyle.Dash;
g.DrawLine(penline,50,50,100,200);

//Draws an Ellipse
penellipse.DashStyle = DashStyle.DashDotDot;
g.DrawEllipse(penellipse,15,15,50,50);

//Draws a Pie
penpie.DashStyle = DashStyle.Dot;
g.DrawPie(penpie,90,80,140,40,120,100);

//Draws a Polygon
g.DrawPolygon(penpolygon,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.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.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.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(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.EventHandler(this.menuItem4_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(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.menuItem3_Click);
//
// WinForm
//
this.AutoScaleBaseSize = 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.EventArgs e)
{
Application.Exit();
}

private void Draw_myGraphics(object sender, PaintEventArgs e)
//menuItem3_Click(object sender, System.EventArgs e)
{
Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5);
Pen penellipse = new Pen(Color.Blue,5);
Pen penpie = new Pen(Color.Tomato,3);
Pen penpolygon = new Pen(Color.Maroon,4);


/*DashStyle Enumeration values are Dash,
DashDot, DashDotDot, Dot, Solid, etc*/

penline.DashStyle = DashStyle.Dash;
g.DrawLine(penline,50,50,100,200);

//Draws an Ellipse
penellipse.DashStyle = DashStyle.DashDotDot;
g.DrawEllipse(penellipse,15,15,50,50);

//Draws a Pie
penpie.DashStyle = DashStyle.Dot;
g.DrawPie(penpie,90,80,140,40,120,100);

//Draws a Polygon
g.DrawPolygon(penpolygon,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
Sep 21 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.