I am developing a console application, that should listen for digits from a numpad keyboard in both num lock states - on and off. The application is running on Raspberry Pi with Arch Linux and Mono. Since I did not found a way, that is compiling under Mono, to permanently turn numlock on, I am using the following method to convert num pad commands to digits:
private string ReadNumPadSymbol(ConsoleKeyInfo keyInfo)
{
char editedSymbol;
switch (keyInfo.Key)
{
case ConsoleKey.Insert:
editedSymbol = '0';
break;
case ConsoleKey.End:
editedSymbol = '1';
break;
case ConsoleKey.DownArrow:
editedSymbol = '2';
break;
case ConsoleKey.PageDown:
editedSymbol = '3';
break;
case ConsoleKey.LeftArrow:
editedSymbol = '4';
break;
case ConsoleKey.Clear:
editedSymbol = '5';
break;
case ConsoleKey.RightArrow:
editedSymbol = '6';
break;
case ConsoleKey.Home:
editedSymbol = '7';
break;
case ConsoleKey.UpArrow:
editedSymbol = '8';
break;
case ConsoleKey.PageUp:
editedSymbol = '9';
break;
default:
return String.Empty;
}
return editedSymbol.ToString();
}
It works as expected under Windows, but under Linux, the method returns empty string, when the "5" button is pressed. For some reason it does not enters the ConsoleKey.Clear case. Is there a fix for this?
Thanks!
Related
I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.
Here is my code:
using System;
namespace SwitchStatements
{
class MainClass
{
public static void Main(string[] args)
{
Start:
Console.WriteLine("Please Input the Grade");
char grade = Convert.ToChar(Console.ReadLine());
switch (grade)
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
Console.ReadKey();
goto Start;
}
}
}
If I put 'a' in instead of 'A' it returns the default response.
Can I use perhaps a .Comparison of some sort? If so where would I put it?
You can use ConsoleKey as condition for switch, the code will be like the following.
var grade =Console.ReadKey().Key;
switch (grade)
{
case ConsoleKey.A:
Console.WriteLine("Excellent Work!");
break;
case ConsoleKey.B:
// Something here
break;
case ConsoleKey.C:
// Something here
break;
case ConsoleKey.D:
// Something here
break;
case ConsoleKey.E:
// Something here
break;
default:
// Something here
break;
}
So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.
You can use ToUpper(); Like
Convert.ToChar(Console.ReadLine().ToUpper());
and to get saved from the error of getting more charaters with Console.ReadLine() you can use
char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());
you can use like following also
char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());
https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx
Convert to uppercase before switch like below,
grade = Char.ToUpper(grade);
Change
char grade = Convert.ToChar(Console.ReadLine());
To
char grade = Convert.ToChar(Console.ReadLine().ToUpper());
https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx
Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after
char grade = Convert.ToChar(Console.ReadLine());
switch (grade.ToUpper())
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
You may fall from one case to another like this
public static void Main(string[] args)
{
Boolean validInputRead = false;
Char grade;
while(!validInputRead)
{
validInputRead = true;
Console.WriteLine("Please Input the Grade");
grade = Convert.ToChar(Console.Read());
switch (grade)
{
case 'A':
case 'a':
Console.WriteLine("Excellent Work!");
break;
case 'B':
case 'b':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
case 'c':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
case 'd':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
validInputRead = false;
break;
}
Console.ReadKey();
}
}
EDIT
Changed from Console.ReadLine() to Console.Read() as suggested
Added while(!validInputRead) as requested
string letterGrade;
int grade = 0;
// This will hold the final letter grade
Console.Write("Input the grade :");
switch (grade)
{
case 1:
// 90-100 is an A
letterGrade = "A";
Console.WriteLine("grade b/n 90-100");
break;
case 2:
// 80-89 is a B
letterGrade = "B";
Console.WriteLine("grade b/n 80-89");
break;
case 3:
// 70-79 is a C
letterGrade = "C";
Console.WriteLine("grade b/n 70-79");
break;
case 4:
// 60-69 is a D
letterGrade = "D";
Console.WriteLine(" grade b/n 60-69 ");
break;
default:
// point whic is less than 59
Console.WriteLine("Invalid grade");
break;
}
I have a lstYourHand that has two cards in it, I loop through the listbox to get the values of both cards. I take the string value of the listbox item (strCardVal) and use a switch to give it an integer value (intCardVal). For some reason, when I run the code, the message Box at the end gives me the value 0 as a result, it does not register me giving it a value in the switch statement. My code is below:
Int32 intCardVal = 0;
String strCardVal;
Int32 intLoopCounter1;
for (intLoopCounter1 = 0; intLoopCounter1 == 1; intLoopCounter1++)
{
strCardVal = lstYourHand.SelectedItem.ToString();
switch (strCardVal)
{
case "2":
intCardVal = 2;
break;
case "3":
intCardVal = 3;
break;
case "4":
intCardVal = 4;
break;
case "5":
intCardVal = 5;
break;
case "6":
intCardVal = 6;
break;
case "7":
intCardVal = 7;
break;
case "8":
intCardVal = 8;
break;
case "9":
intCardVal = 9;
break;
case "10":
case "J":
case "Q":
case "K":
intCardVal = 10;
break;
case "A":
intCardVal = 11;
break;
}
}
MessageBox.Show(intCardVal.ToString());
Have a look at this part of your code:
for (intLoopCounter1 = 0; intLoopCounter1 == 1; intLoopCounter1++)
In fact this loop body never will be executed because of loop execution condition intLoopCounter1 == 1 (which is false at the very first iteration since intLoopCounter1 == 0 in the beginning) - so your intCardVal will not be modified.
I think you've kept in mind intLoopCounter1 <= 1 here.
Also note (as it was mentioned in comments) - this kind of errors is pretty easy can be found by using debugger.
I currently have a WPF window with numerous textboxes/buttons. Currently when trying to use the TAB key to navigate between the objects you hear a 'DING' and the focus is not changed to the next object in the TabIndex.
Here is what the window I have looks like with the TabIndex numbers displayed.
All the objects have TabStop set to True.
Not the prettiest solution but it works.
On Form Initialization call
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent);
Then utilize this function to grab the TAB key and process the focus.
private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event
{
if (e.KeyCode == Keys.Tab)
{
++iFocusCount;
}
else if (e.KeyCode == Keys.Tab && e.KeyCode == Keys.Shift)
{
--iFocusCount;
}
switch (iFocusCount)
{
case 0:
contactBox.Focus();
break;
case 1:
incidentBox.Focus();
break;
case 2:
actionsListBox.Focus();
break;
case 3:
profilesListBox.Focus();
break;
case 4:
currentLatchBox.Focus();
break;
case 5:
daysBox.Focus();
break;
case 6:
calculateDateButton.Focus();
break;
case 7:
copyButton.Focus();
break;
case 8:
notesTextBox.Focus();
break;
case 9:
keycodeBox.Focus();
break;
case 10:
xnaBox.Focus();
break;
case 11:
generateTamButton.Focus();
break;
case 12:
generateNotesButton.Focus();
break;
case 13:
sendEmailButton.Focus();
break;
case 14:
saveButton.Focus();
break;
case 15:
clearLabel.Focus();
break;
case 16:
iFocusCount = 0;
contactBox.Focus();
break;
}
}
This still produces the "DING" but the focus changes which is what I wanted in the first place.
i have problems when adding mullti values into same case:
this is my c# code
string input = combobox1.selectedvalue.ToString();
switch(input)
{
case "one";
return 1;
break;
case "two";
return 2;
break;
case "three" , "four": // error here
return 34;
break;
default:
return 0;
}
need your help for
Just use separate labels:
string input = combobox1.selectedvalue.ToString();
switch(input)
{
case "one":
return 1;
break;
case "two":
return 2;
break;
case "three":
case "four":
return 34;
break;
default:
return 0;
}
See switch:
Each switch section contains one or more case labels followed by one or more statements
You can you the fall though, read this for more information
so it's look like this
switch(input)
{
case "one":
return 1;
break;
case "two":
return 2;
break;
case "three":
case "four":
return 34;
break;
default:
return 0;
}
Right syntax is
case "three":
case "four":
return 34;
break;
instead
case "three" , "four":
return 34;
break;
From switch (C# Reference)
A switch statement includes one or more switch sections. Each switch
section contains one or more case labels followed by one or more
statements.
The problem states "Write a program that accepts a 10-digit telephone number that may contain one or more alphabetic characters. Display the corresponding number using numerals...etc"
ABC:2 through WXYZ:9
This chapter teaches about loops but I found myself really lost at this problem. I completed the code but I think it sucks...
My question: Is there a better way to shorten this code up? And I only figured to use the c# keyword case, is there another way?
EDIT: Arabic as in you could type in 1800WALLTO and it will give you 1800925586
ALSO I am not asking for a code that doesn't work, this does EXACTLY what I want and asked it to do. I am just asking for any advise or input on how to make it better. I really wanted to know a way to do it without switch and case break etc...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
char userInput = ' ';
string inputString = "",
outputString = "";
Console.WriteLine("Enter the digits from the phone number");
do
{
userInput = Console.ReadKey(false).KeyChar;
inputString += userInput;
if (Char.IsLetter(userInput))
userInput = userInput.ToString().ToUpper().ToCharArray()[0];
switch (userInput)
{
case '1':
outputString += '1';
x++;
break;
case '2':
case 'A':
case 'B':
case 'C':
outputString += '2';
x++;
break;
case '3':
case 'D':
case 'E':
case 'F':
outputString += '3';
x++;
break;
case '4':
case 'G':
case 'H':
case 'I':
outputString += '4';
x++;
break;
case '5':
case 'J':
case 'K':
case 'L':
outputString += '5';
x++;
break;
case '6':
case 'M':
case 'N':
case 'O':
outputString += '6';
x++;
break;
case '7':
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += '7';
x++;
break;
case '8':
case 'T':
case 'U':
case 'V':
outputString += '8';
x++;
break;
case '9':
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += '9';
x++;
break;
case '0':
outputString += '0';
x++;
break;
default:
Console.WriteLine("You entered an incorrect value-Try again");
x--;
break;
}
}
while (x < 10);
Console.WriteLine("\nYou entered {0}", inputString);
Console.WriteLine("Your number is {0}", outputString);
}
}
}
Use a System.Collections.Generic.Dictionary where the dictionary keys are the characters A-Z and 0-9 and the values are the corresponding numbers:
var lookup = Dictionary<char, int> {
{'A',2},
{'B',2},
// etc...
{'Z', 9},
{'1':1},
{'2':2}
// etc... include the numerals so you don't have to converts some things to char not the rest...
};
// to lookup a character:
char item = 'A';
int number = lookup['A'];
To decode an phone number just split it into an array of char's and look them up one after the other
List<int> digits = new List<int>();
foreach (char c in inputString)
{
digits.Add(lookup[c]);
}
Im sure somebody will post a 1-liner using LINQ as well, but this is the vanilla version.
Using a string look up would shorten the code:--
String decode "--------------------------------01234567890------2223334445556667778889999---------------------------------"; //256 char string
numout = decode.substring((int) Char.GetNumericValue(userinput),1);
But it would be a lot less efficient than using a "case" statement. Less code does not mean less cpu.