I am trying to create a program where the application should never exit until I press option '6'. Below is my sample program: Right now, if I press any option, it executes the corresponding method and console window closes (exits the application) after it is done executing. I want the console to wait for the next option to be entered.
Console.WriteLine(#"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
You can just place your code into a loop, and change the return to break statements:
while(true) // Loop forever
{
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break; // Break, don't return
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
If you wish to prompt for input each iteration, you can move the prompt into the loop, as well.
using System;
namespace LoopUntilSelectionTester
{
class Program
{
static void Main()
{
string key;
while((key = Console.ReadKey().KeyChar.ToString()) != "6")
{
int keyValue;
int.TryParse(key, out keyValue);
ProcessInput(keyValue);
}
}
private static void ProcessInput(int keyValue)
{
switch (keyValue)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
}
}
}
}
First, what I'd do is make it into a method:
public void AskForInput()
{
Console.WriteLine(#"Select one of the following options:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
}
and then just use it like this:
for (;;) AskForInput();
Just wrap the whole thing in a while statement.
while (true)
{
Console.WriteLine(#"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
This worked for me:
Console.WriteLine(#"Select one of the following option:
1-Write Applications XML
2-Write Drivers XML
3-Write Operating Systems XML
4-Write Packages XML
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
while (true)
{
switch (selectionKey)
{
case 1:
DoApplications(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 2:
DoDrivers(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 3:
DoOperatingSystems(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 4:
DoPackages(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 5:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
}
}
Related
I can load a picture to a UImageView with I click a button, but not from a timer. why is that ? and how can I fix it ?
here is my code :
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
Patter_Function();
switch_Function();
}
void switch_Function()
{
image1 = new UIImage();
image1 = UIImage.FromBundle("heart");
image2 = new UIImage();
image2 = UIImage.FromFile( #"White_Sharingan.png");
image3 = new UIImage();
image3 = UIImage.FromFile("snow.png");
image4 = new UIImage();
//image4.//FromFile("Images/waterfront.jpg");
switch (Patter_1[switch_Counter].Move0) {
case 0 :
Box_1.Image = image1;
break;
case 1 :
Box_1.Image = image1;
break;
case 2 :
Box_2.Image = image1;
break;
case 3:
Box_3.Image = image1;
break;
case 4:
Box_4.Image = image1;
break;
case 5:
Box_5.Image = image1;
break;
case 6:
Box_6.Image = image1;
break;
case 7:
Box_7.Image = image1;
break;
case 8:
Box12.Image = image1;
break;
case 9:
Box_9.Image = image1;
break;
case 10:
Box_10.Image = image1;
break;
case 11:
Box_11.Image = image1;
break;
case 12:
Box8.Image = image1;
break;
case 13:
Box_13.Image = image1;
break;
case 14:
Box_14.Image = image1;
break;
case 15:
Box_15.Image = image1;
break;
case 16:
Box_16.Image = image1;
break;
case 17:
Box_17.Image = image1;
break;
case 18:
Box_18.Image = image1;
break;
case 19:
Box_19.Image = image1;
break;
case 20:
Box_20.Image = image1;
break;}
switch (Patter_1[switch_Counter].Move1)
{
case 0:
Box_1.Image = image2;
break;
case 1:
Box_2.Image = image2;
break;
case 2:
Box_2.Image = image2;
break;
case 3:
Box_3.Image = image2;
break;
case 4:
Box_4.Image = image2;
break;
case 5:
Box_5.Image = image2;
break;
case 6:
Box_6.Image = image2;
break;
case 7:
Box_7.Image = image2;
break;
case 8:
Box12.Image = image2;
break;
case 9:
Box_9.Image = image2;
break;
case 10:
Box_10.Image = image2;
break;
case 11:
Box_11.Image = image2;
break;
case 12:
Box8.Image = image2;
break;
case 13:
Box_13.Image = image2;
break;
case 14:
Box_14.Image = image2;
break;
case 15:
Box_15.Image = image2;
break;
case 16:
Box_16.Image = image2;
break;
case 17:
Box_17.Image = image2;
break;
case 18:
Box_18.Image = image2;
break;
case 19:
Box_19.Image = image2;
break;
case 20:
Box_20.Image = image2;
break;
}
switch (Patter_1[switch_Counter].Move2)
{
case 0:
Box_1.Image = image3;
break;
case 1:
Box_2.Image = image3;
break;
case 2:
Box_2.Image = image3;
break;
case 3:
Box_3.Image = image3;
break;
case 4:
Box_4.Image = image3;
break;
case 5:
Box_5.Image = image3;
break;
case 6:
Box_6.Image = image3;
break;
case 7:
Box_7.Image = image3;
break;
case 8:
Box12.Image = image3;
break;
case 9:
Box_9.Image = image3;
break;
case 10:
Box_10.Image = image3;
break;
case 11:
Box_11.Image = image3;
break;
case 12:
Box8.Image = image3;
break;
case 13:
Box_13.Image = image3;
break;
case 14:
Box_14.Image = image3;
break;
case 15:
Box_15.Image = image3;
break;
case 16:
Box_16.Image = image3;
break;
case 17:
Box_17.Image = image3;
break;
case 18:
Box_18.Image = image3;
break;
case 19:
Box_19.Image = image3;
break;
case 20:
Box_20.Image = image3;
break;
}
switch (Patter_1[switch_Counter].Move3)
{
case 0:
Box_1.Image = image4;
break;
case 1:
Box_2.Image = image4;
break;
case 2:
Box_2.Image = image4;
break;
case 3:
Box_3.Image = image4;
break;
case 4:
Box_4.Image = image4;
break;
case 5:
Box_5.Image = image4;
break;
case 6:
Box_6.Image = image4;
break;
case 7:
Box_7.Image = image4;
break;
case 8:
Box12.Image = image4;
break;
case 9:
Box_9.Image = image4;
break;
case 10:
Box_10.Image = image4;
break;
case 11:
Box_11.Image = image4;
break;
case 12:
Box8.Image = image4;
break;
case 13:
Box_13.Image = image4;
break;
case 14:
Box_14.Image = image4;
break;
case 15:
Box_15.Image = image4;
break;
case 16:
Box_16.Image = image4;
break;
case 17:
Box_17.Image = image4;
break;
case 18:
Box_18.Image = image4;
break;
case 19:
Box_19.Image = image4;
break;
case 20:
Box_20.Image = image4;
break;
}
switch_Counter++;
if (switch_Counter >4 )
{
switch_Counter = 0;
}
}
I used the debugger, so I know the code is getting to the line
but it is not doing anything when it get there
the bebugger says:
UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoke from the UI Thread.
Is there a why around that ?
Any operation you involve in modifying UI view, you will need to do it under UI/Main thread. So put the code inside the lambda of:
InvokeOnMainThread ( () => {
// manipulate UI controls
});
Actually this post is different from this (posted by me as well). I've a phone keypad in my UI and now I'm able to show the button is pressed when its corresponding key is down.
private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D2:
case Key.NumPad2:
TwoBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D3:
case Key.NumPad3:
ThreeBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D4:
case Key.NumPad4:
FourBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D5:
case Key.NumPad5:
FiveBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D6:
case Key.NumPad6:
SixBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D7:
case Key.NumPad7:
SevenBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D8:
case Key.NumPad8:
EightBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D9:
case Key.NumPad9:
NineBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
}
if (((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift))
{
if (e.Key == Key.D3)
{
ThreeBtn.Style = (Style)FindResource("NormalButtonStyle");
HashBtn.Style = (Style)FindResource("PressedButtonStyle");
}
}
}
private void NumDisplayBox_PreviewKeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("ButtonStyle4"); break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("ButtonStyle4"); break;
.
.
.
}
}
The problem is when hash key (Shift + 3) or star key (Shift + 8) is down, the '3' button (for case hash key) also be pressed in UI. How can I show that only hash button is pressed on UI when Shift + 3 are down, but not hash button together with '3' button?
Check the KeyEventArgs.Modifiers property, and only handle the key-down when no modifiers are set.
Alternatively, use the TextInput or PreviewTextInput event to get the composed text instead of the actual key. This may be preferable anyway, as it will respond to any method of entering the appropriate text input, rather than relying on a specific key (for example, you won't have to have cases for all the possible keys, such as the top-row numeric keys and the numeric pad keys, as you are now).
This works for me:
//Make the button to have "pressed" feel when the corresponding key is pressed
private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift))
{
if (e.Key == Key.D3)
HashBtn.Style = (Style) FindResource("PressedButtonStyle");
if (e.Key == Key.D8)
StarBtn.Style = (Style) FindResource("PressedButtonStyle");
}
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D2:
case Key.NumPad2:
TwoBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D3:
case Key.NumPad3:
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
ThreeBtn.Style = (Style) FindResource("ButtonStyle4");
else
ThreeBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
}
case Key.D4:
case Key.NumPad4:
FourBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D5:
case Key.NumPad5:
FiveBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D6:
case Key.NumPad6:
SixBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D7:
case Key.NumPad7:
SevenBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
case Key.D8:
case Key.NumPad8:
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
EightBtn.Style = (Style) FindResource("ButtonStyle4");
else
EightBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
}
case Key.D9:
case Key.NumPad9:
NineBtn.Style = (Style) FindResource("PressedButtonStyle");
break;
default:
break;
}
}
//Return back to its original style
private void NumDisplayBox_PreviewKeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.NumPad0:
ZeroBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D1:
case Key.NumPad1:
OneBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D2:
case Key.NumPad2:
TwoBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D3:
case Key.NumPad3:
ThreeBtn.Style = (Style) FindResource("ButtonStyle4");
HashBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D4:
case Key.NumPad4:
FourBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D5:
case Key.NumPad5:
FiveBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D6:
case Key.NumPad6:
SixBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D7:
case Key.NumPad7:
SevenBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D8:
case Key.NumPad8:
EightBtn.Style = (Style) FindResource("ButtonStyle4");
StarBtn.Style = (Style) FindResource("ButtonStyle4");
break;
case Key.D9:
case Key.NumPad9:
NineBtn.Style = (Style) FindResource("ButtonStyle4");
break;
default:
break;
}
}
This is what I have so far. My problem is that none of the cases are responding when you enter either the correct or incorrect answer. I'm not really sure where to go from here. The program asks you answer two random numbers being multiplied. And then it should give you one of the eight responses.
int result = 0;
int caseSwitch = 0;
string question = DoMultiplication(out result);
Console.WriteLine(question);
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == result)
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("Very Good");
break;
case 2:
Console.WriteLine("Excellent");
break;
case 3:
Console.WriteLine("Nice Work");
break;
case 4:
Console.WriteLine("Keep up the good work!");
break;
}
}
else
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("No, Please Try Again.");
break;
case 2:
Console.WriteLine("Wrong, Try Once More");
break;
case 3:
Console.WriteLine("Don't Give Up!");
break;
case 4:
Console.WriteLine("No, Keep Trying!");
break;
caseSwitch is always 0, so your switch will always fall through without writing anything to console.
If you want a random response you could do something like this:
int result = 0;
int caseSwitch = new Random().Next(1, 4);
string question = DoMultiplication(out result);
Console.WriteLine(question);
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == result)
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("Very Good");
break;
case 2:
Console.WriteLine("Excellent");
break;
case 3:
Console.WriteLine("Nice Work");
break;
case 4:
Console.WriteLine("Keep up the good work!");
break;
}
}
else
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("No, Please Try Again.");
break;
case 2:
Console.WriteLine("Wrong, Try Once More");
break;
case 3:
Console.WriteLine("Don't Give Up!");
break;
case 4:
Console.WriteLine("No, Keep Trying!");
break;
CaseSwitch is always = 0.
You need to assign a value to it, and-or add a default case to your switch.
You have your int caseSwitch = 0; and I don't see you changing it in your code to any of 1-4. So what do you expect it to do if you dont have the caseSwitch changed...
I just need to know when the media player finishes of playing a song, if there is a flag or something...
According to MSDN, you should be able to use the PlayStateChanged event. The event is AxWMPLib._WMPOCXEvents_PlayStateChangeEvent
See the enumeration reference here . It seems that you can use wmppsMediaEnded to find out when the media stream has ended.
I think this gives an example in VB.net, maybe you can adapt it for your purpose: http://msdn.microsoft.com/en-us/library/dd562692(v=vs.85).aspx
EDIT: Just noticed there's a c# solution below the VB example.
Check code implementation of playstateChanged event Here
// Add a delegate for the PlayStateChange event.
player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
// Test the current state of the player and display a message for each state.
switch (e.newState)
{
case 0: // Undefined
currentStateLabel.Text = "Undefined";
break;
case 1: // Stopped
currentStateLabel.Text = "Stopped";
break;
case 2: // Paused
currentStateLabel.Text = "Paused";
break;
case 3: // Playing
currentStateLabel.Text = "Playing";
break;
case 4: // ScanForward
currentStateLabel.Text = "ScanForward";
break;
case 5: // ScanReverse
currentStateLabel.Text = "ScanReverse";
break;
case 6: // Buffering
currentStateLabel.Text = "Buffering";
break;
case 7: // Waiting
currentStateLabel.Text = "Waiting";
break;
case 8: // MediaEnded
currentStateLabel.Text = "MediaEnded";
break;
case 9: // Transitioning
currentStateLabel.Text = "Transitioning";
break;
case 10: // Ready
currentStateLabel.Text = "Ready";
break;
case 11: // Reconnecting
currentStateLabel.Text = "Reconnecting";
break;
case 12: // Last
currentStateLabel.Text = "Last";
break;
default:
currentStateLabel.Text = ("Unknown State: " + e.newState.ToString());
break;
}
}
I need to combine a switch with an if-statement.
How can I do that? I want to do something like this:
switch (periodtype)
{
if(starttime>endtime)
{
;
}
else
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
What's wrong with:
if(starttime<=endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
if(starttime>endtime)
{
// Stuff
}
else
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
Like that?
if(starttime>endtime)
{
;
}
else
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
if(starttime <= endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
Hmm way too late with answer
isnt this easier:
if (starttime <= endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}