Keypress Upper/Lowercase exceptions - c#

i have a little tiny problem, what i am trying to do is limit my textBox to the following characters: [a=>f, x, A=>F, 0=>9], and what i need exactly is add an exception that will make any lower case input in the mentioned textBox become uppercase, except for "x", this is what i tried, but it limited all inputs from the textBox:
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar < 'A' || e.KeyChar > 'F') && (e.KeyChar < 'a' || e.KeyChar > 'f') && (e.KeyChar != ' '))
{
e.Handled = true;
textBox1.CharacterCasing = CharacterCasing.Upper;
}
else if ((e.KeyChar != 'x'))
{
e.Handled = true;
textBox1.CharacterCasing = CharacterCasing.Lower;
}
Thank you.

managed to bypass it:
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar < 'A' || e.KeyChar > 'F') && (e.KeyChar < 'a' || e.KeyChar > 'f') && (e.KeyChar != ' ') && (e.KeyChar != 'x'))
{
e.Handled = true;
}
//textBox1.CharacterCasing = CharacterCasing.Upper;
if (e.KeyChar == 'x') e.KeyChar = Char.ToLower(e.KeyChar);
else e.KeyChar = Char.ToUpper(e.KeyChar);
thank you.

Related

How to make my OnClientClick execute within pre-existing if statements?

I have an asp.net web app and one of my pages is for payment. I have a submit button that will only work if 1) you have selected a currency in which you wish to pay (done with radio buttons) and 2) all of the payment fields have been filled out (card & billing info). This all works great, the last thing missing is if a currency has been selected and all payment info is filled out, I wanted a confirmation box with 'OK' and 'Cancel' buttons. I added the following code into my page_load method: submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";. The issue is that the message box appears whenever I click the submit button, ignoring my if statements already in place.
Is there a way to enable/disable the OnClientClick where applicable within my if statements, so that the box won't appear if a currency hasn't been selected and/or not all of the payment information has been filled out? Cheers!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Coursework
{
public partial class Payment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";
if (!Page.IsPostBack)
{
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
{
String cstext = "alert('Details are correct, welcome');";
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}
}
}
protected void submitPayBtn_Click(object sender, EventArgs e)
{
if (!poundRadBtn.Checked && !usdolRadBtn.Checked && !ozdolRadBtn.Checked && !eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please select a currency in which you wish to pay');</script>");
}
if (poundRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working gbp');</script>");
Response.Redirect("Home.aspx");
}
if (usdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working usd');</script>");
Response.Redirect("Home.aspx");
}
if (ozdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working aud');</script>");
Response.Redirect("Home.aspx");
}
if (eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working eur');</script>");
Response.Redirect("Home.aspx");
}
}
protected void backBtn_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
}
You should move the return confirm('Are you sure you wish to change the name?'); to a separate javascript function. Then you can change it based on other form values.
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="return myFunction()" />
<script type="text/javascript">
function myFunction() {
if (document.getElementById("<%= TextBox1.ClientID %>").value != "") {
return confirm('Are you sure you wish to change the name?');
} else {
return true;
}
}
</script>
And I suggest you take a look at the build-in aspnet Validation Controls

I am trying to delete the records in excel from column 1 to 35

// Here I am trying to delete all the rows starting from column 1
to 35.But while deleting the records, some records are left behind and
its not deleting all the rows. I am unable to understand where the
problem actually is.Is it the problem with the task manager but already I am killing the exe files. I am looping through all the rows from second row till the last row and hence if the records are not empty,I am deleting the records with the help of get_range one by one.
public static void GetIncidentExcel(string Incident_Path, List<excelObj> ListTickets)
{
lastRow = 0;
MyApp = new Excel.Application();
// MyApp.Visible = false;
MyBook = MyApp.Workbooks.Open(Incident_Path);
MySheet = (Excel.Worksheet)MyBook.Sheets["BOXI_summary"]; // Explict cast is not required here
lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
for (int i = 2; i <= lastRow; i++)
{
Array MyValues = (Array)MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString()).Cells.Value;
if (Convert.ToString(MyValues.GetValue(1, 1)) == "" && Convert.ToString(MyValues.GetValue(1, 2)) == "" && Convert.ToString(MyValues.GetValue(1, 3)) == "" &&
Convert.ToString(MyValues.GetValue(1, 4)) == "" && Convert.ToString(MyValues.GetValue(1, 5)) == "" && Convert.ToString(MyValues.GetValue(1, 6)) == "" &&
Convert.ToString(MyValues.GetValue(1, 7)) == "" && Convert.ToString(MyValues.GetValue(1, 8)) == "" && Convert.ToString(MyValues.GetValue(1, 9)) == "" &&
Convert.ToString(MyValues.GetValue(1, 10)) == "" && Convert.ToString(MyValues.GetValue(1, 11)) == "" && Convert.ToString(MyValues.GetValue(1, 12)) == "" &&
Convert.ToString(MyValues.GetValue(1, 13)) == "" && Convert.ToString(MyValues.GetValue(1, 14)) == "" && Convert.ToString(MyValues.GetValue(1, 15)) == "" &&
Convert.ToString(MyValues.GetValue(1, 16)) == "" && Convert.ToString(MyValues.GetValue(1, 17)) == "" && Convert.ToString(MyValues.GetValue(1, 18)) == "" &&
Convert.ToString(MyValues.GetValue(1, 19)) == "" && Convert.ToString(MyValues.GetValue(1, 20)) == "" && Convert.ToString(MyValues.GetValue(1, 21)) == "" &&
Convert.ToString(MyValues.GetValue(1, 22)) == "" && Convert.ToString(MyValues.GetValue(1, 23)) == "")
break;
else
{
Excel.Range cells = MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString());
cells.Delete();
}
}
Try replacing your loop with something like this:
for (int i = lastRow; i >= 2; i--)
{
if (MyApp.WorksheetFunction.CountA(MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString())) == 0)
{
MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString()).Delete(Excel.XlDirection.xlUp);
}
}
You can use the WorksheetFunction object to check if all the cells are empty, and when deleting rows you should always loop backwards

C# use of unassigned variable?

So basically I have a class for a TicTacToe game and a derived class just to practice using inheritance. The class has several methods and all should work perfectly fine but in the main function, when I finally make three objects of the derived class I get three errors "use of unassigned local variable 'boardOne'" "use of unassigned local variable 'boardTwo'" and "use of unassigned local variable 'boardThree'". I do not understand why this is, they are objects, not variables.
public class TicTacToe
{
protected char[] boardCells = new char[9];
public int boardSpacesUsed;
public TicTacToe() //constructor
{
boardCells[0] = '1';
boardCells[1] = '2';
boardCells[2] = '3';
boardCells[3] = '4';
boardCells[4] = '5';
boardCells[5] = '6';
boardCells[6] = '7';
boardCells[7] = '8';
boardCells[8] = '9';
boardCells[9] = '\0';
int boardSpacesUsed = 0;
}
public void playerOneMove()
{
bool space = false;
char cell = '\0';
while (space == false)
{
Console.WriteLine("Please enter cell number you wish to mark: ");
cell = Convert.ToChar(Console.ReadLine());
switch (cell)
{
case '1':
if (boardCells[0] == 'X' || boardCells[0] == 'O')
{
Console.WriteLine("Illegal Move");
}
else
{
boardCells[0] = 'X';
space = true;
}
break;
case '2':
if (boardCells[1] == 'X' || boardCells[1] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[1] = 'X';
space = true;
}
break;
case '3':
if (boardCells[2] == 'X' || boardCells[2] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[2] = 'X';
space = true;
}
break;
case '4':
if (boardCells[3] == 'X' || boardCells[3] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[3] = 'X';
space = true;
}
break;
case '5':
if (boardCells[4] == 'X' || boardCells[4] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[4] = 'X';
space = true;
}
break;
case '6':
if (boardCells[5] == 'X' || boardCells[5] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[5] = 'X';
space = true;
}
break;
case '7':
if (boardCells[6] == 'X' || boardCells[6] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[6] = 'X';
space = true;
}
break;
case '8':
if (boardCells[7] == 'X' || boardCells[7] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[7] = 'X';
space = true;
}
break;
case '9':
if (boardCells[8] == 'X' || boardCells[8] == 'O')
Console.WriteLine("Illegal Move");
else
{
boardCells[8] = 'X';
space = true;
}
break;
default:
Console.WriteLine("Cell Does NOT Exist!");
break;
}// end of switch statement
}//end of while loop
boardSpacesUsed++;
}// end of playerOneMove();
public void CPUMove() //method marks cell for CPU
{
int iCell = 0;
bool space = false;
while (space == false)
{
Random rand = new Random();
iCell = rand.Next(1, 9);
switch (iCell) //switch statement to mark the cell
{
case 1:
if (boardCells[0] == 'X' || boardCells[0] == 'O')
{
space = false;
}
else
{
boardCells[0] = 'O';
space = true;
}
break;
case 2:
if (boardCells[1] == 'X' || boardCells[1] == 'O')
space = false;
else
{
boardCells[1] = 'O';
space = true;
}
break;
case 3:
if (boardCells[2] == 'X' || boardCells[2] == 'O')
space = false;
else
{
boardCells[2] = 'O';
space = true;
}
break;
case 4:
if (boardCells[3] == 'X' || boardCells[3] == 'O')
space = false;
else
{
boardCells[3] = 'O';
space = true;
}
break;
case 5:
if (boardCells[4] == 'X' || boardCells[4] == 'O')
space = false;
else
{
boardCells[4] = 'O';
space = true;
}
break;
case 6:
if (boardCells[5] == 'X' || boardCells[5] == 'O')
space = false;
else
{
boardCells[5] = 'O';
space = true;
}
break;
case 7:
if (boardCells[6] == 'X' || boardCells[6] == 'O')
space = false;
else
{
boardCells[6] = 'O';
space = true;
}
break;
case 8:
if (boardCells[7] == 'X' || boardCells[7] == 'O')
space = false;
else
{
boardCells[7] = 'O';
space = true;
}
break;
case 9:
if (boardCells[8] == 'X' || boardCells[8] == 'O')
space = false;
else
{
boardCells[8] = 'O';
space = true;
}
break;
}
}
boardSpacesUsed++;
}
public void getBoardCells()
{
Console.WriteLine(" " + " " + " | " + " " + " | " + " ");
Console.WriteLine(" " + boardCells[0] + " | " + boardCells[1] + " | " + boardCells[2]);
Console.WriteLine("__" + "_" + "__|__" + "_" + "__|__" + "_");
Console.WriteLine(" " + " " + " | " + " " + " | " + " ");
Console.WriteLine(" " + boardCells[3] + " | " + boardCells[4] + " | " + boardCells[5]);
Console.WriteLine("__" + "_" + "__|__" + "_" + "__|__" + "_");
Console.WriteLine(" " + " " + " | " + " " + " | " + " ");
Console.WriteLine(" " + boardCells[6] + " | " + boardCells[7] + " | " + boardCells[8]);
Console.WriteLine(" " + " " + " | " + " " + " | " + " ");
}
public bool playerOneWinCheck(ref int score)
{
bool check = false;
if (boardCells[0] == 'X' && boardCells[1] == 'X' && boardCells[2] == 'X')
{
check = true;
score++;
}
if (boardCells[3] == 'X' && boardCells[4] == 'X' && boardCells[5] == 'X')
{
check = true;
score++;
}
if (boardCells[6] == 'X' && boardCells[7] == 'X' && boardCells[8] == 'X')
{
check = true;
score++;
}
if (boardCells[0] == 'X' && boardCells[3] == 'X' && boardCells[6] == 'X')
{
check = true;
score++;
}
if (boardCells[1] == 'X' && boardCells[4] == 'X' && boardCells[7] == 'X')
{
check = true;
score++;
}
if (boardCells[2] == 'X' && boardCells[5] == 'X' && boardCells[8] == 'X')
{
check = true;
score++;
}
if (boardCells[0] == 'X' && boardCells[4] == 'X' && boardCells[8] == 'X')
{
check = true;
score++;
}
if (boardCells[6] == 'X' && boardCells[4] == 'X' && boardCells[2] == 'X')
{
check = true;
score++;
}
if (check == true)
return true;
else
return false;
}
public bool CPUWinCheck(ref int score) //Method to check to see if CPU won INCRAMENTS SCORE UP ONE IF ANYTHING HOLDS TRUE
{
bool check = false;
if (boardCells[0] == 'O' && boardCells[1] == 'O' && boardCells[2] == 'O')
{
check = true;
score++;
}
if (boardCells[3] == 'O' && boardCells[4] == 'O' && boardCells[5] == 'O')
{
check = true;
score++;
}
if (boardCells[6] == 'O' && boardCells[7] == 'O' && boardCells[8] == 'O')
{
check = true;
score++;
}
if (boardCells[0] == 'O' && boardCells[3] == 'O' && boardCells[6] == 'O')
{
check = true;
score++;
}
if (boardCells[1] == 'O' && boardCells[4] == 'O' && boardCells[7] == 'O')
{
check = true;
score++;
}
if (boardCells[2] == 'O' && boardCells[5] == 'O' && boardCells[8] == 'O')
{
check = true;
score++;
}
if (boardCells[0] == 'O' && boardCells[4] == 'O' && boardCells[8] == 'O')
{
check = true;
score++;
}
if (boardCells[6] == 'O' && boardCells[4] == 'O' && boardCells[2] == 'O')
{
check = true;
score++;
}
if (check == true)
return true;
else
return false;
}
~TicTacToe()
{
for (int c = 0; c <= 10; c++)
{
boardCells[c] = '\0';
}
boardSpacesUsed = 0;
}
}
public class ThreeD : TicTacToe
{
public void threeDWinCheck(ThreeD boardOne, ThreeD boardTwo, ThreeD boardThree, ref int score) //new function to check to see ThreeD wins
{
if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[0] == 'X' && boardThree.boardCells[0] == 'X')
{
score++;
Console.WriteLine("did it make it");
}
if (boardOne.boardCells[1] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[1] == 'X')
score++;
if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[2] == 'X' && boardThree.boardCells[2] == 'X')
score++;
if (boardOne.boardCells[3] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[3] == 'X')
score++;
if (boardOne.boardCells[4] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[4] == 'X')
score++;
if (boardOne.boardCells[5] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[5] == 'X')
score++;
if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[6] == 'X' && boardThree.boardCells[6] == 'X')
score++;
if (boardOne.boardCells[7] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[7] == 'X')
score++;
if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[8] == 'X' && boardThree.boardCells[8] == 'X')
score++;
if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[2] == 'X')
score++;
if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[1] == 'X' && boardThree.boardCells[0] == 'X')
score++;
if (boardOne.boardCells[3] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[5] == 'X')
score++;
if (boardOne.boardCells[5] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[3] == 'X')
score++;
if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[8] == 'X')
score++;
if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[7] == 'X' && boardThree.boardCells[6] == 'X')
score++;
if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[6] == 'X')
score++;
if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[3] == 'X' && boardThree.boardCells[0] == 'X')
score++;
if (boardOne.boardCells[1] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[7] == 'X')
score++;
if (boardOne.boardCells[7] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[1] == 'X')
score++;
if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[8] == 'X')
score++;
if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[5] == 'X' && boardThree.boardCells[2] == 'X')
score++;
if (boardOne.boardCells[0] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[8] == 'X')
score++;
if (boardOne.boardCells[8] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[0] == 'X')
score++;
if (boardOne.boardCells[2] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[6] == 'X')
score++;
if (boardOne.boardCells[6] == 'X' && boardTwo.boardCells[4] == 'X' && boardThree.boardCells[2] == 'X')
score++;
}
public void CPUThreeDWinCheck(ThreeD boardOne, ThreeD boardTwo, ThreeD boardThree, ref int score) //new function to check CPU ThreeD wins
{
if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[0] == 'O' && boardThree.boardCells[0] == 'O')
score++;
if (boardOne.boardCells[1] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[1] == 'O')
score++;
if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[2] == 'O' && boardThree.boardCells[2] == 'O')
score++;
if (boardOne.boardCells[3] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[3] == 'O')
score++;
if (boardOne.boardCells[4] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[4] == 'O')
score++;
if (boardOne.boardCells[5] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[5] == 'O')
score++;
if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[6] == 'O' && boardThree.boardCells[6] == 'O')
score++;
if (boardOne.boardCells[7] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[7] == 'O')
score++;
if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[8] == 'O' && boardThree.boardCells[8] == 'O')
score++;
if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[2] == 'O')
score++;
if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[1] == 'O' && boardThree.boardCells[0] == 'O')
score++;
if (boardOne.boardCells[3] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[5] == 'O')
score++;
if (boardOne.boardCells[5] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[3] == 'O')
score++;
if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[8] == 'O')
score++;
if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[7] == 'O' && boardThree.boardCells[6] == 'O')
score++;
if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[6] == 'O')
score++;
if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[3] == 'O' && boardThree.boardCells[0] == 'O')
score++;
if (boardOne.boardCells[1] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[7] == 'O')
score++;
if (boardOne.boardCells[7] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[1] == 'O')
score++;
if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[8] == 'O')
score++;
if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[5] == 'O' && boardThree.boardCells[2] == 'O')
score++;
if (boardOne.boardCells[0] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[8] == 'O')
score++;
if (boardOne.boardCells[8] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[0] == 'O')
score++;
if (boardOne.boardCells[2] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[6] == 'O')
score++;
if (boardOne.boardCells[6] == 'O' && boardTwo.boardCells[4] == 'O' && boardThree.boardCells[2] == 'O')
score++;
}
~ThreeD()
{
for (int c = 0; c < 10; c++)
{
boardCells[c] = '\0';
}
}
}
static void Main(string[] args)
{
ThreeD boardOne; //1st of three objects for first board
ThreeD boardTwo; //2nd
ThreeD boardThree; //3rd
int boardSelection = 0; //picks witch object to mark in
int counter = 0; //counter for while loop
int playerOneScore = 0; //score for user
int CPUScore = 0; //score for cpu
int CPUBoardSelection = 0;
Random rand = new Random();
int randomNum = rand.Next(1, 2);
if (randomNum == 1) // if randomNum = 1, user goes first
{
Console.WriteLine("You first");
while (true)
{
boardOne.getBoardCells();
boardTwo.getBoardCells();
boardThree.getBoardCells();
Console.WriteLine("Choose which board you wish to mark in. (1 - 3)"); //promts you to pick board
bool board = false;
Yes, the compiler is right: you're trying to use unassigned variables:
static void Main(string[] args)
{
// It's just a declaration; no value assigned to boardOne; boardOne contains trash
ThreeD boardOne;
// It's just a declaration; no value assigned to boardTwo; boardTwo contains trash
ThreeD boardTwo;
// It's just a declaration; no value assigned to boardThree; boardThree contains trash
ThreeD boardThree;
....
if (randomNum == 1)
{
Console.WriteLine("You first");
while (true)
{
boardOne.getBoardCells(); // <- And here you're trying to access the trash
It should be something like that:
static void Main(string[] args)
{
// boardOne is declared and assigned
ThreeD boardOne = new ThreeD();
// boardTwo is declared and assigned
ThreeD boardTwo = new ThreeD();
// boardThree is declared and assigned
ThreeD boardThree = new ThreeD();
....
if (randomNum == 1)
{
Console.WriteLine("You first");
while (true)
{
boardOne.getBoardCells(); // <- Quite OK
Firstly, boardOne, boardTwo and boardThree are variables, in this case are local variables scoped to the Main() method. Like any other variable, they need a valid data type, in your case the type is the ThreeD class. But this alone don't makes them objects, only defines their data type.
The variables only become objects when you use this class to create a new instance (a new single object in memory). So they must be initialized:
ThreeD boardOne = new ThreeD();
ThreeD boardTwo = new ThreeD();
ThreeD boardThree = new ThreeD();
This way, when the method getBoardCells() is called, each variable points to the object in memory they represent, which contains that method. Without the assignment, the variables are equal null by default. And of course, as null haven't a method getBoardCells() the compiler error you got makes all sense.
You need to instantiate the class:
ThreeD boardOne = new ThreeD();
ThreeD boardTwo = new ThreeD();
ThreeD boardThree = new ThreeD();
If you don't do this you cannot access the non-static class members.
In order to use a local variable, you have to initialize it:
var boardOne = new ThreeD();
var boardTwo = new ThreeD();
...
You have to initialize your board objects becuase C# compiler does not allow the use of uninitialized variables. Check this link : Compiler Error CS0165
static void Main(string[] args)
{
ThreeD boardOne = new ThreeD(); //1st of three objects for first board
ThreeD boardTwo = new ThreeD(); //2nd
ThreeD boardThree = new ThreeD();
//..............
// ..............
}
your missing creating object instance
ThreeD boardOne = new ThreeD();
ThreeD boardTwo = new ThreeD();
ThreeD boardThree = new ThreeD();

C# Validation of Period

I want to ask if anyone knows how to limit the period allowed for a textbox. I'm using C# Visual Studio 2010.
My problem is I need to find validation codes that will ensure that only a single period is allowed for the textbox Middle Initial of the user. And if the user type another period, then the period will not be shown in the textbox. No error messages is needed. Example is the validation code that accept letters only. Here is my code for this example:
private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
I have the following codes currently for my txtboxMI:
private void txtMI_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
Do you have to make it server-side?
You can use regular expression validator.
[a-zA-Z_-.] should give you what you want.
Try this:
var txt = (TextBox)sender;
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == 8 || e.KeyChar == 32) {
} else if (txt.Text.Contains('.') && e.KeyChar == '.') {
e.Handled = true;
} else if (e.KeyChar == '\t') {
e.Handled = true;
SendKeys.Send("{Tab}");
}

How to make this function not prematurely split?

I've written this function...
internal static IEnumerable<KeyValuePair<char?, string>> SplitUnescaped(this string input, char[] separators)
{
int index = 0;
var state = new Stack<char>();
for (int i = 0; i < input.Length; ++i)
{
char c = input[i];
char s = state.Count > 0 ? state.Peek() : default(char);
if (state.Count > 0 && (s == '\\' || (s == '[' && c == ']') || ((s == '"' || s == '\'') && c == s)))
state.Pop();
else if (c == '\\' || c == '[' || c == '"' || c == '\'')
state.Push(c);
if (state.Count == 0 && separators.Contains(c))
{
yield return new KeyValuePair<char?, string>(c, input.Substring(index, i - index));
index = i + 1;
}
}
yield return new KeyValuePair<char?, string>(null, input.Substring(index));
}
Which splits a string on the given separators, as long as they aren't escaped, in quotes, or in brackets. Seems to work pretty well, but there's one problem with it.
There characters I want to split on include a space:
{ '>', '+', '~', ' ' };
So, given the string
a > b
I want it to split on > and ignore the spaces, but given
a b
I do want it to split on the space.
How can I fix the function?
You could continue to split based on and > and then remove the strings which are empty.
I think this does it...
internal static IEnumerable<KeyValuePair<char?, string>> SplitUnescaped(this string input, char[] separators)
{
int startIndex = 0;
var state = new Stack<char>();
input = input.Trim(separators);
for (int i = 0; i < input.Length; ++i)
{
char c = input[i];
char s = state.Count > 0 ? state.Peek() : default(char);
if (state.Count > 0 && (s == '\\' || (s == '[' && c == ']') || ((s == '"' || s == '\'') && c == s)))
state.Pop();
else if (c == '\\' || c == '[' || c == '"' || c == '\'')
state.Push(c);
else if (state.Count == 0 && separators.Contains(c))
{
int endIndex = i;
while (input[i] == ' ' && separators.Contains(input[i + 1])) { ++i; }
yield return new KeyValuePair<char?, string>(input[i], input.Substring(startIndex, endIndex - startIndex));
while (input[++i] == ' ') { }
startIndex = i;
}
}
yield return new KeyValuePair<char?, string>(null, input.Substring(startIndex));
}
I was trying to push the space onto the stack too before, and then doing some checks against that...but I think this is easier.

Categories

Resources