c# Highlight Numbers smaller code snip - c#

I am creating a bingo machine, I have code so once pressed it will check what numbers have been called and highlight those numbers that have been called from the bingo machine.
I am wondering is there a better way of limiting the code size? rather than list the all individually? This will help increase the speed and reduce the program size.
Thank you.
private void btnCheckNumbs_Click(object sender, EventArgs e)
{
for (int i = 1; i <= Globals.NextBalls; i++)
{
if (Globals.balls[i] == 1) txtBoxs1.BackColor = Color.Aqua;
if (Globals.balls[i] == 2) txtBoxs2.BackColor = Color.Aqua;
if (Globals.balls[i] == 3) txtBoxs3.BackColor = Color.Aqua;
if (Globals.balls[i] == 4) txtBoxs4.BackColor = Color.Aqua;
if (Globals.balls[i] == 5) txtBoxs5.BackColor = Color.Aqua;
if (Globals.balls[i] == 6) txtBoxs6.BackColor = Color.Aqua;
if (Globals.balls[i] == 7) txtBoxs7.BackColor = Color.Aqua;
if (Globals.balls[i] == 8) txtBoxs8.BackColor = Color.Aqua;
if (Globals.balls[i] == 9) txtBoxs9.BackColor = Color.Aqua;
if (Globals.balls[i] == 10) txtBoxs10.BackColor = Color.Aqua;
if (Globals.balls[i] == 11) txtBoxs11.BackColor = Color.Aqua;
if (Globals.balls[i] == 12) txtBoxs12.BackColor = Color.Aqua;
if (Globals.balls[i] == 13) txtBoxs13.BackColor = Color.Aqua;
if (Globals.balls[i] == 14) txtBoxs14.BackColor = Color.Aqua;
if (Globals.balls[i] == 15) txtBoxs15.BackColor = Color.Aqua;
if (Globals.balls[i] == 16) txtBoxs16.BackColor = Color.Aqua;
if (Globals.balls[i] == 17) txtBoxs17.BackColor = Color.Aqua;
if (Globals.balls[i] == 18) txtBoxs18.BackColor = Color.Aqua;
if (Globals.balls[i] == 19) txtBoxs19.BackColor = Color.Aqua;
if (Globals.balls[i] == 20) txtBoxs20.BackColor = Color.Aqua;
The statement goes up to 90....

try this
for (int i = 1; i <= Globals.NextBalls; i++)
{
for (int k = 1; k <= 90; k++)
{
if (Globals.balls[i] == k)
{
TextBox txtName = (TextBox)this.Controls.Find("txtBoxs"+k.ToString(), true)[0];
txtName.BackColor = Color.Aqua;
}
}
}

Related

Stack overflow with a loop c#

I am currently having problems with this loop. It becomes an infinite loop and i get a stack overflow error. This is for a interest rate trade swap application. i is the length of the trade and l is the increasing index.
private void button1_Click(object sender, EventArgs e)
{
int outp = 0;
int i = int.Parse(tradeLength.Text);
string month = "January";
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if (l == 1)
{
month = "January";
}
if (l == 2)
{
month = "February";
}
if (l == 3)
{
month = "March";
}
if (l == 4)
{
month = "Aprll";
}
if (l == 5)
{
month = "May";
}
if (l == 6)
{
month = "June";
}
if (l == 7)
{
month = "July";
}
if (l == 8)
{
month = "August";
}
if (l == 9)
{
month = "September";
}
if (l == 10)
{
month = "October";
}
if (l == 11)
{
month = "November";
}
if (l == 12)
{
month = "December";
}
else
{
month = "Null";
l = 1;
}
The cause is the final else:
if (l == 12) {
month = "December";
}
else { // <- if l != 12 (e.g. l == 1) restart the loop
month = "Null";
l = 1;
}
you want else if:
if (l == 1)
{
month = "January";
}
else if (l == 2)
{
...
}
...
else if (l == 12)
{
...
}
else {
month = "Null";
l = 1;
}
Edit: Another problem (see FKEinternet's comment) is a user input: if i is greater than 12 l never reaches it. You have to either validate the user input:
int i = int.Parse(tradeLength.Text);
if (i > 12)
i = 12; // or ask for other value
or use modular arithmetics:
for (int index = 1; index <= i; index++) {
int l = index % 12 + 1;
if (l == 1)
{
month = "January";
}
else if (l == 2)
...
else if (l == 12)
...
else
{
month = "Null";
l = 1;
}
}
It is not a very good idea to set the loop variable inside the loop. Like #stuartd pointed out, in your else line you set the loop variable to 1 and causing the loop to start all over again. Remove the l=1 line in your else block.
I presume you want to go to next year when i > 12. The way your code is made, when this happens, you get to loop forever, because "l" never reaches a number bigger than 12, it becomes 1 when it hits 13 and starts over.
To fix this, instead of
if (l == 1)
you want to use
if ((l % 12) == 1)
so your entire loop would be like this:
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if ((l % 12) == 1)
{
month = "January";
}
if ((l % 12) == 2)
{
month = "February";
}
if ((l % 12) == 3)
{
month = "March";
}
if ((l % 12) == 4)
{
month = "Aprll";
}
if ((l % 12) == 5)
{
month = "May";
}
if ((l % 12) == 6)
{
month = "June";
}
if ((l % 12) == 7)
{
month = "July";
}
if ((l % 12) == 8)
{
month = "August";
}
if ((l % 12) == 9)
{
month = "September";
}
if ((l % 12) == 10)
{
month = "October";
}
if ((l % 12) == 11)
{
month = "November";
}
if ((l % 12) == 0)
{
month = "December";
}
{
PS = this is really not the right way to do this, I'm just using your own code and making the least amount of mods for it to work as intented. Good luck!

Moving buttons in winform board

I'm trying to write a code for a simple game of 2048. I've managed to implement functions for moving the buttons in columns and summing them if they're equal, but somehow, only in X ratio. Why aren't they doing the same thing in Y?
Here's the code:
bool checkIfMoved;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
checkIfMoved = false;
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == false))
{
moveButton(i, j, e);
}
else if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
else if (e.KeyCode == Keys.Right)
{
checkIfMoved = false;
for (int i = 3; i >= 0; i--)
{
for (int j = 3; j >= 0; j--)
{
if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == false))
{
moveButton(i, j, e);
}
else if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
else if (e.KeyCode == Keys.Up)
{
checkIfMoved = false;
for (int i = 0; i <= 3; i++)
{
for (int j = 3; j >= 0; j--)
{
if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == false))
{
moveButton(i, j, e);
}
else if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == true))
{
sumButtons(i, j, e);
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
}
else if (e.KeyCode == Keys.Down)
{
checkIfMoved = false;
for (int i = 3; i >= 0; i--)
{
for (int j = 0; j <= 3; j++)
{
if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == false))
{
moveButton(i, j, e);
}
else if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
}
}
Now the moving and summing functions:
private void moveButton(int i, int j, KeyEventArgs e)
{
SwitchKey Switch = new SwitchKey(i, j, e);
try
{
while (board[Switch.line, Switch.column].Text == "0")
{
board[Switch.line, Switch.column].Text = board[i, j].Text;
board[Switch.line, Switch.column].Visible = true;
board[i, j].Visible = false;
board[i, j].Text = "0";
sumButtons(Switch.line, Switch.column, e);
checkIfMoved = true;
switch(e.KeyCode)
{
case(Keys.Left):
j--;
break;
case(Keys.Right):
j++;
break;
case(Keys.Up):
i--;
break;
case(Keys.Down):
i++;
break;
}
Switch = new SwitchKey(i, j, e);
}
}
catch { }
}
private void sumButtons(int i, int j, KeyEventArgs e)
{
SwitchKey Switch = new SwitchKey(i, j, e);
while ((board[i, j].Text == board[Switch.line,Switch.column].Text))
{
int x;
int y;
Int32.TryParse(board[i, j].Text, out x);
Int32.TryParse(board[Switch.line, Switch.column].Text, out y);
int z = x + y;
string a = z.ToString();
board[Switch.line, Switch.column].Text = a;
board[Switch.line, Switch.column].Visible = true;
board[i, j].Visible = false;
board[i, j].Text = "0";
}
}
and the SwitchKey class:
class SwitchKey
{
public int line;
public int column;
public SwitchKey(int i, int j, System.Windows.Forms.KeyEventArgs e)
{
#region Keycode switch
switch (e.KeyCode)
{
case (System.Windows.Forms.Keys.Left):
{
column = j-1;
line = i;
break;
}
case (System.Windows.Forms.Keys.Right):
{
column = j + 1;
line = i;
break;
}
case (System.Windows.Forms.Keys.Up):
{
column = j;
line = i-1;
break;
}
case (System.Windows.Forms.Keys.Down):
{
column = j;
line = i+1;
break;
}
}
#endregion
}
}
The game works perfectly when i use left and right keys but doesn't do anything when using up and down. What am I doing wrong?
It seems that you need a close brace here
....
else if (e.KeyCode == Keys.Right)
{
checkIfMoved = false;
.... a lot of code
if (checkIfMoved == true)
{
GenerateField();
}
}// <-- here
else if (e.KeyCode == Keys.Up)
{
...
You have a misplaced }
Your
else if (e.KeyCode == Keys.Up)
{
Is nested inside your
else if (e.KeyCode == Keys.Right)
{
Try this:
bool checkIfMoved;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
checkIfMoved = false;
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == false))
{
moveButton(i, j, e);
}
else if ((j != 0) && (board[i, j].Visible == true) && (board[i, j - 1].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
else if (e.KeyCode == Keys.Right)
{
checkIfMoved = false;
for (int i = 3; i >= 0; i--)
{
for (int j = 3; j >= 0; j--)
{
if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == false))
{
moveButton(i, j, e);
}
else if ((j != 3) && (board[i, j].Visible == true) && (board[i, j + 1].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
else if (e.KeyCode == Keys.Up)
{
checkIfMoved = false;
for (int i = 0; i <= 3; i++)
{
for (int j = 3; j >= 0; j--)
{
if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == false))
{
moveButton(i, j, e);
}
else if ((i != 0) && (board[i, j].Visible == true) && (board[i - 1, j].Visible == true))
{
sumButtons(i, j, e);
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
}
else if (e.KeyCode == Keys.Down)
{
checkIfMoved = false;
for (int i = 3; i >= 0; i--)
{
for (int j = 0; j <= 3; j++)
{
if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == false))
{
moveButton(i, j, e);
}
else if ((i != 3) && (board[i, j].Visible == true) && (board[i + 1, j].Visible == true))
{
sumButtons(i, j, e);
}
}
}
if (checkIfMoved == true)
{
GenerateField();
}
}
}

c# remove items from combobox based on MaxLength

So I have a combobox and I have it so that based on other criteria the MaxLength is edited. The problem is that the MaxLength property only applies to typed inputs and not items from the combobox. So is there a way to remove items from the combobox based on their length and add them back if the length increases again? here is my code:
private void titleText_TextChanged(object sender, EventArgs e)
{
int colourNum;
int textType = 1;
if (titleTextType.Text == "Material")
textType = 1;
else if (titleTextType.Text == "Material Flipped")
textType = 2;
else if (titleTextType.Text == "Normal")
textType = 3;
else if (titleTextType.Text == "3D")
textType = 4;
else textType = 0;
if (titleTextColour.Text == "Red")
colourNum = 49;
else if (titleTextColour.Text == "Green")
colourNum = 50;
else if (titleTextColour.Text == "Yellow")
colourNum = 51;
else if (titleTextColour.Text == "Blue")
colourNum = 52;
else if (titleTextColour.Text == "Cyan")
colourNum = 53;
else if (titleTextColour.Text == "Pink")
colourNum = 54;
else if (titleTextColour.Text == "White")
colourNum = 55;
else if (titleTextColour.Text == "Black")
colourNum = 48;
else if (titleTextColour.Text == "Yale Blue")
colourNum = 59;
else if (titleTextColour.Text == "Light Yellow")
colourNum = 58;
else colourNum = 0;
byte[] colourArray = new byte[2]
{
(byte) 94,
(byte) colourNum
};
byte[] prefixArray1 = new byte[5]
{
(byte) 94,
(byte) textType,
(byte) 0x3D,//125decmax
(byte) 0x3D,//125decmax
(byte) titleText.Text.Length
};
if (textType == 3 && colourNum == 0)
{
titleText.MaxLength = 23;
}
else if (textType == 3 && colourNum != 0)
{
titleText.MaxLength = 21;
}
else if (textType == 1 && colourNum == 0 || textType == 2 && colourNum == 0)
{
titleText.MaxLength = 18;
}
else if (textType == 1 && colourNum != 0 || textType == 2 && colourNum != 0)
{
titleText.MaxLength = 16;
}
else if (textType == 4)
{
titleText.MaxLength = 3;
}
}
Thanks in advance c:
Here, will be easier to demonstrate code here.
My comboBox is named comboBox1.
This test code adds Test1 - Test20 and then removes them based on their length:
for (int x = comboBox1.Items.Count-1; x >= 0; x--)
if (comboBox1.Items[x].ToString().Length > comboBox1.MaxLength)
comboBox1.Items.RemoveAt(x);
Your code should look like this:
for (int x = titleText.Items.Count-1; x >= 0; x--)
if (titleText.Items[x].ToString().Length > titleText.MaxLength)
titleText.Items.RemoveAt(x);

C# fill a cell in tableLayoutPanel on a loop

I have a problem in this code, that show me own cell filled.
Here is the code:
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
for (int j = 0; j < 4; j++)
{
if (j % 3 == 0)
{
if (e.Column == j && e.Row == 1)
{
e.Graphics.FillRectangle(Brushes.White, e.CellBounds);
}
}
else
{
if (e.Column == j && e.Row == 1)
{
e.Graphics.FillRectangle(Brushes.Brown, e.CellBounds);
}
}
}
}
And here the result:
http://prntscr.com/20e3pb

combobox1 to 2 selectedindex

I having trouble on combobox selectedindex. basically i wanted to disable the button1 when my textbox result is 1. but the problem is when the button1 get disable and i chose another option it wont enable back. so is there another way to do it? below is just showing some part of the coding.
double[,] arr;
public Form1()
{
arr = new double[3, 3];
for (int i = 0; i < 2; i++)
{
arr[0, 0] = 1;
arr[0, 1] = 0.79;
arr[0, 2] = 1.17;
arr[1, 0] = 1.26;
arr[1, 1] = 1;
arr[1, 2] = 1.08;
arr[2, 0] = 0.85;
arr[2, 1] = 0.93;
arr[2, 2] = 1;
}
void CreateArray()
{
if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1)
return;
else if (comboBox1.SelectedIndex == 1 || comboBox2.SelectedIndex == 0)
{
button1.Enabled = false;
}
else if (comboBox1.SelectedIndex == 0 || comboBox2.SelectedIndex == 1)
{
button1.Enabled = false;
}
else if (comboBox1.SelectedIndex == 1 || comboBox2.SelectedIndex == 2)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
If the second combobox has only 3 items then you will never be able to reach the final else clause where you reset the button to the enabled state.
This happens because you use the || logical OR operator and with only three items you will always take one of the else if condition before the final else

Categories