I am new to StackOverflow, something may not be correct in this post.
I have the following sample code to describe my question:
using System;
sealed public class Program
{
public static void Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("F");
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("ile - ");
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("E");
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("dit - ");
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("C");
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("lose");
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - -");
Console.WriteLine("This is just some sample code.");
Console.WriteLine("Now I will generate a little box that requires a hover effect.");
Console.WriteLine("Basically I want its color to be changed to red upon hovering on it, or better - triggering some function.");
Console.WriteLine("I don't think this is possible with console application, but just asking in case if it is still possible.");
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
Console.WriteLine("###################################");
}
}
This little sample-code will generate some text and a gray box. Now, when a user puts their mouse pointer over a gray box, I'd like the gray box to change its color to red, or anything similar - such as detecting it and doing specific operation, mainly triggering a function. I don't think this is possible with C# Console Application, but just asking in case if it is still possible.
Important to read
Although there are many similar questions, they're using WinForms, or XAML interface. I personally like to use Console Application here. I don't use Visual Studio even though I have it, and so is the reason why I am not using WinForms at all. This isn't important to know, but just so you don't tell me to do so. If a reason required, add a comment about this. I haven't found a similar question to this that is relevant to .NET Framework C# Console Application after 15 minutes of searching. Under "Do any of these posts answer your question" menu, I've got nothing to even close similar to my question.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thanks in advance. :)
I tried adding some kind of function, while loop, or adding DLL reference (using .NET Framework csc.exe feature). I quickly got confused and got no idea how to do this.
.NET and C# does not provide a built-in way for reading such mouse events. Although it might be possible to achieve using Windows API, I highly doubt that it's worth it. I suggest trying out any GUI framework such as Windows Forms, WPF or Avalonia UI. It will much easier to achieve and will save you a lot of time.
Ok, this is no Hover effect, but on a (Windows) Console you can draw users attention by using Blinking text.
#echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
echo %ESC%[5mBlink%ESC%[0m
Save above as blink.bat, and run it on a CMD prompt. It will show the text "Blink", which is blinking.
More info, also on the use of colors: C# - Make text blinking in console using ANSI codes
Blinking text in C#:
char esc = (char)27;
string Hello = $"{esc}[5mHello World!{esc}[0m";
Console.WriteLine($"{Hello}");
Finally: Sorry to say this but flashing text is probably not a sensible thing to be doing with a console application. 😉
Related
This question already has answers here:
How to stop C# console applications from closing automatically? [duplicate]
(9 answers)
Closed 2 years ago.
static void Main(string[] args)
{
string name01;
string name02;
Console.WriteLine("Welcome to Choice RPG! <press enter to start your adventure>");
Console.ReadKey();
Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
name01 = Convert.ToString(Console.ReadLine());
Console.WriteLine(name01 + "! come on, you can think of a better name than that can't you? <press enter to continue>");
Console.ReadKey();
Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
name02 = Convert.ToString(Console.ReadLine());
if (name02 == name01)
{
Console.WriteLine("Wow, i guess you really like that name huh, fine.");
}
else
{
Console.WriteLine("Now thats more like it!");
}
}
This is my first project with programming in general. I am making an RPG game in which you choose your adventure and this is for the user's ingame name.
Everything works up until the if statement (after that the cmd just closes). If anyone has any ideas to fix it please tell me.
Code looks OK to me, try adding:
Console.WriteLine("Press <ENTER> to exit");
Console.Readline();
at the end to see if the message you want to show, actually shows before the application exits and closes. My guess is you are running in Visual Studio, and the message prints, but then the execution window closes so fast you don't see it.
I'm following this tutorial https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169?l=83b9cRQIC_9706218949
and I can't get the program working although I have copied the exact code as in the tutorial.
I have Ubuntu 16.04 so I'm using Visual Studio Code. And I have .NET SDK version 2.1.403.
Here's the code for my program:
using System;
namespace Decision
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Miina's Big Giveaway");
Console.Write("Choose a door: 1, 2 or 3 ");
string userValue = Console.ReadLine();
if (userValue == "1")
{
string message = "You won a new car!";
Console.WriteLine(message);
}
Console.ReadLine();
}
}
}
The problem is that the program isn't writing the line "Choose a door...". Only when I stop the execution of the program, the line "Choose a door ..." appears in the debug terminal.
And if I try typing "1" while the program is still running nothing happens although it should go through the commands in the if statement. I can't figure out where's the problem.
Update on the debugging
When I'm debugging a light bulb appears next to the Console.Write -line. I'm not sure what that means.
Picture of the debugging result
Update
The program is working correctly when I run it through the terminal. So I guess I have to use the terminal with the Visual Studio Code. But it would be nice to use debugger, so if anyone knows how I could get it to work, let me know.
I suspect that Console.Write isn't being flushed (stdout on Linux is unbuffered, and is only flushed on a line ending).
Try Console.Out.Flush() as a work-around. It's not pretty, though.
Recently i have decided to learn c# using the IDE VB platform. I downloaded the open sourced software on the Microsoft website however i have had constant problems since none of which i received with any Java alternative.
I have downloaded and uninstalled it twice, the first time i try to run any form of class even a blank program. I will get a warning stating a Trojan Horse is present (Luckily my AVG managed to get it).
Then when ever i try to run the file, i will get the error code CS2012, and nothing happens.
I am wondering does anyone have any ideas on how to fix it or potentially know of any other free software for c#.
I have included the code which I am using below.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
static void GetUserData()
{
Console.Write("please enter your name: ");
string userName = Console.ReadLine();
Console.Write("please enter your age: ");
string userAge = Console.ReadLine();
//changes echo colour
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.red;
//echo to console
Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);
//Restore previous color
Console.ForegroundColor = prevColor;
}
}
Thanks Joe
This is actually a common issue:
I guess your virus scanner detects YOUR program as suspicious and prevents the execution (or kills it right away). I have the same problem with Avast AntiVirus (although it does give me a very understandable error message that it is MY program, which it is not sure about).
Solution: Turn off the virus check in the folder you are programming at (i.e. the folder with the solution of the project). I don't know AVG personally, but you should be able to set some sort of exception which are not included in the scan process.
Note: I do not take any responsibility if it is really a virus of some sort.
I began to study C#, but I ran into a problem. The ReadLine() method does not pause for user input. I just started C#, and it works on other people's programs, so I have no idea why. I am using Xamarin on a Mac.
Here is an example code that does not work:
using System;
namespace LearningC
{
public class LearningSharp
{
public static void Main (String[] args)
{
Console.WriteLine (Console.ReadLine ());
}
}
}
I found out the solution. Right click on your project and select Options. Then, select General under the Run tab and check "Pause console output" ("Run on external console" must be checked for this to work.)
Maybe your Project is set to be a Windows application. Right click on your Project name, click properties and change output type to Console Application.
I have the same problem of a C# project not stopping with Console.ReadLine().
Here is my code:
char myChar1;
string userName;
string userLastname;
Console.Write($"Insert a char: ");
myChar1 = (char)Console.Read();
Console.WriteLine($"Press ENTER to continue");
Console.ReadLine();
Console.Clear();
Console.Write($"Insert name: ");
userName = Console.ReadLine();
Console.Write($"Insert lastname: ");
userLastname = Console.ReadLine();
// More stuff here
I found that when I use the method Console.Read() the next method Console.ReadLine() does not stop at all.
So after inserting a char in the variable myChar1 the program goes forward and stops only after the method Console.Write($"Insert name: ");
The workaround is to place two Console.ReadLine() methods before the Console.Clear() one but I can not understand why this goes like that.
I am new to programming and as seems to be traditional I tried to create a "hello world" program in C#; however, as soon as I run the program it closes.
This is my code inside:
main()
console.writeline("hello world");
console.writeline("enter name");
console.writeline("where is the frikin console");
It's really annoying and I know it might be something simple for the additional users but how do I keep the window open.
Use Console.ReadLine(); or Console.ReadKey(); at the end of your program to wait for the return key or for any key.
You can build your program and run the exe from the command line, that will allow you to see the output.
If you want the program to remain running then adding the Read() statement is the traditional approach, as others have already said.
If you just want to see it in debugging and do not want or need the read statement then place a breakpoint at the end of the program during a debug session.
It's really quite simple.
After this line of code:
Console.WriteLine("where is the frikin console");
You need to add this:
Console.ReadLine();
That should work.
The reason the console closes is because you told it to write some stuff to the screen, after it has finished writing what you told it to write it simply closes itself all in the fraction of a second. if you add Console.ReadLine, the console will wait for you to input something before closing, like pressing a key on the keyboard.
Try adding Console.Read(). You need to pause execution somehow.
Console.WriteLine("hello world");
Console.WriteLine("enter name");
Console.WriteLine("where is the frikin console");
Console.ReadLine();
Console.ReadLine(); will close the console after you've hit (for example) enter.
Console.ReadKey(); will close the console after the next key-hit
You can read the console-contents with these methods,too
Console.WriteLine("hello world");
Console.WriteLine("enter name");
string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);
Console.ReadLine();
add below line at the end
Console.ReadLine();
You could use CTRL + F5 which will opens the command line and after execution of your code, it shows Press any key to continue.... This will be handy for you than adding few lines of code additionally.
Use
Console.ReadLine();
in the end of your code. You are having this problem because the program just write the message then it ends, that's why you can't see anything. By adding that line, you keep the program waiting something to be typed and you can read the message. After this, type something to end the program.
The window automatically closes after your program, you need to let it take some input, for example:
Add
Console.ReadLine();
Which takes a line of input (till "\n"). And your program will wait until somebody hit the return key (they can type anything and the program won't close: until you hit the return key. You can type in "hello world back what's up are you ok?" and nothing will happen.)
or
Console.ReadKey();
Which will take a character of input. This will make your program wait for the user to press any key and then closes.