6 Replies - 2013 Views - Last Post: 12 April 2011 - 09:01 AM Rate Topic: -----

#1 Vizjerie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 11

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

Posted 11 April 2011 - 07:35 PM

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.

 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)
        {
            richTextBox1.Text = myMap();
        }

    }
}



a Map.CS

 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
        }
    }
}
 


and a Graphic.cs

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; 


This post has been edited by Vizjerie: 11 April 2011 - 07:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I use Console App Graphic Drawings in Windows Form?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 11 April 2011 - 10:06 PM

Three big problems: one is of scope. Your variable myMap exists only in the scope in which it is declared; in this case, the constructor. You need to declare it as part of the class, or declare it when you want to use it, depending on whether or not you need to reuse it or access it from multiple methods.

Second, myMap() will not work, since it is not a method, it's an object. You have to call a method that belongs to it.

Your third problem is the biggest: there is no analog to the Console in a Windows Forms app. You'll have to completely rework your logic to work the way a RichTextBox or something else works. This is no small task, since nothing works the same way the Console does.

Hope that sets you on the right path.
Was This Post Helpful? 0
  • +
  • -

#3 Vizjerie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 11

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 11 April 2011 - 10:24 PM

okay well im kind of stumped and half understanding what you mean. what would you suggest i should do?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 11 April 2011 - 10:30 PM

Which half aren't you understanding? It's hard to help when we don't know what part you need help with.

As to what I suggest to do: identify a viable alternative (like a RichTextBox, perhaps), and then learn how to properly use it. Then recreate your Graphics class (though give it a different name, since System.Windows.Forms already has a Graphics class) using the new logic. It's not going to be simple, but it can be done.

Also, using the built in Graphics class, it's possible to draw shapes, colors, and text directly to a form. That's possibly an alternative as well. It will still need adaption and experimentation, since there's not much similar about it to the console, but it's another viable alternative.

Also, it's past midnight here for me, so I'll be asleep for the next several hours. I'll be posting again during the day if you still need help.
Was This Post Helpful? 0
  • +
  • -

#5 Vizjerie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 11

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 12 April 2011 - 08:56 AM

Alright, aside from re-doing the graphic in forms, (because the graphic.cs and map.cs is done by someone else and i have to use it), is there any way to plug those in? what tool would it be? and what would the code be. I know I can google this, problem is I dont know what question i should be asking if that makes sense...
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 12 April 2011 - 08:58 AM

I'm sorry, but there is no way to plug your Console code into a Windows Forms app.
Was This Post Helpful? 0
  • +
  • -

#7 Vizjerie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 11

Re: How do I use Console App Graphic Drawings in Windows Form?

Posted 12 April 2011 - 09:01 AM

oh. damn it :- /
so ill have to do the form in console app then.

how the hell do i... back to google then.

well thanks anyways, glad i didnt waste any more time on this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1