George Charles

George Charles

  • NA
  • 3
  • 1.8k

How do I use Console Ap Graphic Drawings in Windows Form?

Apr 12 2011 12:39 AM
Hey everyone I'm rather new to c# and I am absolutely stumped with this. I've spent the last week googling every possible corner with every possible phrasing and cannot find a result.

Basically, I have a Map.CS and a Graphic.CS that draws a basic graphic with a bunch of boxes in console application. Now I need this to run in Windows Form but Map myMap = new Map(); does not work... I've tried text, rich text, picturebox and a few other toolboxes and none of them seems to display the graphic.

I'll give an example as to what I did in richtextbox:
What I would get is an error from the myMap(); part saying "The name 'myMap' does not exist in the current context.

what do I do?

Thank you!

PS. Oh yeah, I am basically self taught with windows form, for a project thats suppose to run solely on console application. so apologies if i am asking a really really dumb and obvious question.

[code]
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Map myMap = new Map();
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void richTextBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

    }
}
[/code]

the Map.cs
[code]
namespace WindowsFormsApplication1
{
    public class Map
    {
        public Map()
        {
            Graphic.initializeCanvas();
            Graphic.drawBox(2, 4, 55, 19, "Blue", "White", 6); //draw the actual map
            Graphic.drawString(26, 2, "MAP", "Yellow"); //draw the map title
            Graphic.drawString(60, 4, "COORDINATES", "Yellow");
            Graphic.drawString(60, 6, "4 - 15", "Yellow");

            Graphic.drawBox(60, 9, 2, 1, "Green", null, 0);
            Graphic.drawString(62, 9, " - Base Camp", "Yellow");

            Graphic.drawBox(60, 11, 2, 1, "Blue", null, 0);
            Graphic.drawString(62, 11, " - Unexplored", "Yellow");

            Graphic.drawBox(60, 13, 2, 1, "Red", null, 0);
            Graphic.drawString(62, 13, " - Explored", "Yellow");

            Graphic.drawBox(60, 15, 2, 1, "Yellow", null, 0);
            Graphic.drawString(62, 15, " - Selection", "Yellow");

            Graphic.drawString(60, 20, "(Arrow Keys to Move)", "Yellow");
            Graphic.drawString(60, 21, "(Enter to Select)", "Yellow");

            drawToScreen();
            Console.ReadLine();
        }


        private static void drawToScreen()
        {
            Graphic.drawCanvas(); //draw the canvas to the screen
        }
    }
}
[/code]

the Graphic.CS
[code]
namespace WindowsFormsApplication1
{
    class Graphic
    {
       
        private static char[,] canvasGFX = new char[24, 80];
        private static string[,] canvasColor = new string[24, 80];

        public Graphic(string args)
        {
        }//end Main


        public static void initializeCanvas()
        {
            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 80; j++)
                {
                    canvasGFX[i, j] = ' ';
                    canvasColor[i, j] = "Black";
                }; //end color
            }; //end row
        }//end initialize


        public static void drawBox(int x, int y, int width, int height, string color, string borderColor, int gridSize)
        {
            y--;
            x--;

            //Draw our menu, first by creating a black box:
            for (int j = y; j < (y + height); j++)
            {
                for (int i = x; i < (x + width); i++)
                {
                    canvasGFX[j, i] = '\u2592';
                    canvasColor[j, i] = color;

                    if (gridSize != 0)
                    {
                        if (( ((i) - x) % gridSize == 0) && ( ((j) - y) % gridSize == 0))
                        {
                            canvasGFX[j, i] = '\u253C';
                            canvasColor[j, i] = borderColor;
                        }
                        else
                        {
                            if (((i) - x) % gridSize == 0)
                            {
                                canvasGFX[j, i] = '\u2502';
                                canvasColor[j, i] = borderColor;
                            }
                            else
                            {
                                if (((j) - y) % gridSize == 0)
                                {
                                    canvasGFX[j, i] = '\u2500';
                                    canvasColor[j, i] = borderColor;
                                }
                            }
                        }                      
                    }
                   
                }; //end column loop
            }; //end row loop

            //If our border is more than null, let's draw it

            if (borderColor != null)
            {
                //top left corner
                canvasGFX[y, x] = '\u250c';
                canvasColor[y, x] = borderColor;

                //top right corner
                canvasGFX[y,(x + width) - 1] = '\u2510';
                canvasColor[y, (x + width) - 1] = borderColor;

                //bottom left corner
                canvasGFX[(y + height) - 1, x] = '\u2514';
                canvasColor[(y + height) - 1, x] = borderColor;

                //bottom right corner
                canvasGFX[(y + height) - 1, (x + width) - 1] = '\u2518';
                canvasColor[(y + height) - 1, (x + width) - 1] = borderColor;

                //top line
                for (int i = x + 1; i < (x + width - 1); i++)
                {

                    canvasGFX[y, i] = '\u2500';

                    if (gridSize != 0)
                    {
                        if (((i) - x) % gridSize == 0)
                        {
                            canvasGFX[y, i] = '\u252C';
                        }
                    }

                    canvasColor[y, i] = borderColor;
                }

                //bottom line
                for (int i = x + 1; i < (x + width - 1); i++)
                {
                    canvasGFX[(y + height) - 1, i] = '\u2500';

                    if (gridSize != 0)
                    {
                        if (((i) - x) % gridSize == 0)
                        {
                            canvasGFX[(y + height) - 1, i] = '\u2534';
                        }
                    }

                    canvasColor[(y + height) - 1, i] = borderColor;
                }

                //left line
                for (int i = y + 1; i < (y + height - 1); i++)
                {
                    canvasGFX[i, x] = '\u2502';

                    if (gridSize != 0)
                    {
                        if (((i) - y) % gridSize == 0)
                        {
                            canvasGFX[i, x] = '\u251C';
                        }
                    }

                    canvasColor[i, x] = borderColor;
                }

                //right line
                for (int i = y + 1; i < (y + height - 1); i++)
                {
                    canvasGFX[i, (x + width) - 1] = '\u2502';

                    if (gridSize != 0)
                    {
                        if (((i) - y) % gridSize == 0)
                        {
                            canvasGFX[i, (x + width) - 1] = '\u2524';
                        }
                    }

                    canvasColor[i, (x + width) - 1] = borderColor;
                }
            }; //end border

        } //end drawBox


        public static void drawString(int x, int y, string text, string color)
        {

            string tempString = text;
            char[] thechars = tempString.ToCharArray();
           

            for (int i = 0; i < thechars.Length; i++)
            {
                if (thechars[i] != ' ')
                {
                    canvasGFX[y - 1, x + i - 1] = thechars[i];
                    canvasColor[y - 1, x + i - 1] = color;
                }
            }
        }

       


        public static void drawCanvas()
        {
            //Console.Write("Here");
           
            //declare a temp array to build our canvasGFX on
            char[] tempcanvasGFX = new char[24];
            string color = "";

           

            //transfer our canvasGFX data to the temp array and draw each row when done
            for (int i = 0; i < 24; i++)
            {
                for (int j = 0; j < 80; j++)
                {
                    tempcanvasGFX[i] = canvasGFX[i, j];
                    color = canvasColor[i, j];

                    switch (color)
                    {
                        case "Yellow":
                            //ColorConsole.Write(ConsoleColor.Yellow, tempcanvasGFX[i]);
                            //SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_YELLOW );
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Blue":
                            //ColorConsole.Write(ConsoleColor.Blue, tempcanvasGFX[i]);
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "DarkGreen":
                            //ColorConsole.Write(ConsoleColor.DarkGreen, tempcanvasGFX[i]);
                            Console.ForegroundColor = ConsoleColor.DarkGreen;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Green":
                            //ColorConsole.Write(ConsoleColor.Green, tempcanvasGFX[i]);
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Black":
                            Console.ForegroundColor = ConsoleColor.Black;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Cyan":
                            Console.ForegroundColor = ConsoleColor.Cyan;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "DarkGray":
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Magenta":
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        case "Red":
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;

                        default:                           
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.Write("{0}", tempcanvasGFX[i]);
                            break;
                    };//end switch

                   

                };//end column for loop
            }; //end row for loop
        } //end draw canvas
   
   
   
    }//end class
}//end namespace
[/code]

Answers (3)