I'd like to run my switch and if/else statement again after the buttons are clicked three times in total.
The current pressed code is for three buttons with each 1 value. If those (current pressed code) are equal to the global string that contains a 3 number value, the picture box color will change to forest green. This is my code:
switch ((sender as Button).Text)
{
case "1":
serialMonitor.PrintLine("1");
currentPressedCode = currentPressedCode + "1";
break;
case "2":
serialMonitor.PrintLine("2");
currentPressedCode = currentPressedCode + "2";
break;
case "3":
serialMonitor.PrintLine("3");
currentPressedCode = currentPressedCode + "3";
break;
default:
break;
} if (buttonsPressed == 3)
{
if (currentPressedCode == vaultCode)
{
//vault open
serialMonitor.PrintLine("vault");
pcbGreen.BackColor = Color.ForestGreen;
}
}
else
{
// wrong code
serialMonitor.PrintLine("wrong");
MessageBox.Show("Wrong password"); // wrong password messagebox
pcbRed.BackColor = Color.DarkRed;
}
There are many ways you could accomplish this, but trying to keep your logic intact as much as possible:
switch ((sender as Button).Text)
{
case "1":
serialMonitor.PrintLine("1");
currentPressedCode = currentPressedCode + "1";
break;
case "2":
serialMonitor.PrintLine("2");
currentPressedCode = currentPressedCode + "2";
break;
case "3":
serialMonitor.PrintLine("3");
currentPressedCode = currentPressedCode + "3";
break;
default:
break;
}
if (buttonsPressed == 3)
{
if (currentPressedCode == vaultCode)
{
//vault open
serialMonitor.PrintLine("vault");
pcbGreen.BackColor = Color.ForestGreen;
}
else
{
// wrong code
serialMonitor.PrintLine("wrong");
MessageBox.Show("Wrong password"); // wrong password messagebox
pcbRed.BackColor = Color.DarkRed;
}
buttonsPressed = 0;
currentPressedCode = "";
}
The only changes I made were:
Moving your "Wrong Password" else block after the "Right Password"
if block
Resetting buttonsPressed and the currentPressedCode
after someone has typed in 3 numbers
Related
I have a method that appends or inserts characters to the text of either a combo box or textbox depending on what was last focused on. I am using buttons to pass in the character as a parameter, Using a keyboard or sendkeys is not an option. When my method appends or inserts characters into a textbox the result is as expected however when the same method is applied to a combobox the text of the combobox is highlighted. Obviously this is not the functionality im looking for and I believe this is stopping the autocomplete mode from working correctly.
Why is the combobox behaving differently than the textbox?
The code:
private void createText(string lowerCaseChar, string upperCaseChar)
{
Control FocusedTextComboBox;
switch (lastTextComboBoxFocused)
{
case 54:
FocusedTextComboBox = SearchTextBox;
break;
case 4:
FocusedTextComboBox = VendorComboBox;
break;
case 6:
FocusedTextComboBox = SectionComboBox;
break;
case 5:
FocusedTextComboBox = DeptComboBox;
break;
default:
FocusedTextComboBox = SearchTextBox;
break;
}
if (FocusedTextComboBox is TextBox)
{
TextBox FocusedTextBox = (TextBox)FocusedTextComboBox;
int SelectionStartNumber = FocusedTextBox.SelectionStart;
switch (shift)
{
case true:
FocusedTextBox.Text = FocusedTextBox.Text.Insert(FocusedTextBox.SelectionStart, upperCaseChar);
break;
case false:
FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedTextBox.SelectionStart, lowerCaseChar);
break;
}
FocusedTextBox.SelectionStart = SelectionStartNumber + 1;
FocusedTextBox.Focus();
}
else
{
ComboBox FocusedComboBox = (ComboBox)FocusedTextComboBox;
if (FocusedComboBox.SelectionStart == 0 && FocusedComboBox.Text != "")
{
switch (shift)
{
case true:
FocusedComboBox.Text += upperCaseChar;
break;
case false:
FocusedComboBox.Text += lowerCaseChar;
break;
}
}
else
{
int SelectionStartNumber = FocusedComboBox.SelectionStart;
switch (shift)
{
case true:
FocusedComboBox.Text = FocusedComboBox.Text.Insert(FocusedComboBox.SelectionStart, upperCaseChar);
break;
case false:
FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedComboBox.SelectionStart, lowerCaseChar);
break;
}
FocusedComboBox.SelectionStart = SelectionStartNumber + 1;
}
FocusedComboBox.Focus();
}
}
I think setting focus back to the combobox is highlighting the text, move FousedCombox.Focus() to before assigning text to combobox.
I have a label on a form that displays a float (_DataFloat) with a variable (_Digits) that sets the number of digits to show to the right of the decimal point. Assuming that _Digits can be any value from 0 through 6, is there a better way of formatting the text other than using a switch statement as below?
switch (_Digits) {
case 0:
label1.Text = _DataFloat.ToString("0");
break;
case 1:
label1.Text = _DataFloat.ToString("0.0");
break;
case 2:
label1.Text = _DataFloat.ToString("0.00");
break;
case 3:
label1.Text = _DataFloat.ToString("0.000");
break;
case 4:
label1.Text = _DataFloat.ToString("0.0000");
break;
case 5:
label1.Text = _DataFloat.ToString("0.00000");
break;
case 6:
label1.Text = _DataFloat.ToString("0.000000");
break;
default:
label1.Text = _DataFloat.ToString("0.00");
break;
}
How about:
var format = String.Format("0.{0}", new string('0', _Digits));
label1.Text = _DataFloat.ToString(format);
I am trying to create an application in C# that converts numbers in a text box to roman numerals in a label control and need to use a case statement. However one of my variable Roman gets the error message: Use of unassigned local variable 'Roman'.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Roman_Numeral_Converter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
int Number=int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number>=1 && Number <=10)
{
switch (Roman)
{
case "Number==1":
lblRoman.Text = "I";
break;
case "Number==2":
lblRoman.Text = "II";
break;
case "Number==3":
lblRoman.Text = "III";
break;
case "Number==4":
lblRoman.Text = "IV";
break;
case "Number==5":
lblRoman.Text = "V";
break;
case "Number==6":
lblRoman.Text = "VI";
break;
case "Number==7":
lblRoman.Text = "VII";
break;
case "Number==8":
lblRoman.Text = "VIII";
break;
case "Number==9":
lblRoman.Text = "IX";
break;
case "Number==10":
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtNum.Text = "";
lblRoman.Text = "";
}
}
}
Your structure is a little off.
private void btnCalc_Click(object sender, EventArgs e)
{
var Number = int.Parse(txtNum.Text); // To hold Number
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
default:
MessageBox.Show("Error: Invalid Input");
break;
}
}
You're using the lblRoman to hold your result, thus your Roman variable is unnecessary. Additionally, since you're interrogating every possible valid number in your switch, you can just use the default to replace your if/else structure.
I'm assuming you're doing this as an academic exercise. That being said, I would be remiss not to point to you Mosè Bottacini's solution to this problem.
This is because Roman variable is really unassigned. You should assign it before you enter the statement
try this,
when your number value like 1 so roman number is I.
private void btnCalc_Click(object sender, EventArgs e)
{
int Number = int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number >= 1 && Number <= 10)
{
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}
Instead of switch, You can do other way using Linq which is even better.
int Number=int.Parse(txtNum.Text);
var romanList = new List<string> {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
if (Number >= 1 && Number <= 10)
lblRoman.Text = romanList.Select((r, i) => new { Roman = r, Index = i+1}).FirstOrDefault(x=> x.Index == Number).Roman;
you can replace your switch statement this way. and of course you need to assign a variable before using it.
public string GetNum(string val)
{
string res = ""; // Assign it an empty string.
var numToRom = new Dictionary<string, string>
{
{"1","I"},
{"2","II"}
//so on
};
numToRom.TryGetValue(val, out res);
return res;
}
I need to create while running a flow chart for string input which I get from textbox1.
the form size is 700*450, and it is allowed scroll.between each letter(char from the input string) has to be( in the output) an arrow(which is displayed on button)
*the whole chart has to be ehxibited on buttons *
for example, for this input string: 'ABZAZAZA'
for each letter there is asuitable color that the background of the button should be colored in.
the program should be "print":
A --> B --> Z --> A --> Z --> A --> Z -->
A -->
the size of arrow button: 34*23
the size of letter button: 34*29
the problem with my code, that the flowchart isn't shown
Heres the code:
public void DrawingSystem(string st)
{
shura_acid = 12;
tur_acid = 185;
for (int i = 1; i <= st.Length; i++)
{
if ((i % 7) == 0)
{
OpenNewLine();
}
CreateAcid(st[i - 1], i);
shura_acid = shura_acid + 24 + 68;
}
}
public void OpenNewLine()
{
tur_acid = tur_acid + 29 + 12;//34 because the size of button,12 because space between lines
shura_acid = 12;
}
public void CreateAcid(char letter, int i)
{
//create acid
Button acid = new Button();
acid.Location = new System.Drawing.Point(shura_acid, tur_acid);
acid.Name = "acid" + i;
acid.Size = new System.Drawing.Size(34, 29);
acid.TabIndex = 100 + i;
acid.Text = Convert.ToString(letter);
switch (letter)
{
case 'A': acid.BackColor = System.Drawing.Color.Fuchsia; break;
case 'C': acid.BackColor = System.Drawing.Color.Pink; break;
case 'D': acid.BackColor = System.Drawing.Color.Gray; break;
case 'F': acid.BackColor = System.Drawing.Color.Azure; break;
case 'G': acid.BackColor = System.Drawing.Color.Red; break;
case 'H': acid.BackColor = System.Drawing.Color.Aqua; break;
case 'I': acid.BackColor = System.Drawing.Color.Lime; break;
case 'K': acid.BackColor = System.Drawing.Color.Yellow; break;
case 'L': acid.BackColor = System.Drawing.Color.Olive; break;
case 'M': acid.BackColor = System.Drawing.Color.Coral; break;
case 'N': acid.BackColor = System.Drawing.Color.SaddleBrown; break;
case 'P': acid.BackColor = System.Drawing.Color.Teal; break;
case 'Q': acid.BackColor = System.Drawing.Color.Blue; break;
case 'R': acid.BackColor = System.Drawing.Color.Orange; break;
case 'S': acid.BackColor = System.Drawing.Color.Green; break;
case 'T': acid.BackColor = System.Drawing.Color.SteelBlue; break;
case 'V': acid.BackColor = System.Drawing.Color.DarkViolet; break;
case 'W': acid.BackColor = System.Drawing.Color.Crimson; break;
case 'X': acid.BackColor = System.Drawing.Color.MediumAquamarine; break;
default: acid.BackColor = System.Drawing.Color.Gold; break;
}
//create arrow
Button arrow = new System.Windows.Forms.Button();
arrow.Location = new System.Drawing.Point(shura_acid + 34 + 12, tur_acid);
arrow.Name = "acid" + i;
arrow.Size = new System.Drawing.Size(34, 23);
arrow.TabIndex = 100 + i;
arrow.Text = "-->";
arrow.UseVisualStyleBackColor = false;
}
I'll take a stab at it. No where in your code do I see you actually adding the acid or arrow buttons to a container.
You need something like this:
this.Controls.Add(acid);
and
this.Controls.Add(arrow);
Change this.Controls to the container you want them to appear in.
I know how to draw text in XNA but I am implementing a level designer that will require the user to enter text into a UI. I have had a little scoot around on Google but couldn't find how to implement an editable text field. Is there nothing built into the framework for this?
you can use some sort of gui library like
http://nuclexframework.codeplex.com
or use winforms
http://create.msdn.com/en-US/education/catalog/sample/winforms_series_1
Have you considered implementing a level editor for your game by embedding XNA in WinForms?
If just need a simple way to enter a small amount of text create a class kbHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
namespace CodeName
{
public class KbHandler
{
private Keys[] lastPressedKeys;
public string tekst = "";
public KbHandler()
{
lastPressedKeys = new Keys[0];
}
public void Update()
{
KeyboardState kbState = Keyboard.GetState();
Keys[] pressedKeys = kbState.GetPressedKeys();
//check if any of the previous update's keys are no longer pressed
foreach (Keys key in lastPressedKeys)
{
if (!pressedKeys.Contains(key))
OnKeyUp(key);
}
//check if the currently pressed keys were already pressed
foreach (Keys key in pressedKeys)
{
if (!lastPressedKeys.Contains(key))
OnKeyDown(key);
}
//save the currently pressed keys so we can compare on the next update
lastPressedKeys = pressedKeys;
}
//Create your own
private void OnKeyDown(Keys key)
{
switch (key)
{
case Keys.D0:
tekst += "0";
break;
case Keys.D1:
tekst += "1";
break;
case Keys.D2:
tekst += "2";
break;
case Keys.D3:
tekst += "3";
break;
case Keys.D4:
tekst += "4";
break;
case Keys.D5:
tekst += "5";
break;
case Keys.D6:
tekst += "6";
break;
case Keys.D7:
tekst += "7";
break;
case Keys.D8:
tekst += "8";
break;
case Keys.D9:
tekst += "9";
break;
case Keys.NumPad0:
tekst += "0";
break;
case Keys.NumPad1:
tekst += "1";
break;
case Keys.NumPad2:
tekst += "2";
break;
case Keys.NumPad3:
tekst += "3";
break;
case Keys.NumPad4:
tekst += "4";
break;
case Keys.NumPad5:
tekst += "5";
break;
case Keys.NumPad6:
tekst += "6";
break;
case Keys.NumPad7:
tekst += "7";
break;
case Keys.NumPad8:
tekst += "8";
break;
case Keys.NumPad9:
tekst += "9";
break;
case Keys.OemPeriod:
tekst += ".";
break;
case Keys.Back:
if (tekst.Length > 0)
{
tekst = tekst.Remove(tekst.Length - 1, 1);
}
break;
}
}
private void OnKeyUp(Keys key)
{
//do stuff
}
}
}
And in the update loop
kb.Update();
string text = kb.tekst;
This is a really easy and dirty way to do it
You will have to create your own area that the user can click into. When they do you need to capture the key presses.
To go along with some of the other suggestions on this thread, you could also use WPF and just render the XNA to a custom user control in xaml. There's a great blog post by Nick Gravelyn on how to do this here:
http://blogs.msdn.com/b/nicgrave/archive/2010/07/25/rendering-with-xna-framework-4-0-inside-of-a-wpf-application.aspx
The great part about this is that you can use techniques like MVVM then to write your editor :-)