This question already has answers here:
Console.ReadLine("Default Text Editable Text On Line")
(2 answers)
Closed 4 years ago.
I have multiple I/O tasks that I want to do with a console:
Print out standard, non-editable text (Console.WriteLine())
Print out text that the user can edit (?)
Allow the user to type, and be able to output text via the two methods above (?)
It would be nice if I could do password masking too.
Anybody have any solutions?
Edit text like in a console-based text editor?
I think all that you need is in the Console class, have a look at its members:
http://msdn.microsoft.com/en-us/library/system.console.aspx
Maybe you could give curses a try, there is a C# wrapper avaiable. Didn't tried it myself, though...
Party like it's 1988 with Mono's getline. http://tirania.org/blog/archive/2008/Aug-26.html
Answer already submitted
but the below code may be help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static int i = 0;
public static string[] S = new string[] { "A", "B", "C", "D", "E", "F" };
static void Main(string[] args)
{
Console.Write("Please Select : "+S[i]);
ConsoleKeyInfo K = Console.ReadKey();
while (K.Key.ToString() != "Enter")
{
Console.Write(ShowOptions(K));
K = Console.ReadKey();
}
Console.WriteLine("");
Console.WriteLine("Option Selected : " + S[i]);
Console.ReadKey();
}
public static string ShowOptions(ConsoleKeyInfo Key)
{
if(Key.Key.ToString() == "UpArrow")
{
if (i != S.Length-1)
return "\b\b" + S[++i];
else
return "\b\b" + S[i];
}
else if (Key.Key.ToString() == "DownArrow")
{
if(i!=0)
return "\b\b" + S[--i];
else
return "\b\b" + S[i];
}
return "";
}
}
}
Related
This question already has answers here:
How to change Foreground Color of each letter in a string in C# Console?
(3 answers)
Closed 6 months ago.
So I am trying to color individual charactrs E.G "0 is allways red 1 is allways blue 2 is allways green etc." I am using a nuget package called "MathNet.Numerics.FSharp." And from that i am using BigRational (a BigRational is a infanate bit int.) in a Console.WriteLine E.G "Console.WriteLine(i);" and I am makeing a program with extreamly large numbers and I want to color the numbers to so I can look at the numbers and look for patterns.
The CharacterColoring function that I created can be used both singly and in a loop.
It is used in the loop as follows:
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
public static void CharacterColoring(char YourChar, ConsoleColor YourFavoriteColor)
{
Console.ForegroundColor = YourFavoriteColor;
Console.Write(YourChar);
}
static void Main(string[] args)
{
string YourText = "Hello world 1 2";
for (int i = 0; i < YourText.Length; i++)
{
if (YourText[i] == '1')
{
CharacterColoring(YourText[i], ConsoleColor.Red);
}
else if (YourText[i] == '2')
{
CharacterColoring(YourText[i], ConsoleColor.Blue);
}
else
{
CharacterColoring(YourText[i], ConsoleColor.Green);
}
}
Console.ReadKey();
}
}
}
Tested in:
Visual Studio 2017, .NET Framework 4.5.2, Console App
Trying to make a simple app which will ask a few questions.
But for some reason, my AskQuestion function doesn't work.
I plan on adding an easily swap able database later which is why I'm trying to take a slightly more modular approach and as I am a beginner I am unsure what I did wrong. The only errors are in line 21 for the AskQuestion class.
Errors are:
CS1001 Identifier Expected
CS1514 { expected
CS1513 } expected
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
// Question Base
class Question
{
public String question = "Empty Question";
public String correctanswer = "Empty Answer";
public String givenanswer = "Empty Answer";
public String response = "Empty Response.";
public bool cleared = false;
}
// Ask Base
class void AskQuestion(Question Q)
{
while (Q.cleared == false)
{
Console.WriteLine(Q.question);
Q.givenanswer = Console.ReadLine();
Q.givenanswer.ToLower();
if (Q.givenanswer == Q.correctanswer)
{
Console.WriteLine(Q.response);
Q.cleared = true;
}
else
{
Console.WriteLine("Wrong. Try again.");
}
}
}
// Main Function
void Main(string[] args)
{
string Name;
Console.WriteLine("Welcome challenger! You're going to have a good time.");
Console.WriteLine("Make sure you use proper grammar. Or you may be stuck for no reason.");
Console.WriteLine("What is your name challenger?");
Name = Console.ReadLine();
Console.WriteLine("Welcome {0} to the challenge. I wish you best of luck. You will need it.",Name);
Question Q1 = new Question();
Q1.question = "What is the color of the sun?";
Q1.correctanswer = "White";
Q1.response = "Correct. Despite the fact it appears Yellow on earth, if you observe the sun from space, you would see it's true color. White.";
AskQuestion(Q1);
Q1.cleared = true;
Console.WriteLine("Nice little warmup. But, lets get a bit serious.");
}
}
}
change this
class void AskQuestion(Question Q)
to
void AskQuestion(Question Q)
This should be a method. The keyword class tells the compiler you want to create a inner class inside the out class Program
Q.givenanswer.ToLower(); doesn't make Q.givenanswer lowercase - it returns a new lowercase string which you need to assign to a variable, or just `Q.givenanswer = Q.givenanswer.ToLower();
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I am making a project, But i need to be able to split 1 row of text into 2 strings.
How would i go about doing this?
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Stinger";
Console.Write("Configuration FIle.... ");
Thread.Sleep(1000);
if (System.IO.File.Exists(#"C:\Stinger\Configuration\config.cfg"))
{
Console.Write("Found");
Thread.Sleep(1000);
Console.WriteLine("\r\n");
Console.WriteLine("--Configuration--");
string[] readText = File.ReadAllLines(#"C:\Stinger\Configuration\config.cfg");
foreach (string s in readText)
{
Console.WriteLine(s);
Thread.Sleep(1000);
}
}
else // Configuration File Else Statement
{
Console.WriteLine("Missing");
Thread.Sleep(4000);
}
}
}
And here is my Config Contents
fullscreen 1
I want to be able to make "fullscreen" and "1" part of the row 2 strings.
I've done continuous googling and reading articles on how to split.
But it's not making any sense to me.
Any help?
This is a simple string.Split and is actually very straightforward and fundamental.
Assuming this is the code where you are getting fullscreen 1 from, you can do something like this.
string[] readText = File.ReadAllLines(#"C:\Stinger\Configuration\config.cfg");
foreach (string s in readText)
{
string[] split= s.Split(" ");
Console.WriteLine(split[0]+" "+split[1]);
Thread.Sleep(1000);
}
The Console.WriteLine will print your expected output.
I'm taking a programming class in c# and it is our first week and I'm trying to work ahead a little. To that end, I am trying to rework one of our class labs and am stuck with my 'GetInt' method.
Ideally, the GetInt method (line 50) takes in a string (Enter a number) and returns the number as an int so i can then use it to do some math. Currently line #24 turns the test error 'Cannot implicity convert type 'int' to 'string'.
Any help is appreciated. I am very much a newb, so please don't assume i know anything.
Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
String numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
Your GetInt method returns int. So you need to change from
String numDonuts = GetInt("How many donuts would you like?: ");
to
int numDonuts = GetInt("How many donuts would you like?: ");
The problem is that the method GetInt returns int.
And you're trying to assign its return value to this String variable on line 24.
String numDonuts
You cannot do this.
Change the first word on line #24 from 'String' to 'int' because I am asking for an int not a string.
Your problem is there:
String numDonuts = GetInt("How many donuts would you like?: ");
You try to set int value to string variable.
change to
var numDonuts = GetInt("How many donuts would you like?: ");
Your GetInt method returns integer type so you should use an integer type to store it, not a string.
Here is the fixed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
int numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
I've just tested the program. You enter a number and you get a response. Works nicely. (For it's simple purpose :-) )
I googled and found the solution at MSDN.
// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";
// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);
file.Close();
How to extend the lines to complex content which including some natural C# code lines.
eg. I want to write the information below to my test.cs file.
Why?
I am parsing a XML schema with C# Console Application. And i want to generate the Console Result to a .cs file during the compiler time.
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonDef
{
public class CCODEData
{
public int iCodeId;
public string sCode;
public CODEDType cType;
public int iOccures;
}
[Description("CodeType for XML schema.")]
public enum CODEDType
{
cString = 1,
cInt = 2,
cBoolean = 3,
}
thank you.
If your source code is hardcoded as in your sample, you could use a C# literal string:
string lines =
#"using System;
using System.Collections.Generic;
using System.Text;
namespace CommonDef
..."
Anyway in such cases it is a better idea (more readable and maintainable) to have the whole text contents into a text file as an embedded resource in your assembly, then read it using GetManifestResourceStream.
(I'm assuming you're trying to build up the result programmatically - if you genuinely have hard-coded data, you could use Konamiman's approach; I agree that using an embedded resource file would be better than a huge verbatim string literal.)
In your case I would suggest not trying to build up the whole file into a single string. Instead, use WriteLine repeatedly:
using (TextWriter writer = File.CreateText("foo.cs"))
{
foreach (string usingDirective in usingDirectives)
{
writer.WriteLine("using {0};", usingDirective);
}
writer.WriteLine();
writer.WriteLine("namespace {0}", targetNamespace);
// etc
}
You may wish to write a helper type to allow simple indentation etc.
If these suggestions don't help, please give more details of your situation.
I know an answer has already been accepted but why not use an XSLT applied to the XML instead? this would mean that you could easily generate c#, vb.net, .net without having to recompile the app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileHandling
{
class Class1
{
static void Main()
{
Console.WriteLine("Enter data");
ConsoleKeyInfo k;
//Console.WriteLine(k.KeyChar + ", " + k.Key + ", " + k.Modifiers );
string str="";
char ch;
while (true)
{
k = Console.ReadKey();
if ((k.Modifiers == ConsoleModifiers.Control) && (k.KeyChar == 23))
{
Console.WriteLine("\b");
break;
}
if (k.Key == ConsoleKey.Enter)
{
Console.WriteLine("");
str += "\n";
}
ch = Convert.ToChar(k.KeyChar);
str += ch.ToString();
}
Console.WriteLine(str);
Console.Read();
}
}
}