C# - Rewrite/edit line while program is running - c#

Is there any way that i can edit/rewrite certain lines that have already bin printed by the Console.PrintLine() method? I have to be able to edit any line that is shown in the prompt.
This is an example on what the code that i'm trying to get running, maybe can look like:
public static void RewriteLine(LineNr, Text)
{
//Code
}
Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");
Example to show which line that i want rewritten based on output from the previous code:
Text to be rewritten //This line (has already bin executed by the Console.WriteLine() method) shall be replaced by: "New text"
Just some text

It should look like this:
public static void RewriteLine(int lineNumber, String newText)
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - lineNumber);
Console.Write(newText); Console.WriteLine(new string(' ', Console.WindowWidth - newText.Length));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Main(string[] args)
{
Console.WriteLine("Text to be rewritten");
Console.WriteLine("Just some text");
RewriteLine(2, "New text");
}
What's happening is that you change cursor position and write there something. You should add some code for handling long strings.

Related

handover ConsoleColor to method in C#

I want to write a short method that I can use to write one single line in the console colorful.
Currently I have to call my method like this:
ChangeConsoleColor("ABC", ConsoleColor.Yellow, ConsoleColor.Red);
But I want to be able to call it like this:
ChangeConsoleColor("ABC", Yellow, Red);
The perfectly working code I have looks like this:
static void Main(string[] args)
{
string myText = "ABCDEFG";
ChangeConsoleColor(myText, ConsoleColor.Yellow, ConsoleColor.Red);
System.Threading.Thread.Sleep(6000);
}
private static void ChangeConsoleColor(string Text, ConsoleColor BGC, ConsoleColor FGC)
{
Console.BackgroundColor = BGC;
Console.ForegroundColor = FGC;
Console.WriteLine(Text);
Console.ResetColor();
}
How do I need to change my code to get rid of the 'ConsoleColor' in the function call part?

Is it possible to keep portions of the console from scrolling in .NET?

Figure 1: the bottom portion is to scroll, while the top portion is to stay put.
So I'm writing a CLI .NET Core application.
The top portion of the image is to stay put whereas the bottom portion is to scroll under the top portion.
If there was a more direct access to the console, it would be rather easy. But as we are in C#, no such (cross-platform) access exists that I am aware of.
So, how would I achieve the desired goal here, without platform-locking?
You can try this if it might give you an idea. Based on this post C# Console : How to make the console stop scrolling automatically?.
static readonly int maxLine = 10;
static int currentLine = 0;
static void Main(string[] args)
{
setHeader();
while (true)
{
writeLine("Please type and enter:");
string value = Console.ReadLine();
writeLine($"Your input is: {value}");
writeLine($"Current Line: {currentLine}");
if (currentLine > maxLine)
{
resetCursorPosition();
writeLine($"Your input is: {value}");
writeLine($"Current Line: {currentLine}");
}
}
}
private static void writeLine(string value)
{
Console.WriteLine(value);
currentLine = currentLine + 1;
}
private static void resetCursorPosition()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
currentLine = 0;
setHeader();
}
private static void setHeader()
{
writeLine("----------------------------------------------");
writeLine("---------------MY CUSTOM HEADER---------------");
writeLine("----------------------------------------------");
}
Output:

First time attempting programing. What am I doing wrong here?

enter image description hereFor my first try, I looked up some YouTube tutorials. One is showing how to create a Magic 8 Ball. I am following the instructions pretty much exactly but it won't work. When written like this it's fine:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
String Void TellPeopleTheName(String Text);
}
}
As you see I am trying to try create a String, not that I need one to do this. I just want to see how it works (So when I need it I don't need to re-type it and can just post this) But starting off it says "TellPeopleTheName must declare a body" I'm confused as this did not happen in the video. Not only that but when I cut and paste the code to join with it. For some reason it all breaks and no longer works:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
String Void TellPeopleTheName(String Text);
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
The entire bottom section is underlined in red saying "Invalid Token" and Writeline does not exist in this context". This confuses me more as this did not happen in the video either. What am I doing so wrong?
Thanks for the help and sorry for the nooby comments. I'm 20 and always wanted to learn coding but always been too busy working. Recently broke my back and now have plenty of time sitting doing nothing so I figured I would give it a shot.
This is mine (on top) and his (Bellow) and even after your help (I did go and re-watch and noticed I put String Void and not Static void) These problems still are here and I still don't understand. I apologise. I am really trying to learn and understand. This is a section I could skip and still make the 8-ball but I want to know why it does not work and how to fix it so then I know for when I do need to use this.
NEW FULL CODE:
namespace Magic8Ball
{
class Program
{
static void Main(string[] args)
{
//Preserve Current Console Text Color
ConsoleColor OldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Ask a question?: ");
Console.ForegroundColor = ConsoleColor.DarkGray;
String QuestionString = Console.ReadLine();
//Cleaning Up
Console.ForegroundColor = OldColor;
}
//This will tell people the name
static void TellPeopleTheName()
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
}
This line is out of place and doesn't actually do anything:
String Void TellPeopleTheName(String Text);
It's void, not Void. Casing matters.
You're giving it two return types, String and void. It either returns something or it doesn't.
That's a method header, but you're missing a method body. As a single line of code it doesn't make sense. Methods need to do something.
For example, a method might look like this:
void TellPeopleTheName(string text)
{
// write code in here to do something
}
Then you would invoke (call) that method somewhere from another method, when you want to perform that operation:
TellPeopleTheName("some text");
Or, in the case of a method which returns a string:
string TellPeopleTheName(string text)
{
// code which does something
return "some string";
}
Invoking it would be the same, but you could store the return value in a variable or otherwise use it in some way:
var someVariable = TellPeopleTheName("some text");
(Note: There are an ongoing variety of issues you may encounter even in trying what's described here. The difference between a static and non-static method come to mind, particularly if you try to call this from main(). Or the variety of ways you may return from a method or use the returned value. And so on. There's much to learn. Good luck!)
String Void TellPeopleTheName(String Text);
should be
void TellPeopleTheName(String Text)
{
}
That would take care of "missing body" error!
void should be in lowercase.
You are missing open/close brackets on the method too.
I notice a couple things you've missed in understanding.
What you've tried to do is write a method to write to the console the name of the program, but the method is not structured correctly.
The general structure of a method is...
AccessModifier static/non-static ReturnType MethodName (ParameterType ParameterName) {
//code to do something
}
An access modifier defines how accessible the method is to other code (as a starting programmer, just keep it as public until you become more advanced).
Putting static or nothing (non-static) is a little confusing for a beginner, if you'll be calling the method from Main (a static method) then it will need to be static, that's sufficient enough for a beginner's understanding for now.
The return type defines what the method should return back when the method is called.
The method name is just how the method is called, should be descriptive and clear what the method does.
A parameter type is the type of object that is being passed as a parameter for the method to use.
A parameter name is just a name, the name should easily explain what the parameter's purpose is for the method.
Following this structure, your method should look like...
public static void TellPeopleTheName(string text)
{
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Magic 8 Ball. By: Conner Bostock");
}
If you want to make the method more "correct", then it would be...
public static void TellPeopleTheName(string nameOfProgram)
{
//Change Console Text Color
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(nameOfProgram);
}
and you would call the method like this...
TellPeopleTheName("Magic 8 Ball. By: Conner Bostock");

C# Restarting a console application

I've created a small application that does a small conversion. At the end of the program I've created a method that allows the user to make another calculation if they press 'r'. All I want it to do is if they press r, take them back to the beginning of Main, else terminate program. I do not want to use goto. This is what I've got so far, and the error I'm getting.
http://puu.sh/juBWP/c7c3f7be61.png
I recommend you use another function instead of Main(). Please refer to the code below:
static void Main(string[] args)
{
doSomething();
}
public static void WouldYouLikeToRestart()
{
Console.WriteLine("Press r to restart");
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine();
if (input.KeyChar == 'r')
{
doSomething();
}
}
public static void doSomething()
{
Console.WriteLine("Do Something");
WouldYouLikeToRestart();
}
A while loop would be a good fit, but since you say the program should run and then give the user the option to run again, an even better loop would be a Do While. The difference between while and Do While is that Do While will always run at least once.
string inputStr;
do
{
RunProgram();
Console.WriteLine("Run again?");
inputStr = Console.ReadLine();
} while (inputStr == "y");
TerminateProgram();
In your case, you want to repeat something so of course you should use a while loop. Use a while loop to wrap all your code up like this:
while (true) {
//all your code in the main method.
}
And then you prompt the user to enter 'r' at the end of the loop:
if (Console.ReadLine () != "r") {//this is just an example, you can use whatever method to get the input
break;
}
If the user enters r then the loop continues to do the work. break means to stop executing the stuff in the loop.

Arbitrary and non sense string inside a code block

This is something , i don't understand , if i put any arbitrary string inside a code block, it will throw some compile time error but if i put something like below , it will not.
static void Main(string[] args)
{
int i = 5;
ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp://www.google.com
Console.WriteLine(i.ToString());
Console.ReadLine();
}
any idea why this is happening? I just found it accidentally , not sure why , may be i am missing something.
That is a Label.
Look at the : at the end.
If you remove the : in the end. It won't compile
goto and label
ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp: is a label, because it's followed by :.
You can then use it with goto statement:
static void Main(string[] args)
{
int i = 5;
ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp://www.google.com
Console.WriteLine(i.ToString());
Console.ReadLine();
goto ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp;
}

Categories

Resources