Write random text into Textbox - c#

I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}

Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}

Related

Can't show all nodes of TreeView

I have the following Code down here, but when I try to add a child to parent it causes an error when parentID is bigger than 4.
public void LoadNodes()
{
ConnectionShorten("TreeViewTable");
int H = MyNodes.Tables["TreeViewTable"].Rows.Count;
for (int i = 0; i < H; i++)
{
int PID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("ParentID");
string Name = MyNodes.Tables["TreeViewTable"].Rows[i].Field<string>("RootName");
int Level = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("Level");
int UID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("UID");
switch (Level)
{
case 0:
treeView1.Nodes.Add(Name.ToString());
break;
case 1:
treeView1.Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
switch (PID)
{
case 1:
treeView1.Nodes[0].Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
treeView1.Nodes[0].Nodes[1].Nodes.Add(Name.ToString());
break;
case 3:
treeView1.Nodes[0].Nodes[2].Nodes.Add(Name.ToString());
break;
case 4:
treeView1.Nodes[0].Nodes[3].Nodes.Add(Name.ToString());
break;
case 5:
treeView1.Nodes[0].Nodes[4].Nodes.Add(Name.ToString());
break;
case 6:
treeView1.Nodes[0].Nodes[5].Nodes.Add(Name.ToString());
break;
case 7:
treeView1.Nodes[0].Nodes[6].Nodes.Add(Name.ToString());
break;
case 8:
treeView1.Nodes[0].Nodes[7].Nodes.Add(Name.ToString());
break;
case 9:
treeView1.Nodes[0].Nodes[8].Nodes.Add(Name.ToString());
break;
case 10:
treeView1.Nodes[0].Nodes[9].Nodes.Add(Name.ToString());
break;
case 11:
treeView1.Nodes[0].Nodes[10].Nodes.Add(Name.ToString());
break;
default:
break;
}
break;
case 3:
break;
default:
break;
}
}
}
It does load 1 main node and 10 parents, then when it comes to child it says a negative index, but I'm sure the parent does exist!
Also how can I make more levels with no difficulty like this, because I think it's very difficult to make levels > 3.
You can close it, I just found that problem and solved it.
It was because there is a jump on ID on the DB. So I try to add child before I even add the parent. my program is read by ID so thats it ... sorry for this.

Switch statement using text box within forms

I am building a form that takes an input number from a text box and then will take the number that was input, and display the roman numeral equivalent in another text box.
My Form:
private void convertButton_Click(object sender, EventArgs e)
{
int numberInput;
switch (numberInput)
This is where I keep getting an error code. The "switch (numberInput)" is seen as and unassigned local variable. How do I assign it so that it will be able to access all of the case integers?
{
case 1:
outputTextBox.Text = "I";
break;
case 2:
outputTextBox.Text = "II";
break;
case 3:
outputTextBox.Text = "III";
break;
case 4:
outputTextBox.Text = "IV";
break;
case 5:
outputTextBox.Text = "V";
break;
case 6:
outputTextBox.Text = "VI";
break;
case 7:
outputTextBox.Text = "VII";
break;
case 8:
outputTextBox.Text = "VIII";
break;
case 9:
outputTextBox.Text = "IX";
break;
case 10:
outputTextBox.Text = "X";
break;
default:
MessageBox.Show("Please enter and number between 1 and 10. Thank you!");
break;
}
Cause your variable is not assigned yet int numberInput; and so the error. You said input is coming from a TextBox, in that case do like below assuming textbox1 is your TextBox control instance name
int numberInput = Convert.ToInt32(this.textbox1.Text.Trim());
Convert.ToInt32 may throw an exception if the parsing is unsuccessful. Another method is to Int.Parse:
int numberInput = int.Parse(textbox1.Text.Trim());
or better yet
int numberInput;
if(int.TryParse(textbox1.Text.Trim(), out numberInput))
{
switch (numberInput)
...
}

C# Case Sensitivity in Switch-statement

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;
}

'DING' On TabStop in C#/WPF

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.

image not displaying

I hope you can help me! I am trying to make a game similiar to candyland. I want the die to spin when the user clicks the button. A random number is chosen and based on that number, the dice displays the image for that number. That works! Then, I want our user to be able to move forward on our board- based on the spot that they're on, it adds whatever they spinned and the image on that spot becomes visible. When in debug mode, everything works perfectly but for some reason, the pawn never moves! Can you please tell me why. I am attaching my code below. Thank you so much!
protected void btnSpin_Click(object sender, EventArgs e)
{
Random randomNumber = new Random();
int x = randomNumber.Next(1, 6);
switch (x)
{
case 1:
//imgDie.ImageUrl = "~/Images/dice1.jpg";
Session["Die"] = "~/Images/dice1.jpg";
break;
case 2:
Session["Die"] = "~/Images/dice2.jpg";
break;
case 3:
Session["Die"] = "~/Images/dice3.jpg";
break;
case 4:
Session["Die"] = "~/Images/dice4.jpg";
break;
case 5:
Session["Die"] = "~/Images/dice5.jpg";
break;
case 6:
Session["Die"] = "~/Images/dice6.jpg";
break;
}
imgDie.ImageUrl = (string)Session["Die"];
place = place + x;
switch (place)
{
case 2:
img2.Visible = true;
img2.ImageUrl = (string)Session["Imagesrc"];
break;
case 3:
img3.Visible = true;
img3.ImageUrl = (string)Session["Imagesrc"];
break;
case 4:
img4.Visible = true;
img4.ImageUrl = (string)Session["Imagesrc"];
break;
case 5:
img5.Visible = true;
img5.ImageUrl = (string)Session["Imagesrc"];
break;
case 6:
img6.Visible = true;
img6.ImageUrl = (string)Session["Imagesrc"];
break;
case 7:
img7.Visible = true;
img7.ImageUrl = (string)Session["Imagesrc"];
break;
case 8:
img8.ImageUrl = (string)Session["Imagesrc"];
img8.Visible = true;
break;
my guess is your 'place' variable is a member field and it's being reinitialized with each page construction. chnge your place variable to be viewstate or session state like your other stuff.

Categories