I need to change the font and text color in a console Application.

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 5043 Views - Last Post: 22 July 2012 - 02:21 AM Rate Topic: -----

#1 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

I need to change the font and text color in a console Application.

Posted 20 July 2012 - 06:57 PM

Hello all, I need to somehow adapt a certain color to certain text....here is my code:

string holdValue = string.Empty;

            for (; ;)/>
            {

                holdValue = Console.ReadLine();
                
                if (holdValue == "/help"){Console.WriteLine("Commands: /help /attack /equipment");}
                if (holdValue == "/attack"){Console.WriteLine("You are Attacking!");}
                if (holdValue == "/equipment") { Console.WriteLine("You have equipment!"); }
                if (holdValue == "/train") { Console.WriteLine("Which skill would you like to train?"); }
            }


Can I do something like:
 if (holdValue == "/help"){Console.WriteLine.Color = Blue("Commands: /help /attack /equipment");}


thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: I need to change the font and text color in a console Application.

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 07:40 PM

Nope. Console is fixed font.
Give up on making this a console app.
Make it a WinForms or WPF and use a RichTextBox as your 'console'. Then you can do as you like.
Was This Post Helpful? 0
  • +
  • -

#3 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 10:03 PM

Okay so I moved everything over to a Forms App... I am having trouble accessing the text box from my class:
holdValue = Console.ReadLine();
                
                if (holdValue == "/help"){Form1.textBox1.Text = "Commands: /help /attack /equipment";}


My errors are:
Error	1	'ExodusMud.Form1.textBox1' is inaccessible due to its protection level	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Commands.cs	20	49	ExodusMud



and
Error	2	An object reference is required for the non-static field, method, or property 'ExodusMud.Form1.textBox1'	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Commands.cs	20	43	ExodusMud


the code is the same as above with this as the load method:
 private void Form1_Load(object sender, EventArgs e)
        {
            Commands commands = new Commands();
            commands.commands();
        }

Any ideas why I cannot access that textbox1?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 10:29 PM

I've got an idea! Maybe it's because textBox1 is marked as private. :lol:

This post has been edited by Skydiver: 20 July 2012 - 10:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 10:39 PM

That fixed Error 1 what about error 2?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 10:53 PM

While it is early, rename textBox1 to something like commandTextBox, and richTextBox1 to gameConsole, before our friendly Klingon tells you to. :-)

The brute force solution is to make commandTextBox (and possibly gameConsole) public. I highly, highly discourage you from doing so. Never make member variables public unless you really, really, really, really have to. There are better object oriented ways.

The next worse thing to brute force is to migrate your code from Commands and move it into your Form code. Mind you that this is the way probably 90% of the people in the world end up writing their WinForms code, so you'll be in good company and nobody will think bad of you. The downside of this approach is that you'll have a mishmash of code that deals with UI, code that deals with game state, and code that deals with parsing. No separation of concerns at all.

Since you already have a nice Commands class that separates your code that contains your command parsing and your game state, why not keep it separate. Just let your Commands object have two addition methods:
void Introduction(RichTextBox gameConsole);
void Parse(string command, RichTextBox gameConsole);


When the game first starts, pass in the gameConsole to Introduction() so that your game can display the game scenario and instructions. And then after that, whenever the user presses Enter or hits the Send button, you call Parse() passing in commandTextBox.Text and gameConsole.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 10:58 PM

You should really post the code where the compiler is reporting errors on. We are not psychic, you know. (Psychotic, getting there. Psychic, not yet. :lol:)
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 11:13 PM

Actually, looking at the errors on post #3, I've got a pretty good idea what is happening.

You are trying to access textBox1 as: Form1.textBox1. You need to access it as form1.textBox1 where form1 is a variable of type Form1. You can probably pass in this in your load event:
       private void Form1_Load(object sender, EventArgs e)
       {
           Commands commands = new Commands();
           commands.commands(this);
       }



See how ugly this is getting? Now the Commands class knows about the intimate details of the Form1 class. This is why I was advising going the route I suggested in post #6.
Was This Post Helpful? 0
  • +
  • -

#9 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I need to change the font and text color in a console Application.

Posted 20 July 2012 - 11:48 PM

Thanks for taking the time to reply, I tried as you said this:
private void Form1_Load(object sender, EventArgs e)
2
{
3
    Commands commands = new Commands();
4
    commands.commands(this);
5
}



It gave me a new error:
Error	2	No overload for method 'commands' takes 1 arguments	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Form1.cs	23	13	ExodusMud


This is on the: commands.commands(this); line.
and I still have the issue of this:
Error	1	An object reference is required for the non-static field, method, or property 'ExodusMud.Form1.textBox1'	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Commands.cs	20	43	ExodusMud


That is on this: if (holdValue == "/help"){Form1.textBox1.Text ="Commands: /help /attack /equipment";}
The problem seems to be with The part that says Form1.textBox1

Not sure what I am doing wrong!
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 12:06 AM

View PostTailean, on 20 July 2012 - 11:48 PM, said:

Thanks for taking the time to reply, I tried as you said this:
private void Form1_Load(object sender, EventArgs e)
{
    Commands commands = new Commands();
    commands.commands(this);
}



It gave me a new error:
Error	2	No overload for method 'commands' takes 1 arguments	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Form1.cs	23	13	ExodusMud


This is on the: commands.commands(this); line.
and I still have the issue of this:
Error	1	An object reference is required for the non-static field, method, or property 'ExodusMud.Form1.textBox1'	C:\Users\Tailean\AppData\Local\Temporary Projects\ExodusMud\Commands.cs	20	43	ExodusMud


That is on this: if (holdValue == "/help"){Form1.textBox1.Text ="Commands: /help /attack /equipment";}
The problem seems to be with The part that says Form1.textBox1

Not sure what I am doing wrong!


It looks like you just cut pasted the text without reading the text above. Let me put in the text above again and make it in bold:

View PostSkydiver, on 20 July 2012 - 11:13 PM, said:

You are trying to access textBox1 as: Form1.textBox1. You need to access it as form1.textBox1 where form1 is a variable of type Form1. You can probably pass in this in your load event:


This means:
1. You need to change your commands method to take a Form1 parameter, and probably name this parameter form1. (I personally recommend naming it form, or mainForm.)
2. Wherever you are using Form1.textBox1, change it to whatever you named the paramter. eg. mainForm.textBox1.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3171
  • View blog
  • Posts: 9,609
  • Joined: 05-May 12

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 12:12 AM

An alternative is just to pass the Form1 in as a parameter to the Commands constructor and just have the Commands class hold on to a reference for it's entire lifetime.
Was This Post Helpful? 0
  • +
  • -

#12 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 01:21 AM

hmm... For some reason I just dont get what it is I need to do....I put Form1 in it like this:
public void commands(Form1)


I even tried it with form1 lower case and it still don't work. I don't know the Syntax for it I guess..you said to: Pass the Form1 in as a parameter to the Commands constructor and just have the Commands class hold on to a reference for it's entire lifetime.

I don't know what that means..lol. How do I have commands class hold on to a reference?
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5957
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 04:49 AM

Before you start trying to make a game, take the time to actually learn the language, eh? You seem to have no understanding of the very basics. The road you're following leads directly to "help vampire".
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,361
  • Joined: 02-June 10

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 07:28 AM

Tailean has seen that same suggestion along with my 'Resources' post probably more than any other rookie. It appears to not be in her nature to try to learn before building.

But here it is again for old time sake:

Stop trying to architect a program while you are still learning the basics of C#. It just never works. Its like saying you are going to learn home architecting WHILE you hammer boards together without a blueprint, and try to learn both new skills at the same time.

First learn the language. Do a couple hundred tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

Then later you can start architecting your own simple stuff. Build a calculator. Build a DVD library program. Etc. Stuff that doesn't involve the complexity of a game.

Then move up to games.



There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

Was This Post Helpful? 0
  • +
  • -

#15 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: I need to change the font and text color in a console Application.

Posted 21 July 2012 - 12:00 PM

I thank you for your advise, But I don't think that you understand. I have been writing in C# for 2 years now. I have been to School and have my Associate's in Computer Annalist. I have taken 2 C# classes and have been on many forms trying to learn. I have even created a few games..including Tic Tac Toe, and Keno. It be blatantly honest.....my problem is I am a much slower leaner than most people. I wish we had "Special Ed C# Teacher...lol. I am not saying this to down grade myself...just the truth about me. Most of the games I have build I have been able to find out how by looking online. My problem seems to be explanation. I most likely know what "Pass the Form1 in as a parameter to the Commands constructor and just have the Commands class hold on to a reference for it's entire lifetime." means just not in the context it is written. I know about Parameters...I understand Constructor..I don't understand: Have the Commands class hold on to a reference. Thanks tho, I know that tlhIn`toq is right in what he says. I just don't want to wast 2 more years and not be much farther than I am now..because I have a Passion for C#.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2