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
Related
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;
}
Hello I have just recently finished a code of a grid. The instructions allow you to go right,left,up or down and enter a specific value to move by. The problem right now however is, which value is entered is the only location that becomes true. I'm trying to get the entire amount to become true. For example fi we move right 3, it should be 3 spots = true instead of just the last. Can anyone help me with that?
// Make a bool 2d array and save already drilled values into the array
bool[,] drill = new bool[401, 200];
drill[200, 1] = true;
drill[200, 2] = true;
drill[200, 3] = true;
drill[201, 3] = true;
drill[202, 3] = true;
drill[203, 3] = true;
drill[203, 4] = true;
drill[203, 5] = true;
drill[204, 5] = true;
drill[205, 5] = true;
drill[205, 4] = true;
drill[205, 3] = true;
drill[206, 3] = true;
drill[207, 3] = true;
drill[207, 4] = true;
drill[207, 5] = true;
drill[207, 6] = true;
drill[207, 7] = true;
drill[206, 7] = true;
drill[205, 7] = true;
drill[204, 7] = true;
drill[203, 7] = true;
drill[202, 7] = true;
drill[201, 7] = true;
drill[200, 7] = true;
drill[199, 7] = true;
drill[199, 6] = true;
drill[199, 5] = true;
// Set some values
bool okay = true;
int column = -1;
int row = -5;
int check_column = 199;
int check_row = 5;
// Run a while loop
do
{
// Ask the user to input a direction
Console.WriteLine("Which direction would you like to go?");
string direction = Console.ReadLine();
// Ask the user to input a movement
Console.WriteLine("What value would you like to move by?");
string distance = Console.ReadLine();
int new_distance = Convert.ToInt32(distance);
// Use if statements
if (direction == "l" || direction == "L")
{
column = column - new_distance;
check_column = check_column - new_distance;
}
else if (direction == "u" || direction == "U")
{
row = row + new_distance;
check_row = check_row - new_distance;
}
else if (direction == "r" || direction == "R")
{
column = column + new_distance;
check_column = check_column + new_distance;
}
else if (direction == "d" || direction == "D")
{
row = row - new_distance;
check_row = check_row + new_distance;
}
else if (direction == "q" || direction == "Q" || new_distance == 0)
{
Console.WriteLine("Program will now end.");
okay = false;
break;
}
while (new_distance > 0)
{
if (drill[check_column, check_row] == true && check_row >= 0 && check_row <=200 && check_column >=0 && check_column <=400 && drill[check_column, check_row] != true)
{
Console.WriteLine("{0},{1} safe", column, row);
break;
}
else
{
Console.WriteLine("{0},{1} danger", column, row);
Console.WriteLine("Program will now end.");
okay = false;
break;
}
}
} while (okay);
if (okay == false)
{
Console.WriteLine("Thanks for using the program!");
}
Console.ReadLine();
To answer you question, the reason you are not seeing each move as safe is because you are not checking each position, you were simply setting your check position to the position you wanted to move and not each incremental position. I have added a class with a bit more logic in this answer because each move in your game requires slightly different logic to work.
public class DrillGame
{
private const int Columns = 401;
private const int Rows = 200;
public void Play()
{
// Make a bool 2d array and save already drilled values into the array
bool[,] drill = ProvideDrill();
// Set some values
bool okay = true;
_currentGameRow = -1;
_currentGameColumn = -5;
_currentArrayRow = 5;
_currentArrayColumn = 199;
// Run a while loop
do
{
// Ask the user to input a direction
Console.WriteLine("Which direction would you like to go?");
string direction = Console.ReadLine();
// Ask the user to input a movement
Console.WriteLine("What value would you like to move by?");
string distanceInput = Console.ReadLine();
int distanceToMove = Convert.ToInt32(distanceInput);
// Use if statements
if (direction == "l" || direction == "L")
{
okay = TryMoveLeft(distanceToMove);
}
else if (direction == "u" || direction == "U")
{
okay = TryMoveUp(distanceToMove);
}
else if (direction == "r" || direction == "R")
{
okay = TryMoveRight( distanceToMove);
}
else if (direction == "d" || direction == "D")
{
okay = TryMoveDown(distanceToMove);
}
else if (direction == "q" || direction == "Q" || distanceToMove == 0)
{
Console.WriteLine("Program will now end.");
okay = false;
break;
}
} while (okay);
if (okay == false)
{
Console.WriteLine("Thanks for using the program!");
}
Console.ReadLine();
}
private bool TryMoveLeft(int distanceToMove)
{
while(distanceToMove > 0)
{
_currentArrayColumn = _currentArrayColumn - 1;
_currentGameColumn = _currentGameColumn - 1;
if (!TryMoveColumn(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveRight(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayColumn = _currentArrayColumn + 1;
_currentGameColumn = _currentGameColumn + 1;
if (!TryMoveColumn(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveUp(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayRow = _currentArrayRow - 1;
_currentGameRow = _currentGameRow - 1;
if (!TryMoveRow(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveDown(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayRow = _currentArrayRow + 1;
_currentGameRow = _currentGameRow + 1;
if (!TryMoveRow(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveColumn(int distanceToMove)
{
var drill = ProvideDrill();
if (_currentArrayColumn >= 0 && _currentArrayColumn < Columns && drill[_currentArrayColumn, _currentArrayRow])
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe");
return true;
}
else
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger");
Console.WriteLine("Program will now end.");
return false;
}
}
private bool TryMoveRow(int distanceToMove)
{
var drill = ProvideDrill();
if (_currentArrayRow >= 0 && _currentArrayRow < Rows && drill[_currentArrayColumn, _currentArrayRow])
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe");
return true;
}
else
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger");
Console.WriteLine("Program will now end.");
return false;
}
}
private bool[,] ProvideDrill()
{
bool[,] drill = new bool[Columns, Rows];
drill[200, 1] = true;
drill[200, 2] = true;
drill[200, 3] = true;
drill[201, 3] = true;
drill[202, 3] = true;
drill[203, 3] = true;
drill[203, 4] = true;
drill[203, 5] = true;
drill[204, 5] = true;
drill[205, 5] = true;
drill[205, 4] = true;
drill[205, 3] = true;
drill[206, 3] = true;
drill[207, 3] = true;
drill[207, 4] = true;
drill[207, 5] = true;
drill[207, 6] = true;
drill[207, 7] = true;
drill[206, 7] = true;
drill[205, 7] = true;
drill[204, 7] = true;
drill[203, 7] = true;
drill[202, 7] = true;
drill[201, 7] = true;
drill[200, 7] = true;
drill[199, 7] = true;
drill[199, 6] = true;
drill[199, 5] = true;
return drill;
}
private int _currentArrayRow;
private int _currentArrayColumn;
private int _currentGameRow;
private int _currentGameColumn;
}
Sample output
I'm trying to make a basic noughts and crosses game in c#.
So far I've made the grid using buttons which's text changes when it is pressed depending which player's turn it is.
The part which I am stuck on is checking if any of the players has won, I've written this however it doesn't seem to do anything.
private void Form1_Load(object sender, EventArgs e)
{
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
{
MessageBox.Show("Player", player_turntxt.Text + " wins");
Application.Restart();
}
}
private void button1_Click(object sender, EventArgs e)
{
int Player_Turn = Convert.ToInt32(player_turntxt.Text);
if (Player_Turn == 1)
{
button1.Text = "X";
player_turntxt.Text = "2";
button1.Enabled = false;
return;
}
else
{
button1.Text = "O";
player_turntxt.Text = "1";
button1.Enabled = false;
return;
}
(The Application.Restart(); is just a temporary method of checking if it works.)
This is just one of 8 statements which I'll have to make as conditions for victory, any idea where I'm going wrong?
Update:
public void CheckForWinner(int x)
{
if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
{
x = 1;
}
else if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X")
{
x = 1;
}
else if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X")
{
x = 1;
}
else if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X")
{
x = 1;
}
else if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
{
x = 1;
}
else if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X")
{
x = 1;
}
else if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X")
{
x = 1;
}
else if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X")
{
x = 1;
}
if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O")
{
x = 2;
}
else if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
{
x = 2;
}
else if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O")
{
x = 2;
}
else if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O")
{
x = 2;
}
else if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
{
x = 2;
}
else if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")
{
x = 2;
}
else if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")
{
x = 2;
}
else if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O")
{
x = 2;
}
}
You should create a method to check if any player has won, and run it at the end of everyone of your "button clicks".
I algo suggest you to remove the "return"'s from your button clicks. They can introduce unexpected behaviors sometimes, like ending the function before you execute the line of code that calls the method to check if someone has won.
Something like this:
private void button1_Click(object sender, EventArgs e)
{
int Player_Turn = Convert.ToInt32(player_turntxt.Text);
if (Player_Turn == 1)
{
button1.Text = "X";
player_turntxt.Text = "2";
button1.Enabled = false;
}
else
{
button1.Text = "O";
player_turntxt.Text = "1";
button1.Enabled = false;
}
CheckIfSomeoneHasWon();
}
Let's discuss the implementation of CheckIfSomeoneHasWon. I figure that there are several ways of doing it. We could create a bidimensional array with the values of the buttons, and iterate it. In fact, I will do it that way. Your way works too, but we would have to write a lot. This is what I came up with:
static readonly string _player1_symbol = "X";
static readonly string _player2_symbol = "O";
static void CheckIfSomeoneHasWon()
{
string[,] userChoices = BuildUserChoices();
string winner = CheckWhoWon(userChoices);
if (winner != null)
{
// Somebody won! Display message and start over
}
}
private static string CheckWhoWon(string[,] values)
{
// Horizontal checks
for (int i = 0; i < 3; i++)
{
if (values[i, 0] == values[i, 1] && values[i, 1] == values[i, 2])
{
return (values[i, 0] == _player1_symbol) ? "player 1" : "player 2";
}
}
// Vertical checks
for (int i = 0; i < 3; i++)
{
if (values[0, i] == values[1, i] && values[1,i] == values[2,i])
{
return (values[i, 0] == _player1_symbol) ? "player 1" : "player 2";
}
}
// Diagonal checks
if (values[0, 0] == values[1, 1] && values[1, 1] == values[2, 2])
{
return (values[0, 0] == _player1_symbol) ? "player 1" : "player 2";
}
if (values[0, 2] == values[1, 1] && values[1, 1] == values[2, 0])
{
return (values[1, 1] == _player1_symbol) ? "player 1" : "player 2";
}
// No one has won yet
return null;
}
private static string[,] BuildUserChoices()
{
var values = new string[3, 3];
values[0, 0] = button1.Text;
values[0, 1] = button2.Text;
values[0, 2] = button3.Text;
// and so on...
// If a button has not been click, they must have a unique text, like a number
return values;
}
You need to check if a player wins after each turn. Right now you are checking it once, after the form loaded.
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;
}
}
}
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);