The name 'g' does not exist in the current context

May 10 2010 8:38 PM
Hi,

I am making a basic sketch program for university. So far its going ok and drawing lines with up, down, left, right. 

But I want to set the letter C to clear the program.

I get the error "The name 'g' does not exist in the current context".

Is it to do with the protected override bool?

My code is below.

Thanks in advance.

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

namespace EtchaSketch
{
    public partial class Form1 : Form
    {
        int x = 50, y = 50;     // starting position
        Bitmap bm;
        int x1 = 50, y1 = 50;
   

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
            bm = new Bitmap(this.Width, this.Height);   // create a form-size bitmap
            Graphics g = Graphics.FromImage(bm);        // get a graphic object for the bitmap
            g.FillEllipse(Brushes.Blue, x, y, 20, 20);  // put a circle in the bitmap
            this.BackgroundImage = bm;                  // use the bitmap as the form background
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromImage(bm);        // get a graphic object for the bitmap
            g.FillEllipse(Brushes.Blue, x1, y1, 20, 20);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            string input;
            input = keyData.ToString();

            if (input == "Down")
            {
                y1 = y1 + 10;

                
                Refresh();
                
                return true;
                
            }
            if (input == "Up")
            {
                y1 = y1 - 10;


                Refresh();

                return true;

            }
            if (input == "Left")
            {
                x1 = x1 - 10;


                Refresh();

                return true;

            }
            if (input == "Right")
            {
                x1 = x1 + 10;


                Refresh();

                return true;

            }
            if (input == "C")
            {
                
                g.Clear(BackColor);


                Refresh();

                return true;

            }
          

            return false;    // return true if key processed, otherwise false
        }
    }
}


Answers (1)