Copying values from dynamic textbox to jagged array error - c#

How can I copy the values from dynamic textboxes to a jagged array? I tried with a for cycle but I constantly get this error message:"Object reference not set to an instance of an object." What can be the problem?(the textboxes are also made with jagged arrays) Here is the full code, you can find the problematic lines in the first lines of the button1 event handler link
for (int a = 0; a < nr; a++)
{
for (int b = 0; b < nr+ 1; b++)
{
array[a][b] =int.Parse(TB[a][b].Text);
}
}
(Here's the full code:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int ismeretlen = 2;
TextBox[][] TB;
string file = "3ismeretlen.dat";
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int[][] egyenletek = new int[ismeretlen][];
for (int a = 0; a < ismeretlen; a++)
{
for (int b = 0; b < ismeretlen + 1; b++)
{
egyenletek[a][b] =int.Parse(TB[a][b].Text);
}
}
int változószám = TB[0].Length;
for (int i = 0; i < változószám - 1; i++)
{
for (int j = i; j < változószám - 1; j++)
{
int[] d = new int[változószám];
for (int x = 0; x < változószám; x++)
{
if (i == j && egyenletek[j][i] == 0)
{
bool changed = false;
for (int z = egyenletek.Length - 1; z > i; z--)
{
if (egyenletek[z][i] != 0)
{
int[] temp = new int[változószám];
temp = egyenletek[z];
egyenletek[z] = egyenletek[j];
egyenletek[j] = temp;
changed = true;
}
}
if (!changed)
{
textBox1.Text += "Az egyenletrendszernek nincs megoldása!\r\n";
return;
}
}
if (egyenletek[j][i] != 0)
{
d[x] = egyenletek[j][x] / egyenletek[j][i];
}
else
{
d[x] = egyenletek[j][x];
}
}
egyenletek[j] = d;
}
for (int y = i + 1; y < egyenletek.Length; y++)
{
int[] f = new int[változószám];
for (int g = 0; g < változószám; g++)
{
if (egyenletek[y][i] != 0)
{
f[g] = egyenletek[y][g] - egyenletek[i][g];
}
else
{
f[g] = egyenletek[y][g];
}
}
egyenletek[y] = f;
}
}
double val = 0;
int k = változószám - 2;
double[] eredmény = new double[egyenletek.Length];
for (int i = egyenletek.Length - 1; i >= 0; i--)
{
val = egyenletek[i][változószám - 1];
for (int x = változószám - 2; x > k; x--)
{
val -= egyenletek[i][x] * eredmény[x];
}
eredmény[i] = val / egyenletek[i][i];
if (eredmény[i].ToString() == "NaN" || eredmény[i].ToString().Contains("Végtelen sok megoldás."))
{
textBox1.Text += "Az egyenletrendszernek nincs megoldása!\n";
return;
}
k--;
TextBox[] megoldás = new TextBox[ismeretlen];
for (int b = 0; b < ismeretlen; i++)
{
megoldás[b] = new TextBox();
megoldás[b].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
megoldás[b].Left = 536+ b * 36;
megoldás[b].Top = 36 * b + 10;
megoldás[b].Width = 35;
megoldás[b].Font = new Font(megoldás[b].Font.FontFamily, 16);
megoldás[b].BackColor = Color.Cyan;
megoldás[b].TextAlign = HorizontalAlignment.Center;
megoldás[b].Text = eredmény[ismeretlen - 1].ToString();
this.panel1.Controls.Add(megoldás[b]);
}
FileStream fs = new FileStream(file, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
for (int r = 0; r < ismeretlen; r++)
for (int t = 0; t < ismeretlen + 1; t++)
bw.Write(egyenletek[r][t]);
bw.Close();
fs.Close();
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Maximum = 6;
numericUpDown1.Minimum = 2;
}
private void Generál_Click(object sender, EventArgs e)
{
this.panel1.Controls.Clear();
ismeretlen = (int)numericUpDown1.Value;
TB = new TextBox[ismeretlen][];
for(int i = 0; i < ismeretlen; i++)
TB[i] = new TextBox[ismeretlen + 1];
int height = 20;
int width = 40;
int curX = 10;
int curY = 10;
for(int i = 0; i < ismeretlen; i++)
{
for(int j = 0; j < ismeretlen + 1; j++)
{
TextBox txtbox = new TextBox();
txtbox = new TextBox();
txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
txtbox.Left = curX;
txtbox.Top = curY;
txtbox.Width = width;
txtbox.Height = height;
txtbox.Font = new Font(txtbox.Font.FontFamily, 16);
txtbox.BackColor = Color.Azure;
txtbox.TextAlign = HorizontalAlignment.Center;
TB[i][j] = txtbox;
this.panel1.Controls.Add(TB[i][j]); // Add as a child of panel
curX += width + 15;
}
curX = 10;
curY = curY + height + 20;
}
}
private void Ment_Click(object sender, EventArgs e)
{
}
}
}

In this line of code, you only initialize the first dimension of your array:
int[][] egyenletek = new int[ismeretlen][];
But you then use it before initializing the second dimension a handful of lines later (so this dimension is null (an object not set to a reference)):
egyenletek[a][b] =int.Parse(TB[a][b].Text);
Before that line, you should initialize the 2nd dimension in some way. You did this at another part of your code, in the lines of 172-173 in your jsfiddle link.
In general, when you see this error you should evaluate what objects you are reading from and assigning to and ensure they have been initialized (ie. they are not null).

Related

I got a stack overflow error and I can't figure out what causes it

I am trying to generate a maze and I faced a stack overflow error while trying to do divide and conquer kind of approach to my 2D array.I will have to post the whole code since I have no idea what causes it and i am very inexperienced in the subject.
this is the details : System.StackOverflowException
HResult=0x800703E9
Message=
this is where the exception happens
!https://imgur.com/a/iwX8pKY
https://www.robinsnyder.com/MazeStaticGif I got the idea from here.
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
namespace labirentVize2
{
class labirentOlustur
{ public static int delik;
public static int x=0;
public static int y;
public static int N = 30;
public static int boy = 30;
int[,] uret = new int[N, N];
public Array girisCikis(int[,] uret)
{
int en1=1;
Random giris = new Random();
int gir = giris.Next(1, 29);
if (gir > 14)
{
int gir2 = giris.Next(15, 28);
int gir3 = (gir + gir2)/2;
uret[0, gir] = 1;
uret[0, gir2] = 1;
uret[0, gir3] = 1;
}
else
{
int gir2 = giris.Next(1, 15);
int gir3 = (gir + gir2)/2;
uret[0, gir] = 1;
uret[0, gir2] = 1;
uret[0, gir3] = 1;
}
Random cikis = new Random();
int cik = cikis.Next(1, 28);
if (cik > 14)
{
int cik2 = cikis.Next(1, 28);
int cik3 = (cik + cik2)/2;
uret[29, cik] = 1;
uret[29, cik2] = 1;
uret[29, cik3] = 1;
}
else
{
int cik2 = cikis.Next(15, 28);
int cik3 = (cik + cik2)/2;
uret[29, cik] = 1;
uret[29, cik2] = 1;
uret[29, cik3] = 1;
}
labYap(uret, en1, x, boy);
return uret;
}
void labGoster(int[,] uret)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
Console.Write(" " + uret[i, j] + " ");
Console.WriteLine();
}
}
public void labYap(int[,] uret, int en1, int x, int boy)
{
int total1 = 0;
Random rand2 = new Random();
for (int i = 0; i < uret.GetLength(0); i++)
{
for (int j = 0; j < uret.GetLength(1); j++)
{
total1 += uret[i, j];}
}
if(total1 > 800)
{
labGoster(uret);
}
else if (total1 == 784)
{
Random rand1 = new Random();
en1 = rand1.Next(2, 29);
x = 30;
delik = rand2.Next(1, 29);
}
else
{
Random rand = new Random();
en1 = rand.Next(1, en1);
boy = boy - en1;
delik = rand2.Next(1, en1);
}
if (diziUstToplam(uret, en1) >= diziAltToplam(uret, en1))
{
y = en1;
for (int j = 0; j < x ; j++)
{
uret[en1, j] = 0;
}
uret[en1, delik] = 1;
labYap(uret, en1, x, boy);
labYap(uret, boy, x, boy);
}
else
{ x = en1;
for (int j = 0; j < en1; j++)
{
uret[j, en1] = 0;
}
uret[delik, en1] = 1;
labYap(uret, en1, x, boy);
labYap(uret, en1, x, boy);
}
int diziUstToplam(int[,] uret, int en1)
{
int total = 0;
// Dizinin ilk boyutu için
for (int i = 0; i < en1; i++)
{
// Dizinin ikinci boyutu için
for (int j = 0; j < uret.GetLength(1); j++)
{
total += uret[i, j];
}
}
return total;
}
int diziAltToplam(int[,] uret, int en1)
{
int total = 0;
// Dizinin ilk boyutu için
for (int i = en1; i < 30; i++)
{
// Dizinin ikinci boyutu için
for (int j = 0; j < uret.GetLength(1); j++)
{
total += uret[i, j];
}
}
return total;
}
int rastgeleSayi()
{
Random rand = new Random();
int en1 = rand.Next(1, 29);
return en1;
}
}
}
}
The stack overflow error is because you call labYear() too many times from within itself. You need to ensure that you have some escape condition where the function can return.
See more here: https://learn.microsoft.com/en-us/dotnet/api/system.stackoverflowexception?view=net-6.0

check the condition and remove dynamic controls accordingly c#

I have a combobox which has values 4-9, and according to that value I want generate runtime labels and textboxes. When I click on 6 then the code can generate 6 labels and textboxes as required, but when I click on 5 again one label and textbox should disappear or if I click on 4 again 2 labels and textboxes should disappear....which is not happening. I have this code in c#. What changes should I make in this code? Is there any other way that I can do this code?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "4")
{
checkBox1.Visible = true;
for (int i = 0; i < 4; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 4; i1++)
{
addlabel1(i1);
}
}
if (comboBox1.Text == "5")
{
checkBox1.Visible = true;
for (int i = 0; i < 5; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 5; i1++)
{
addlabel1(i1);
}
}
if (comboBox1.Text == "6")
{
checkBox1.Visible = true;
for (int i = 0; i < 6; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 6; i1++)
{
addlabel1(i1);
}
}
}
void addlabel(int i)
{
int left = 70;
int top = 100;
int step_x = 80;
int step_y = 30;
new Label()
{
Name = $"label{i}",
Text = "Enter Subject:",
Location = new Point(left, top + step_y * i),
Parent = this,
};
left += step_x;
int left1 = 357;
int top1 = 100;
int step_x1 = 80;
int step_y1 = 30;
new Label()
{
Name = $"label{i}",
Text = "Total Marks:",
Location = new Point(left1, top1 + step_y1 * i),
Parent = this,
};
left1 += step_x1;
}
void addlabel1(int i1)
{
int left = 200;
int top = 100;
int step_x = 80;
int step_y = 30;
new TextBox()
{
Name = $"textbox{i1}",
Text = "",
Size = new Size(122, 20),
Location = new Point(left, top + step_y * i1),
Parent = this,
};
left += step_x;
int left1 = 480;
int top1 = 100;
int step_x1 = 80;
int step_y1 = 30;
new TextBox()
{
Name = $"textbox{i1}",
Text = "",
Size = new Size(122, 20),
Location = new Point(left1, top1 + step_y1 * i1),
Parent = this,
};
left1 += step_x1;
}
Any Suggestions? Help me out.
Try the following code:
public partial class Form1 : Form
{
private int prev = 0;
private Point lblLocation = new Point(70, 100);
private Point tbLocation = new Point(170, 100);
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int cur = Convert.ToInt32(comboBox1.SelectedItem);
int tmp = cur - prev;
if (tmp > 0)
{
// add new controls
for (int i = 1; i <= tmp; i++)
{
AddLabel(prev + i);
AddTextBox(prev + i);
lblLocation.Y += 30;
tbLocation.Y += 30;
}
prev = cur;
}
else
{
// remove controls
tmp = Math.Abs(tmp);
for(int i= 0; i < tmp; i++)
{
RemoveControl($"lbl{prev}");
RemoveControl($"tb{prev}");
lblLocation.Y -= 30;
tbLocation.Y -= 30;
prev--;
}
}
}
private void AddLabel(int i)
{
new Label()
{
Name = $"lbl{i}",
Text = $"lbl{i}",
Location = lblLocation,
Parent = this
};
}
private void AddTextBox(int i)
{
new TextBox()
{
Name = $"tb{i}",
Text = $"tb{i}",
Location = tbLocation,
Parent = this
};
}
private void RemoveControl(string name)
{
foreach (Control item in Controls.OfType<Control>())
{
if (item.Name == name)
{
Controls.Remove(item);
}
}
}
}

c# line chart. Why does my line go to 0 instead of just staying at the last point?

So i made a button and when you press it displays a graph of the arrays, but it goes to 0 when its finished.
private void button14_Click(object sender, EventArgs e)
{
for (int i = 0; i <= period; i++)
{
xos[i] = i+1;
yos[i] = pot[i];
listBox1.Items.Add(xos[i]);
listBox1.Items.Add("y " + yos[i]);
}
for (int i = 0; i <= period; i++)
{
x2os[i] = i + 1;
y2os[i] = pot2[i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
}
Here is a photo of chart
Don't create the arrays before you know how long they need to be, just declare the references:
public double[] xos;
public double[] yos;
public double[] y2os;
public double[] x2os;
Then when you use them, create the actual arrays:
private void button14_Click(object sender, EventArgs e)
{
xos = new double[period + 1];
yos = new double[period + 1];
x2os = new double[period + 1];
y2os = new double[period + 1];
for (int i = 0; i <= period; i++)
{
xos[i] = i+1;
yos[i] = pot[i];
listBox1.Items.Add(xos[i]);
listBox1.Items.Add("y " + yos[i]);
}
for (int i = 0; i <= period; i++)
{
x2os[i] = i + 1;
y2os[i] = pot2[i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.Series["Potražnja"].Points.DataBindXY(xos, yos);
chart1.Series["Predviđanje"].Points.DataBindXY(x2os,y2os);
}
To make the yellow line skip the first zero values, you would need to check for those first, and then create the arrays:
var start = 0;
while (start <= period && pot2[start] == 0) {
start++;
}
x2os = new double[period - start + 1];
y2os = new double[period - start + 1];
for (int i = 0; i <= period - start; i++)
{
x2os[i] = start + i + 1;
y2os[i] = pot2[start + i];
listBox2.Items.Add(x2os[i]);
listBox2.Items.Add("y "+y2os[i]);
}

After changing tabpage, the combobox crashes

I have a combobox outside my tabcontrol. In each tab control there is a datagridview full of values. In the combobox you can choose a conversion for all values. For example eV→meV.
When i am in the first tab and use the combobox there are no problems, but after i switch the tab and then wanna use the combobox the program list down however the whole combobox is full of try/catch
private void OpenB_Click(object sender, EventArgs e)
{
string[] result = new string[2];
bool lesen = false;
int Spalte = 0;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//Datagridview will be rested, so all values and rows are removed
for (int i = 1; i < Anzahl; i++)
{
DGV[i].Rows.Clear();
}
Anzahl = openFileDialog1.FileNames.Length;
counter = new int[Anzahl];
try
{
if (tabControl1.TabCount < Anzahl)
{
for (int i = 1; i <= Anzahl; i++)
{
if (i > tabControl1.TabCount)
{
string title = "Tab " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
DataGridView NewDGV = new DataGridView();
NewDGV.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
NewDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
NewDGV.Columns.Add("Energy", "Energy");
NewDGV.Columns[0].ReadOnly = true;
NewDGV.Columns.Add("Count Rate", "Count Rate");
NewDGV.Columns[1].ReadOnly = true;
NewDGV.Location = new System.Drawing.Point(3, 3);
NewDGV.Name = "NewDGV" + Convert.ToString(i);
NewDGV.RowHeadersVisible = false;
NewDGV.Size = new System.Drawing.Size(276, 379);
NewDGV.TabIndex = i;
foreach (DataGridViewColumn col in NewDGV.Columns)
col.SortMode = DataGridViewColumnSortMode.NotSortable;
NewDGV.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
DGV.Add(NewDGV);
tabControl1.TabPages[i - 1].Controls.Add(NewDGV);
}
}
}
else if (tabControl1.TabCount > Anzahl)
{
for (int i = tabControl1.TabCount - 1; i >= Anzahl; i--)
{
tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
}
}
}
catch { }
try
{
//Double arrays and Datagridview will be attuned to the count of data
eV = new double[openFileDialog1.FileNames.Length][];
meV = new double[openFileDialog1.FileNames.Length][];
cm = new double[openFileDialog1.FileNames.Length][];
CR = new double[openFileDialog1.FileNames.Length][];
CRmax = new double[openFileDialog1.FileNames.Length][];
for (int i = 0; i < Anzahl; i++)
{
//Naming the columns after data names
string[] Dateiname = openFileDialog1.FileNames[i].Split('\\');
int L = Dateiname.Length;
tabControl1.TabPages[i].Text = Dateiname[L-1];
}
}
catch
{
}
//Datafiles will be read one by one
DataRead(result, ref lesen, ref Spalte);
}
}
/// Reading loop
///
/// double[] eV2 = Energy values of the current data file in eV
/// double[] meV2 = Energy values of the current data file in meV
/// double[] cm2 = Energy values of the current data file in cm^-1
/// double[] CR2 = Intensities of the current data file in CR
/// double[] CRmax2 = normalizied Intensities of the current data file in 1/CRmax
private void DataRead(string[] result, ref bool lesen, ref int Spalte)
{
for (Spalte = 0; Spalte < Anzahl; Spalte++)
{
string line;
lesen = false;
counter[Spalte] = 0;
try
{
Ursprung = openFileDialog1.FileNames[Spalte];
//initialize stream reader
System.IO.StreamReader file1 = new System.IO.StreamReader(openFileDialog1.FileNames[Spalte]);
//read line per line in stream reader
while (((line = file1.ReadLine()) != null))
{
counter[Spalte]++;
Count2 = counter[Spalte];
Count2 = Count2 / 2;
try
{
string[] splitter = line.Split(' ');
if ((splitter[0] == "S") && (splitter[1] == "0000"))
{
lesen = true;
counter[Spalte] = 0;
}
if (lesen == true)
{
//Rows will be filled an added with data value strings
if (counter[Spalte] % 2 == 0)
{
result[0] = splitter[2];
}
else
{
result[1] = splitter[2];
dataGridView1.Rows.Add();
DGV[Spalte].Rows.Add();
int Zeile = (counter[Spalte] - 1) / 2;
DGV[Spalte][0, Zeile].Value = result[0];
DGV[Spalte][1, Zeile].Value = result[1];
}
}
}
catch
{
continue;
}
}
//Streamreader is closed
file1.Close();
counter[Spalte] = counter[Spalte] / 2;
//Current datagridviw values are saved in arrays
//The conversions will be calculated and saved in new arrays
//So every unit gets its own array
double[] eV2 = new double[counter[Spalte]];
double[] meV2 = new double[counter[Spalte]];
double[] cm2 = new double[counter[Spalte]];
double[] CR2 = new double[counter[Spalte]];
double[] CRmax2 = new double[counter[Spalte]];
//Conversion calculation
for (int i = 0; i < counter[Spalte]; i++)
{
eV2[i] = Convert.ToDouble(DGV[Spalte][0, i].Value);
CR2[i] = Convert.ToDouble(DGV[Spalte][1, i].Value);
meV2[i] = 1000 * eV2[i];
cm2[i] = 8066 * eV2[i];
}
//Current file's arrays are saved in double arrays
eV[Spalte] = eV2;
CR[Spalte] = CR2;
meV[Spalte] = meV2;
cm[Spalte] = cm2;
for (int i = 0; i < counter[Spalte]; i++)
{
CRmax2[i] = CR2[i] / CR2.Max();
}
CRmax[Spalte] = CRmax2;
//Chosen conversion replaces values in datagridview
if (Hilfe == 1)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV2[i];
}
}
else if (Hilfe == 2)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = cm2[i];
}
}
if (Hilfe2 == 1)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][1, i].Value = CRmax2[i];
}
}
}
catch
{
MessageBox.Show("Es ist ein Fehler beim Einlesen eingetreten");
}
}
}
/// Energy Unit
/// Choses between eV, meV, 1/cm
/// Datagridview values are replaced by the unit array values
/// Hilfe... Saves current energy unit
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int L = comboBox1.SelectedIndex;
try
{
if (L == 0)
{
Hilfe = 1;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV[Spalte][i];
}
}
}
catch
{
}
}
if (L == 1)
{
Hilfe = 2;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = cm[Spalte][i];
}
}
}
catch
{
}
}
if (L == 2)
{
Hilfe = 0;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = eV[Spalte][i];
}
}
}
catch
{
}
}
}
catch { }
}
Accept these answer if you dont know how to post your own answer?.
Answer:
Problem fixed. The reason was the dgv.autosizemode , when i deactivate it before the conversions it runs.
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV[Spalte][i];
}
DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
}

Decreasing Counter not Working

I am trying to make a counter using an array that I used when I created several images, after a certain amount of time the image should disappear if it is not clicked, I am new to C#, and I am very confused. I think I have it for the most part, but it does not seem to work, thanks. In the code I include the two methods that use this array, CreateImage() which creates the "Mole" image, then adds it to a random spot on the grid, and deleteMole() where I am trying to delete this mole after 4 seconds.
Code:
private void ChangeImage()
{
string Moleimage = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
String MoleImageFunction = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
for (int j = 0; j > NumofImages; j++)
{
ListArray[j]++;
}
Image newImage = HoleImage();
molePopup = MoleImage();
int numCol = Convert.ToInt32(NumberOfColumns);
int ranCol = randomColumns.Next(1, numCol);
int ranRow = randomRow.Next(1, NumberofRows);
Image mole = new Image();
//for (int i = 0; i < NumofImages; i++)
//{
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + ListArray;
mole.Width = ImageSize;
mole.Height = ImageHeight;
//}
Grid.SetColumn(mole, ranCol);
Grid.SetRow(mole, ranRow);
grid_Main.Children.Add(mole);
//Calling MoileLifeCounter for Mole Death
moleLifeCounter();
mole.MouseUp += new MouseButtonEventHandler((o, e) =>
{
grid_Main.Children.Remove(mole);
MolePoints++;
});
}
private void deleteMole()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
int NumberofImages;
NumberofImages = Convert.ToInt32(NumofImages);
for (int j = 0; j > NumofImages; j++)
{
ListArray[j]--;
if (ListArray[j] == 0)
{
int numCol = Convert.ToInt32(NumberOfColumns);
int ranCol = randomColumns.Next(1, numCol);
int ranRow = randomRow.Next(1, NumberofRows);
Image newImage = HoleImage();
Grid.SetColumn(HoleImage(), ranCol);
Grid.SetRow(HoleImage(), ranRow);
grid_Main.Children.Add(HoleImage());
Console.WriteLine("TIMER WORKED!");
}
else
{
break;
}
}
}
looks to me like the loop which decrements the counter is never entered
for (int j = 0; j > NumofImages; j++) // wrong!
should be
for (int j = 0; j < NumofImages; j++)

Categories

Resources