Moving buttons in winform board - c#

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

Related

AI does not play well in vertical tic tac toe c#

I'm doing a tic tac toe game with an AI. The AI plays well, except when it has to win with a vertical. All the other ways (horizontal and diagonal) does work.
I've done a lot of debugging and step by step and haven't found the solution. I thought you guys could help me finding the problem and help me solving it!
int findTwoPions(Boolean?[,] jeu)
{
// La méthode qui selon moi est à modifier car c'est celle ci qui décide la position que doit prendre l'IA quand elle peu gagner
int somme = 0;
int compteurX = 0;
int compteur0 = 0;
//Diagonale descendante
for (int i = 0; i < 3; i++)
{
if ((jeu[0, 0] == false || jeu[1, 1] == false || jeu[2, 2] == false) && (jeu[0, 0] == true || jeu[1, 1] == true || jeu[2, 2] == true))
{
somme += 0;
}
else
{
if (jeu[i, i] == false)
{
compteur0++;
compteurX = 0;
if (compteur0 == 2)
{
somme += 1500;
}
}
else if (jeu[i, i] == true)
{
compteur0 = 0;
compteurX++;
if (compteurX == 2)
{
somme -= 1600;
}
}
}
}
compteurX = 0;
compteur0 = 0;
//Diagonale montante
for (int i = 0; i < 3; i++)
{
if ((jeu[0, 2] == false || jeu[1, 1] == false || jeu[2, 0] == false) && (jeu[0, 2] == true || jeu[1, 1] == true || jeu[2, 0] == true))
{
}
else
{
if (jeu[i, 2 - i] == false)
{
compteur0++;
compteurX = 0;
if (compteur0 == 2)
{
somme += 1500;
}
}
else if (jeu[i, 2 - i] == true)
{
compteurX++;
compteur0 = 0;
if (compteurX == 2)
{
somme -= 1600;
}
}
}
}
//En ligne
for (int i = 0; i < 3; i++)
{
compteurX = 0;
compteur0 = 0;
if ((jeu[0, i] == false || jeu[1, i] == false || jeu[2, i] == false) && (jeu[0, i] == true || jeu[1, i] == true || jeu[2, i] == true))
{
somme += 0;
}
else
{
//Verticale
for (int j = 0; j < 3; j++)
{
if (jeu[j, i] == false)
{
compteur0++;
compteurX = 0;
if (compteur0 == 2)
{
somme += 1500;
}
}
else if (jeu[j, i] == true)
{
compteurX++;
compteur0 = 0;
if (compteurX == 2)
{
somme -= 1600;
}
}
}
}
compteurX = 0;
compteur0 = 0;
if ((jeu[i, 0] == false || jeu[i, 1] == false || jeu[i, 2] == false) && (jeu[i, 0] == true || jeu[i, 1] == true || jeu[i, 2] == true))
{
return somme += 0;
} // Voir les valeurs i j pcque c'est faux
else
{
//Horizontale
for (int j = 0; j < 3; j++)
{
if (jeu[i, j] == false)
{
compteur0++;
compteurX = 0;
if (compteur0 == 2)
{
somme += 1500;
}
}
else if (jeu[i, j] == true)
{
compteurX++;
compteur0 = 0;
if (compteurX == 2)
{
somme -= 1600;
}
}
}
}
}
return somme;
}
}
}
I think the problem is when I add a value to 'somme' or the way I run trough my tic tac toe.
If you need further code please tell me, thank you !
UPDATE:
MY AIRoutine.cs
public Boolean?[][] IAPlay(Boolean?[][] jeu, int profondeur)
{
int max = -10000;
int tmp, tmp2 = 0, tmpSomme = -10000; // -10000
int tmpBefore = 0;
int maxi = 0, maxj = 0;
int somme = 0;
int biggestSomme = 0;
setTab(jeu); // convertit le tableau[][] en tableau[,]
for (int i = 0; i < 3; i++) // parcours toutes les cases vides du tableau
{
for (int j = 0; j < 3; j++)
{
//Si une case est vide, on joue le coup de l'IA sur cette case et on simule le jeu complet
if (tab[i, j] == null)
{
tab[i, j] = false; // On simule le coup de l'IA
somme = findTwoPions(tab);
tmp = Max(tab, profondeur - 1);
if (tmpBefore < tmp && biggestSomme > somme)
{
tmpSomme = somme + tmpBefore;
}
else if (tmpBefore > tmp && biggestSomme < somme)
{
tmpSomme = somme + tmpBefore;
}
else
{
tmpSomme = tmp + somme;
}
if (somme > biggestSomme)
{
biggestSomme = somme;
tmpBefore = tmp;
}
//|| ((tmp == max) && (r.Next(1, 100) % 2 == 0))
if (tmpSomme >= max)
{
max = tmpSomme;
tmp2 = somme;
maxi = i;
maxj = j;
}
tab[i, j] = null;
}
}
}
tab[maxi, maxj] = false;
return getTab(jeu);
}
Let's put it in readable and maintainabe way: let's extract a method WinningLines where we enumerate all winning combinations (I've assummed that jue is 2d array - bool?[3, 3]):
using System.Linq;
...
private static IEnumerable<bool?[]> WinningLines(bool?[,] field) {
// Verticals
for (int column = 0; column < 3; ++column)
yield return new bool?[] {field[0, column], field[1, column], field[2, column]};
// Horizontals
for (int row = 0; row < 3; ++row)
yield return new bool?[] {field[row, 0], field[row, 1], field[row, 2]};
// Diagonals
yield return new bool?[] {field[0, 0], field[1, 1], field[2, 2]};
yield return new bool?[] {field[0, 2], field[1, 1], field[2, 0]};
}
Now let's query (with a help of Linq):
// Do we have any winning combinations for the 1st Player (all 3 true in WinningLines):
bool hasFirstWon = WinningLines(jeu).Any(line => line.All(cell => cell == true));
// Do we have any winning combinations for the 2nd Player (all 3 false in WinningLines):
bool hasSecondWon = WinningLines(jeu).Any(line => line.All(cell => cell == false));
Or if you operate with somme:
int somme =
WinningLines(jeu).Any(line => line.All(cell => cell == true)) ?
1500 // 1st wins
: WinningLines(jeu).Any(line => line.All(cell => cell == false)) ?
-1600 // 2nd wins
: 0; // no-one wins
Edit: Now let's implement a (simple) version of the int findTwoPions(Boolean?[,] jeu) method. First let's have
private static bool FirstIsOnMove(bool?[,] field) {
int count = 0;
foreach (var item in field)
if (item == true)
count += 1;
else if (item == true)
count -= 1;
return count == 0;
}
and the method itself will be
// This method rates the position in a [-1600..1500] range
// [1st player has lost..1st player has won]
int findTwoPions(Boolean?[,] jeu) {
// Edge cases: win or lose
if (WinningLines(jeu).Any(line => line.All(cell => cell == true)))
return 1500; // 1st has won
else if (WinningLines(jeu).Any(line => line.All(cell => cell == false)))
return -1600; // 1st has lost
//TODO: add more heuristics (depending who is on move)
// Example: if palayer is on move and can win by its next move?
// Say, we have positions like
// X.. XXO
// OX. Or X.O
// .O. ...
if (FirstIsOnMove(jeu)) {
if (WinningLines(jeu)
.Any(line => line.Sum(item => item == true ? 1 : item == false ? -1 : 0) == 2))
return 1200; // 1st is going to win (unless it makes a blind)
}
else {
if (WinningLines(jeu)
.Any(line => line.Sum(item => item == true ? 1 : item == false ? -1 : 0) == -2))
return -1200; // 2st is going to win (unless it makes a blind)
}
// Neutral position - neither 1st not 2nd have any advantages
return 0;
}

Function is not updating Button.Text c#

I have a grid of buttons that should reveal some info when they are clicked (within the button itself) For a knowledge assignment I'm trying to program minesweeper.
The grid method:
public void createGrid()
{
for (int i = 0; i < 8; i++)
{
grid[i] = new Cell[8];
for (int j = 0; j < 8; j++)
{
chance = rand1.Next(0, 6);
if (chance == 0 && bombAmount < 10)
{
grid[i][j] = new Cell(true);
bombAmount++;
}
else
{
grid[i][j] = new Cell();
}
grid[i][j].Name = "grid" + i.ToString() + j.ToString();
grid[i][j].Location = new System.Drawing.Point(i * 49, j * 49);
grid[i][j].Size = new System.Drawing.Size(50, 50);
grid[i][j].TabIndex = 0;
grid[i][j].Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j <8; j++)
{
this.Controls.Add(grid[i][j]);
}
}
}
The Reveal method:
public void RevealCell(int n, int m)
{
if (grid[n][m].HasBomb == true)
{
grid[n][m].Text = "B";
}
else if (grid[n][m].NeighbourBombCount > 0)
{
grid[n][m].Text = grid[n][m].NeighbourBombCount.ToString();
}
else
{
grid[n][m].Text = "NB";
}
grid[n][m].IsRevealed = true;
}
and then there's the checking if neighbours have bombs or not method:
public void CheckNeighbours(int cswitch)
{
switch (cswitch)
{
// Left upper corner (i = 0, j = 0)
case 1:
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j < 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Upper edge (i = 0, 0 < j < 7)
case 2:
for (int i = 0; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Right upper corner (i = 0, j = 7)
case 3:
for (int i = 0; i <= 1; i++)
{
for (int j = -1; j <= 0; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Right edge (0 < i < 7, j = 7)
case 4:
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 0; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Right down corner (i = 7, j = 7)
case 5:
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 0; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Bottom edge (i = 7, 0 < j < 7)
case 6:
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Left down corner (i = 7, j = 0)
case 7:
for (int i = -1; i <= 0; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
// Left edge (i = 0, 0 < j < 7)
case 8:
for (int i = 0; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
default:
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0) { }
else
{
if (grid[i][j].HasBomb == true)
{
grid[0][0].NeighbourBombCount++;
}
}
}
}
break;
}
}
As my buttons are laid out in grid fashion the Top left button is button 1 and right bottom button is button 64 (hence the switch statements).
Edit: Didn't add the Click method to the grid[i][j].Click += handler...
Update:
private void Cell_click(object sender, EventArgs e)
{
e.
if(i == 0 && j == 0)
{
CheckNeighbours(1);
}
if (i == 0 && j > 0 && j < 7)
{
CheckNeighbours(2);
}
if (i == 0 && j == 7)
{
CheckNeighbours(3);
}
if (i > 0 && i < 7 && j == 7)
{
CheckNeighbours(4);
}
if (i == 7 && j == 7)
{
CheckNeighbours(5);
}
if (i == 7 && j > 0 && j < 7)
{
CheckNeighbours(6);
}
if (i == 7 && j == 0)
{
CheckNeighbours(7);
}
if(i > 0 && i < 7 && j == 0)
{
CheckNeighbours(8);
}
RevealCell(i, j);
}
Now I just need to know how to grab the i and j indeces from the sender object and I'm golden!
you need
cell[i][j].Click += new System.EventHandler(this.CellClick);
and create a single CellClick event handler method

Random movement of char in console c#

I made a maze game in a console application and by using keys I can control character to leave the maze. Now I want to figure out how to automate that process So that my character can go by random directions and try to find the way out. Here is my class.
The problem is in the randomMovement Method.
class Player: Maze
{
public int x = 1;
public int y = 1;
public string playerName;
public static void printMv() {
while (true)
{
for (int i = 0; i < 15; i++)
{
Console.Clear();
mazeDev();
Console.Write("0");
Thread.Sleep(1000);
}
}
}
public static void movementByUser()
{
int x = 1, y = 1;
while (true)
{
Console.Clear();
mazeDev();
Console.CursorLeft = x;
Console.CursorTop = y;
Console.Write("0");
ConsoleKeyInfo mv = Console.ReadKey();
if (mv.Key == ConsoleKey.Escape) break;
if (mv.Key == ConsoleKey.A && mazeBluePrint[y, x - 1] == 0) {
x--;
}
if (mv.Key == ConsoleKey.D && mazeBluePrint[y, x + 1] == 0)
{
x++;
}
if (mv.Key == ConsoleKey.W && mazeBluePrint[y - 1, x] == 0)
{
y--;
}
if (mv.Key == ConsoleKey.S && mazeBluePrint[y + 1, x] == 0)
{
y++;
}
}
}
public static void randomMovement()
{
Random randMove = new Random();
int x = 1;
int y = 1;
int irandom = randMove.Next(4);
Console.CursorLeft = x;
Console.CursorTop = y;
if (irandom == mazeBluePrint[y, x - 1])
{
x--;
printMv();
}
else if (irandom == mazeBluePrint[y, x + 1])
{
x++;
printMv();
}
else if (irandom == mazeBluePrint[y - 1, x])
{
y--;
printMv();
}
else
{
y++;
printMv();
}
}
}
}
Your problem probably is the direction evaluation. Try this:
public static void randomMovement()
{
Random randMove = new Random();
int x = 1;
int y = 1;
int irandom = randMove.Next(4);
Console.CursorLeft = x;
Console.CursorTop = y;
if (irandom == 0 && mazeBluePrint[y, x - 1] == 0)
{
x--;
printMv();
}
else if (irandom == 1 && mazeBluePrint[y, x + 1] == 0)
{
x++;
printMv();
}
else if (irandom == 2 && mazeBluePrint[y - 1, x] == 0)
{
y--;
printMv();
}
else if (mazeBluePrint [y + 1, x] == 0)
{
y++;
printMv();
}
}
However, this code still has a lot of weaknesses (e.g. you could walk out of the boundaries of the mazeBluePrint array, which would result in an exception).
EDIT
If you want to move in the other direction if the first didn't work, you could try something like this:
public static void randomMovement()
{
Random randMove = new Random();
int x = 1;
int y = 1;
Console.CursorLeft = x;
Console.CursorTop = y;
while (true)
{
int irandom = randMove.Next(4);
if (irandom == 0 && mazeBluePrint[y, x - 1] == 0)
{
x--;
printMv();
break;
}
else if (irandom == 1 && mazeBluePrint[y, x + 1] == 0)
{
x++;
printMv();
break;
}
else if (irandom == 2 && mazeBluePrint[y - 1, x] == 0)
{
y--;
printMv();
break;
}
else if (mazeBluePrint[y + 1, x] == 0)
{
y++;
printMv();
break;
}
}
}
However, this only moves one time, beginning from 1,1.

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

Categories