char SideA = 'A';
char SideB = 'B';
int CPUPlayer = 1;
Test.Text = x.ToString();
if (difficulty == 1)
{
if (CPUPlayer == 1)
{
string targetString = "";
for (int side = 1; side <= 1; side++)
{
targetString = SideA.ToString();
Test.Text = targetString.ToString();
for (int game = 1; game < 25; game++)
{
targetString = game.ToString();
for (int tile = 1; tile < 10; tile++)
{
targetString = tile.ToString();
PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
if (target.BackgroundImage == null)
{
target.BackgroundImage = Properties.Resources.smallo;
Test.Text = targetString.ToString();
}
}
}
}
}
}
So I'm trying to loop through prenamed PictureBoxes (Ex: A11,A12,A13....etc) to have them change background Images by using the loop through string to connect to the pictureboxes but I keep getting Index is out of range from the PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
I'm not sure what to do to fix it. I'm very new to c# and coding in general so would be nice to know what I'm missing!
To help you find the problem change
PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
to
var found = this.Controls.Find(targetString, true);
if(found == null || found.Length < 1 ) {
throw new Exception("Picture not found")
}
After that, set a breakpoint in the throw line and find out why the picture was not found.
So turns out it was just my .ToString(); logic that was just flat out wrong and the string was coming out as a picturebox that didn't exist. This is the code that works:
if (difficulty == 1)
{
if (CPUPlayer == 1)
{
string targetString = "";
for (int side = 1; side <= 1; side++)
{
for (int game = 1; game < 25; game++)
{
for (int tile = 1; tile < 10; tile++)
{
targetString = SideA;
targetString += game.ToString();
targetString += tile.ToString();
Test.Text = targetString.ToString();
PictureBox target = (PictureBox)(this.Controls.Find(targetString, true))[0];
Related
I am trying to create a word search puzzle matrix, this is the code I have,
static void PlaceWords(List<string> words)
{
Random rn = new Random();
foreach (string p in words)
{
String s = p.Trim();
bool placed = false;
while (placed == false)
{
int nRow = rn.Next(0,10);
int nCol = rn.Next(0,10);
int nDirX = 0;
int nDirY = 0;
while (nDirX == 0 && nDirY == 0)
{
nDirX = rn.Next(3) - 1;
nDirY = rn.Next(3) - 1;
}
placed = PlaceWord(s.ToUpper(), nRow, nCol, nDirX, nDirY);
}
}
}
static bool PlaceWord(string s, int nRow, int nCol, int nDirX, int nDirY)
{
bool placed = false;
int LetterNb = s.Length;
int I = nRow;
int J = nCol;
if (MatriceIndice[nRow, nCol] == 0)
{
placed = true;
for (int i = 0; i < s.Length-1; i++)
{
I += nDirX;
J += nDirY;
if (I < 10 && I>0 && J < 10 && J>0)
{
if (MatriceIndice[I, J] == 0)
placed = placed && true;
else
placed = placed && false;
}
else
{
return false;
}
}
}
else
{
return false;
}
if(placed==true)
{
int placeI = nRow;
int placeJ = nCol;
for (int i = 0; i < s.Length - 1; i++)
{
placeI += nDirX;
placeJ += nDirY;
MatriceIndice[placeI,placeJ] = 1;
MatriceChars[placeJ, placeJ] = s[i];
}
}
return placed;
}
However it seems like it is an infinite loop. I am trying to add the code in a 1010 char matrix linked to a 1010 int matrix initially filled with 0 where I change the cases to 1 if the word is added to the matrix. How should I fix the code?
There are several errors. First,
MatriceChars[placeJ, placeJ] = s[i];
should be
MatriceChars[placeI, placeJ] = s[i];
Second,
for (int i = 0; i < s.Length - 1; i++)
(two occurrences) should be
for (int i = 0; i < s.Length; i++)
(You do want all the letters in the words, right?)
Third, when testing indices, you should use I >= 0, not I > 0, as the matrix indices start at 0.
However, the main logic of the code seems to work, but if you try to place too many words, you will indeed enter an infinite loop, since it just keeps trying and failing to place words that can never fit.
I have DataGridView which consist of these data
From these data i would like to make the first column, airport name to group the third column and sum to their corresponding ActLoadQuantityTo which is in the fifth column, the cargo type which landed on the airport. right now my chart is messed up as it look like this
for (int j = 0; j < dgv_3.Rows.Count; j++)
{
double Load_qty = 0;
double new_load_qty = 0;
DiscPort = dgv_3.Rows[j].Cells["ActDiscDischargingPort"].Value.ToString();
DestType = dgv_3.Rows[j].Cells["SchLoadDestination"].Value.ToString();
CargoType = dgv_3.Rows[j].Cells["SchLoadCargoType"].Value.ToString();
string CargoType2 = "Series-" + CargoType;
Load_qty = Convert.ToDouble(dgv_3.Rows[j].Cells["ActLoadQuantityTon"].Value);
if (chart3.Series.IndexOf(DiscPort) != -1)
{
chart3.Series["000 - Port"].Points.AddY(Load_qty);
}
else
{
chart3.Series["000 - Port"].Points.AddXY(DiscPort, Load_qty);
}
}
no grouping of series,etc. i've tried adding more series but it just doesn't make sense toward the DataGridView. and my expectation is that i would like the chart to look like this as I've described before
Any idea what set of code should it be?
I found my desired result by using this code
public void isichartdomestic(System.Data.DataTable initialDataSource)
{
chart3.Titles.Add("Your title");
chart3.Legends.Add("Your Legend");
for (int i = 1; i < initialDataSource.Columns.Count; i++)
{
System.Windows.Forms.DataVisualization.Charting.Series series = new System.Windows.Forms.DataVisualization.Charting.Series();
foreach (DataRow dr in initialDataSource.Rows)
{
double y = (double)dr[i];
series.Points.AddXY(dr["DiscPort"].ToString(), y);
TotalDomestic += y;
}
chart3.Series.Add(series);
string NamaSeries = series.ToString();
NamaSeries = NamaSeries.Replace("Series-","");
chart3.Series[NamaSeries].IsValueShownAsLabel = true;
chart3.Series[NamaSeries].IsVisibleInLegend = true;
if (NamaSeries == "Series1")
{
chart3.Series[NamaSeries].LegendText = "Bag";
}
else if (NamaSeries == "Series2")
{
chart3.Series[NamaSeries].LegendText = "Bulk";
}
else if (NamaSeries == "Series3")
{
chart3.Series[NamaSeries].LegendText = "Clinker";
}
else if (NamaSeries == "Series4")
{
chart3.Series[NamaSeries].LegendText = "Container";
}
}
lb_TotalDomestic.Text = Convert.ToString("Total Domestic: " + TotalDomestic);
TotalDomesticExport += TotalDomestic;
}
I'm trying to figure out how to set up my win condition (when the player lines up four chips as the same color horizontally, vertically, or diagonally). The win condition when met will display a win message, add 1 to a player win and player loss variable, add text to a list box, and clear the board.
I set up the board when the user presses the start button using the following code:
btnStartGame.Enabled = false;
btnStartGame.Visible = false;
btnExitGame.Enabled = true;
btnExitGame.Visible = true;
//This for loop creates the buttons used for the gameplay
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 7) * 50;
int y = 50 + (i / 7) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += (sender1, ex) => this.PlaceChip(sender1, index);
this.Controls.Add(gameButtons[i]);
From there main game play uses the following code for "dropping" chips into the columns:
private void PlaceChip(object sender, int index)
{
var pressedButton = (Button)sender;
if (pressedButton.BackColor == Color.BlanchedAlmond)
{
var newBackColor = black ? Color.Red : Color.Black;
var buttonToChangeIndex = index;
while (buttonToChangeIndex + 7 < gameButtons.Count() &&
gameButtons[buttonToChangeIndex + 7].BackColor == Color.BlanchedAlmond)
{
buttonToChangeIndex += 7;
}
gameButtons[buttonToChangeIndex].BackColor = newBackColor;
black = !black;
}
}
Currently my Win Condition code looks like the following, I just am not sure how I need to set this up correctly (assuming I am making a mistake somewhere) or how I call this and set up the arguments correctly when I call it.
private void WinCondition(int a, int b, int c, int d)
{
if (gameButtons[a].BackColor == gameButtons[b].BackColor && gameButtons[a].BackColor == gameButtons[c].BackColor && gameButtons[a].BackColor == gameButtons[d].BackColor)
{
gamesPlayed += 1;
do
{
if (gameButtons[a].BackColor == Color.Red)
{
MessageBox.Show("Player 1 Wins!");
player1wins += 1;
player2loss += 1;
lstScoreBoard.Items.Add("Player One");
//add to file
ResetBoard();
}
else
{
MessageBox.Show("Player 2 Wins!");
player2wins += 1;
player1loss += 1;
lstScoreBoard.Items.Add("Player Two");
ResetBoard();
}
if(gamesPlayed == 5)
{
MessageBox.Show("Maximum number of games have been played!\nWin board will now be reset!");
gamesPlayed = 0;
player1wins = 0;
player2wins = 0;
player1loss = 0;
player2loss = 0;
}
} while (gamesPlayed > 5);
}
}
If there is a better way to set up this win condition (In regards to the arguments etc) I am open for it! I'm at a loss for how to properly set it up!
If my description of what I need doesn't make sense feel free to ask and I will try to clarify!
Is it possible to set two values in one for loop? I would like to create a string matrix [,], the first element (i) is variable, the second (j) is constant
int rowRun = 1;
string[,] costume = new string [elementsOfRunning, rowRun];
int columnRun = costume.GetLength(0);
for (int i = 0; i < columnRun; i++)
{
int rowOfRunning;
do
{
Console.WriteLine("Row of running (0-42)");
rowOfRunning = int.Parse(Console.ReadLine());
}
while (!(0 <= rowOfRunning && rowOfRunning <= 42));
string rowOfRunning2 = rowOfRunning.ToString();
}
And here I would like to set the i value for example: costume[i,j] = rowOfRunning; But I can't in this way.
for (int j = 0; j < rowRun; j++)
{
string comment = "";
do
{
Console.WriteLine("Write a comment: („verseny”, „terep”, „laza”, „fartlek”, „résztáv”");
comment = Console.ReadLine();
}
while (!comment.Contains(",") && comment != "verseny" && comment != "terep" && comment != "laza" && comment != "fartlek" && comment != "résztáv");
costume[i, j] = comment;
Console.WriteLine(costume[i,j]);
}
I'm not sure what you want to do but you can declare more than one variable in for loop like this:
var array = new Array[10, 10];
for (int i = 0, j = 1; i < 3; j++, i++)
{
Console.WriteLine("i:" + i + " j:" + j);
array[i,j] = Console.ReadLine();
}
This generates a picturebox
PictureBox[][] picturebox;
public void loadPictureBox()
{
string path = #"../../Images/Catelogue/"; //set pathing
string[] list = Directory.GetFiles(path, "*.jpg");
//pictureboxCatelogue = new PictureBox[list.Length];
//pictureboxCosplay = new PictureBox[list.Length];
picturebox = new PictureBox[4][];
for (int i = 0; i < 4; i++)
{
picturebox[i] = new PictureBox[list.Length];
int y = 85, temp = 220, run = 0;
for (int index = 13; index < list.Length; index++) // loads all pictures and create pictureboxes
{
picturebox[i][index] = new PictureBox();
picturebox[i][index].Image = Image.FromFile(path + index + ".jpg");
this.Controls.Add(picturebox[i][index]);
temp = temp + 200;
if (index % 4 == 0)
{
if (run != 0)
y = y + 200;
run++;
temp = 220;
}
picturebox[i][index].Location = new Point(temp, y);
picturebox[i][index].Size = new Size(180, 180);
picturebox[i][index].Name = Convert.ToString(index);
picturebox[i][index].SizeMode = PictureBoxSizeMode.Zoom;
picturebox[i][index].BackColor = Color.FromArgb(35, 35, 35);
picturebox[i][index].Click += new System.EventHandler(PictureBox_Click);
}
}
}
I'm trying to hide a jagged array which is a picturebox in winsform c#, but i keep getting an error, is hiding a jagged array possible? This is the code that i'm having trouble with.
for (int i = 0; i < picturebox.Length; i++)
{
picturebox[0][i].Hide();
}
This is the error i get
ERROR : A first chance exception of type 'System.NullReferenceException' occurred in APPD Assignment 2.exe (Additional information: Object reference not set to an instance of an object.)
Aren't you using the wrong length here?
for (int i = 0; i < picturebox.Length; i++)
Should be
for (int i = 0; i < picturebox[0].Length; i++)
When you load the pictureboxes, you're only starting at index 13, so you either need to start the hide loop at 13, or check for null.
for (int i = 13; i < picturebox[0].Length; i++)
{ ... }
or
for (int i = 0; i < picturebox[0].Length; i++)
{
if (picturebox[0][i] != null)
{
picturebox[0][i].Hide();
}
}