I have been making a card matching game in C#. It has a 12 card set up (2 of each photo so 6 photos in total) using a picture array, and when you press the cards it finds out if the photos are a match by using another array (a int array with the numbers 1-6 repeated twice, same as the photos) I use this code to randomize the numbers
Image away;
int tagger;
for (int i = 1; i < 13; i++)
{
cards[i].Visible = true;
away = pics[i];
tagger = tags[i];
int h = random.Next(1, 6);
pics[i] = pics[h];
tags[i] = tags[h];
pics[h] = away;
tags[h] = tagger;
}
for (int i = 1; i < 13; i++)
{
cards[i].Image = pics[i];
}
It works for about 4 of the matches, then it says the matches are wrong for the rest... can anyone help?
here is the rest of my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int a = 0;
PictureBox card = null;
Image[] pics = new Image[13];
int[] tags = new int[13];
PictureBox[] cards = new PictureBox[13];
Random random = new Random();
public Form1()
{
InitializeComponent();
pics[1] = Image.FromFile(#"h:\profile\desktop\game\photos\g1.jpg");
pics[2] = Image.FromFile(#"h:\profile\desktop\game\photos\g1.jpg");
pics[3] = Image.FromFile(#"h:\profile\desktop\game\photos\g2.jpg");
pics[4] = Image.FromFile(#"h:\profile\desktop\game\photos\g2.jpg");
pics[5] = Image.FromFile(#"h:\profile\desktop\game\photos\g3.jpg");
pics[6] = Image.FromFile(#"h:\profile\desktop\game\photos\g3.jpg");
pics[7] = Image.FromFile(#"h:\profile\desktop\game\photos\g4.jpg");
pics[8] = Image.FromFile(#"h:\profile\desktop\game\photos\g4.jpg");
pics[9] = Image.FromFile(#"h:\profile\desktop\game\photos\g5.jpg");
pics[10] = Image.FromFile(#"h:\profile\desktop\game\photos\g5.jpg");
pics[11] = Image.FromFile(#"h:\profile\desktop\game\photos\g6.jpg");
pics[12] = Image.FromFile(#"h:\profile\desktop\game\photos\g6.jpg");
tags[1] = 1;
tags[2] = 1;
tags[3] = 2;
tags[4] = 2;
tags[5] = 3;
tags[6] = 3;
tags[7] = 4;
tags[8] = 4;
tags[9] = 5;
tags[10] = 5;
tags[11] = 6;
tags[12] = 6;
cards[1] = pictureBox1;
cards[2] = pictureBox2;
cards[3] = pictureBox3;
cards[4] = pictureBox4;
cards[5] = pictureBox8;
cards[6] = pictureBox7;
cards[7] = pictureBox6;
cards[8] = pictureBox5;
cards[9] = pictureBox9;
cards[10] = pictureBox10;
cards[11] = pictureBox11;
cards[12] = pictureBox12;
}
private void click_card(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
string s = pic.Name;
string s1 = s.Substring(10);
int num = int.Parse(s1);
if (card == null)
{
card = pic;
a = num;
}
else if (a > 0)
{
int one = tags[a];
int two = tags[num];
if (one == two)
{
System.Windows.Forms.MessageBox.Show("Correct!");
a = 0;
pic.Visible = false;
card.Visible = false;
card = null;
}
else if (one != two)
{
System.Windows.Forms.MessageBox.Show("Wrong!");
card = null;
a = 0;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Image away;
int tagger;
for (int i = 1; i < 13; i++)
{
cards[i].Visible = true;
away = pics[i];
tagger = tags[i];
int h = random.Next(1, 6);
pics[i] = pics[h];
tags[i] = tags[h];
pics[h] = away;
tags[h] = tagger;
}
for (int i = 1; i < 13; i++)
{
cards[i].Image = pics[i];
}
}
}
}
To comment on your "shuffler".
If cards is your array, then your for loop should start at 0. Arrays are zero based. You have 12 cards, so you'll 0 - 11 indexes. Then you should use the Length property of the array to control your for loop.
When you use random.next(1, 6), you're only generating a random number from 1 - 5. Indexes 0 & 6 - 11, are never being generated, so are never being shuffled.
Lastly, I don't see why you need the extra for loop, just add the assigning of Image to the bottom of your for loop after the swapping.
Image away;
int tagger;
for (int i = 1; i < cards.Length; i++)
{
cards[i].Visible = true;
away = pics[i];
tagger = tags[i];
int h = random.Next(1, cards.Length + 1);
pics[i] = pics[h];
tags[i] = tags[h];
pics[h] = away;
tags[h] = tagger;
cards[i].Image = pics[i];
}
Related
Difficulty in source conversion
I have looked up the internal functions and related documentation supported by the DLL, but I have difficulty finding matching functions or data types.
[Calibration Find value]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using OpenCvSharp;`
namespace Project_NFT
{
class CalibrateCamera
{
public const string ImageCalibration = "C:\\Users\\JL\\Documents\\Visual Studio 2008\\Projects\\Project NET\\Project NFT\\Image/{0:D2}.jpg";
public CalibrateCamera()
{
const int IMAGE_NUM = 3;
const int PAT_ROW = 7;
const int PAT_COL = 10;
const int PAT_SIZE = PAT_ROW * PAT_COL;
const int ALL_POINTS = IMAGE_NUM * PAT_SIZE;
const float CHESS_SIZE = 24.0f;
CvSize patternSize = new CvSize(PAT_COL, PAT_ROW);
IplImage[] srcImg = new IplImage[IMAGE_NUM];
for (int i = 0; i < IMAGE_NUM; i++)
{
string path = string.Format(ImageCalibration, i);
srcImg[i] = new IplImage(path, LoadMode.Color);
}
CvPoint3D32f[] objects = new CvPoint3D32f[ALL_POINTS];
for (int i = 0; i < IMAGE_NUM; i++)
{
for (int j = 0; j < PAT_ROW; j++)
{
for (int k = 0; k < PAT_COL; k++)
{
objects[(i * PAT_SIZE) + (j * PAT_COL) + k] = new CvPoint3D32f
{
X = j * CHESS_SIZE,
Y = k * CHESS_SIZE,
Z = 0.0f
};
}
}
}
CvMat objectPoints = new CvMat(ALL_POINTS, 3, MatrixType.F32C1, objects);
int foundNum = 0;
List<CvPoint2D32f> allCorners = new List<CvPoint2D32f>(ALL_POINTS);
int[] pCount = new int[IMAGE_NUM];
using (CvWindow window = new CvWindow("Calibration", WindowMode.AutoSize))
{
for (int i = 0; i < IMAGE_NUM; i++)
{
CvPoint2D32f[] corners;
bool found = Cv.FindChessboardCorners(srcImg[i], patternSize, out corners);
Debug.Print("{0:D2}...", i);
if (found)
{
Debug.Print("ok");
foundNum++;
}
else
{
Debug.Print("fail");
}
using (IplImage srcGray = new IplImage(srcImg[i].Size, BitDepth.U8, 1))
{
Cv.CvtColor(srcImg[i], srcGray, ColorConversion.BgrToGray);
Cv.FindCornerSubPix(srcGray, corners, corners.Length, new CvSize(3, 3), new CvSize(-1, -1), new CvTermCriteria(20, 0.03));
Cv.DrawChessboardCorners(srcImg[i], patternSize, corners, found);
pCount[i] = corners.Length;
window.ShowImage(srcImg[i]);
Cv.WaitKey(0);
}
allCorners.AddRange(corners);
}
if (foundNum != IMAGE_NUM)
{
Debug.Assert(false);
}
}
CvMat imagePoints = new CvMat(ALL_POINTS, 1, MatrixType.F32C2, allCorners.ToArray());
CvMat pointCounts = new CvMat(IMAGE_NUM, 1, MatrixType.S32C1, pCount);
CvMat intrinsic, distortion, rotation, translation;
Cv.CalibrateCamera2(objectPoints, imagePoints, pointCounts, new CvSize(640, 480), out intrinsic, out distortion, out rotation, out translation, CalibrationFlag.Default);
CvMat subImagePoints, subObjectPoints;
Cv.GetRows(imagePoints, out subImagePoints, 0, PAT_SIZE);
Cv.GetRows(objectPoints, out subObjectPoints, 0, PAT_SIZE);
Cv.FindExtrinsicCameraParams2(subObjectPoints, subImagePoints, intrinsic, distortion, out rotation, out translation);
using (CvFileStorage fs = new CvFileStorage("camera.xml", null, FileStorageMode.Write))
{
fs.Write("intrinsic", intrinsic);
fs.Write("rotation", rotation);
fs.Write("translation", translation);
fs.Write("distortion", distortion);
}
foreach (IplImage img in srcImg)
{
img.Dispose();
}
Console.WriteLine(File.ReadAllText("camera.xml"));
Console.Read();
}
}
}
[Calibration Apply Value]
CvMat intrinsic, distortion, extrinsic;
CvFileNode param;
using (CvFileStorage fs = new CvFileStorage("camera.xml", null, FileStorageMode.Read))
{
param = Cv.GetFileNodeByName (fs, null, "intrinsic");
intrinsic = fs.Read<CvMat>(param);
param = Cv.GetFileNodeByName (fs, null, "distortion");
distortion = fs.Read<CvMat>(param);
}
Cv.FindExtrinsicCameraParams2(object_points, image_points,intrinsic, distortion,
out rotation_vector, out translation_vector);
The source is the Camera Calibration reference, which is difficult to convert
I'm trying to make a chart in checkerboard (as pictured in the screenshot), but I can not think of an algorithm for its construction. How can I change my algorithm so that I can build it?
// location for the first point
double x = FirstStationX;
double y = FirstStationY;
double x1 = 0, y1 = 0;
// button separation inline
double spacingX = InlineStations * (InlineSpacing - 1);
// button separation crossline
double spacingY = CrosslineStations * (CrosslineSpacing - 1);
// Cycle by number of blocks (buttons) along the X axis
for (int i = 0; i < InlineButtons; i++)
{
if (i > 0) x = x1 + spacingX + InlineSeparation;
// Cycle by number of blocks (buttons) along the Y axis
for(int j = 0; j < CrosslineButtons; j++)
{
if (j > 0)
{
y = y1 + spacingY + CrosslineSeparation;
}
// Cycle by number of point in buttons along the X axis
for(int k = 0; k < InlineStations; k++)
{
if (k == 0) x1 = x;
else x1 = x1 + InlineSpacing;
// Cycle by number of point in buttons along the Y axis
for(int h = 0; h < CrosslineStations; h++)
{
if (h == 0) y1 = y;
else y1 = y1 + CrosslineSpacing;
listPointsButtonStation.Add(new ObservablePoint(x1, y1));
}
}
}
}
Try code like this
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;
namespace WindowsFormsApplication26
{
public partial class Form1 : Form
{
const string IMAGE_FILENAME = #"c:\temp\image1.jpg";
const int NUMBER_OF_CHART_COLUMNS = 8;
const int NUMBER_OF_CHART_ROWS = 8;
const int MAIN_PANEL_TOP = 20;
const int MAIN_PANEL_LEFT = 20;
const int MAIN_PANEL_WIDTH = 1000;
const int MAIN_PANEl_HEIGHT = 1000;
const int CHART_LEFT = 20;
const int CHART_TOP = 20;
const int CHART_WIDTH = 100;
const int CHART_HEIGHT = 100;
const int CHART_SPACE = 10;
List<MyChart> charts = new List<MyChart>();
public Form1()
{
InitializeComponent();
Panel mainPanel = new Panel();
mainPanel.Left = MAIN_PANEL_LEFT;
mainPanel.Top = MAIN_PANEL_TOP;
mainPanel.Height = MAIN_PANEl_HEIGHT;
mainPanel.Width = MAIN_PANEL_WIDTH;
this.Controls.Add(mainPanel);
for(int row = 0; row < NUMBER_OF_CHART_ROWS; row++)
{
for (int col = 0; col < NUMBER_OF_CHART_COLUMNS; col++)
{
MyChart newChart = new MyChart();
newChart.row = row;
newChart.col = col;
newChart.Width = CHART_WIDTH;
newChart.Height = CHART_HEIGHT;
newChart.Left = col * (CHART_WIDTH + CHART_SPACE);
newChart.Top = row * (CHART_HEIGHT + CHART_SPACE);
newChart.Image = Image.FromFile(IMAGE_FILENAME);
mainPanel.Controls.Add(newChart);
charts.Add(newChart);
}
}
}
}
public class MyChart : PictureBox
{
public int row { get; set; }
public int col { get; set; }
}
}
ok, decided I would mess around and create a very basic Bubble sorting algorithm, I have only spent a couple hours, and this is only my second iteration of the program, and I'm kind of burnt out right now, and I seem to have hit a bit of a wall. I have it designed so that it will produce and display an Integer based on the number of transpositions it made on each round of sorting ( so I can keep an eye on it and make sure it is trending downward) and it is stuck in an infinite loop, returning the value '36' constantly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool sorted = false;
int[] data = new int[100];
data = GenerateData(data);
while (sorted == false)
{
int count = Sort(data);
if (count == 0)
{
sorted = true;
}
else
{
Console.WriteLine("{0}", count);
}
}
}
public static int[] GenerateData(int[] data)
{
Random num = new Random();
for (int x = 0; x < 100; x++)
{
data[x] = num.Next(0, 99);
}
return data;
}
public static int Sort (int[] data)
{
int TempA = 0;
int TempB = 101;
int count = 0;
for (int x =0; x<100; x++)
{
TempA = data[x];
if ((x + 1) < 100)
{
TempB = data[(x + 1)];
}
else
{
TempB = 101;
}
if ( TempA > TempB)
{
data[x++] = TempA;
data[x] = TempB;
count++;
}
}
return count;
}
}
}
I think there is something wrong with these two lines
data[x++] = TempA;
data[x] = TempB;
It should either be
data[x++] = TempA;
data[x--] = TempB;
Or
data[x+1] = TempA;
data[x] = TempB;
Otherwise your for loop will end up skipping elements.
I have a function that takes a variable as a parameter and returns a calculated result. That function splits up into other functions each doing their own calculation. I need the function to run multi threaded.
My code:
for (int i = 0; i < pic.Width; i++)
{
for (int k = 0; k < pic.Height; k++)
{
var localK = k;
var localI = i;
Image bestPic;
new Thread(() =>
{
bestPic = new Bitmap(getBestPic(argb));//THIS IS WHERE THE WRONG VALUES ARE ASSIGNED BECAUSE OF CROSS THREADING
lock (thisLock)
{
g.DrawImage(bestPic, localI * bestPic.Width, localK * bestPic.Height, bestPic.Width, bestPic.Height);
}
}).Start();
}
}
All I need is the function getBestPic to run multi threaded. But how do I run the function getBestPic multi threaded and make the assigning of the returned result to the bestPic variable atomic?
My entire program if needed: This is a montage program.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
namespace test
{
public partial class Form1 : Form
{
private static readonly Object thisLock = new Object();
private Graphics g;
private Bitmap returnImg;
private Bitmap pic;
private int done = 0;
private int pictureWidthAndLength = 200;
private string inputPicName = "test";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DateTime dtStart = DateTime.Now;
pic = new Bitmap(inputPicName + ".jpg");
//MessageBox.Show(pic.GetPixel(1,1).ToArgb().ToString());
//MessageBox.Show(pic.Width.ToString() + " x " + pic.Height.ToString());
returnImg = new Bitmap(pic.Width * pictureWidthAndLength, pic.Height * pictureWidthAndLength);
using (g = Graphics.FromImage(returnImg))
{
Color clr;
int[] argb = new int[4];
for (int i = 0; i < pic.Width; i++)
{
for (int k = 0; k < pic.Height; k++)
{
clr = pic.GetPixel(i, k);
argb[0] = clr.A;
argb[1] = clr.R;
argb[2] = clr.G;
argb[3] = clr.B;
var localK = k;
var localI = i;
Image bestPic;
if (cbxthreading.Checked)
{
new Thread(() =>
{
bestPic = new Bitmap(getBestPic(argb));
lock (thisLock)
{
g.DrawImage(bestPic, localI * bestPic.Width, localK * bestPic.Height, bestPic.Width, bestPic.Height);
done++;
}
}).Start();
}
else
{
//Single threaded
bestPic = new Bitmap(getBestPic(argb));
g.DrawImage(bestPic, localI * pictureWidthAndLength, localK * pictureWidthAndLength, pictureWidthAndLength, pictureWidthAndLength);
}
//MessageBox.Show(getBestPic(argb));
}
}
if (cbxthreading.Checked)
{
int loopNum = pic.Width * pic.Height;
while (done < loopNum) { }
}
}
DateTime dtEnd = DateTime.Now;
MessageBox.Show((dtEnd - dtStart).ToString());
}
//Get picture that is best suited to replace pixel
private string getBestPic(int[] argb)
{
int numOfpics = 5;
int[] currentBest = new int[2];
currentBest[0] = 255;
currentBest[1] = 150;
for (int i = 0; i < numOfpics; i++)
{
int compare = compareARGB(getAverageRGB(new Bitmap((i + 1).ToString()+".jpg")), argb);
if (compare < currentBest[0])
{
currentBest[0] = compare;
currentBest[1] = i + 1;
}
}
return currentBest[1].ToString() + ".jpg";
}
// smaller the value, closer the camparison
private int compareARGB(int[] one, int[] two)
{
int [] tmp = new int[4];
tmp[0] = Convert.ToInt32(Math.Abs(one[0] - two[0]));
tmp[1] = Convert.ToInt32(Math.Abs(one[1] - two[1]));
tmp[2] = Convert.ToInt32(Math.Abs(one[2] - two[2]));
tmp[3] = Convert.ToInt32(Math.Abs(one[3] - two[3]));
return (tmp[0] + tmp[1] + tmp[2] + tmp[3]);
}
//return int arry with size 4 containing the argb values
private int[] getAverageRGB(Bitmap img)
{
Color clr;
int aplha = 0;
int red = 0;
int green = 0;
int blue = 0;
for (int i = 0; i < img.Width; i++)
{
for (int k = 0; k < img.Height; k++)
{
clr = img.GetPixel(i, k);
aplha += clr.A;
red += clr.R;
green += clr.G;
blue += clr.B;
}
}
aplha = aplha / (img.Width * img.Height);
red = red / (img.Width * img.Height);
green = green / (img.Width * img.Height);
blue = blue / (img.Width * img.Height);
int[] re = new int[] {aplha,red,green,blue};
return re;
}
private void button2_Click(object sender, EventArgs e)
{
returnImg.Save(inputPicName+".bmp");
MessageBox.Show("Done!");
}
}
}
The single thread functionality works, but takes long. The multi threaded functionality also finishes in a third of the time of the single threaded, but the result is not correct.
getBestPic() method runs multi-thread as I understand. But the problem is the argb parameter. You initialize it ones and then overwrite its values in for loops.argb is reference type, so only the reference is passed to getBestPic(), so it's referenced values get changed while processed in getBestPic().
I would try to pass it by Value or move int[] argb = new int[4];line to the inside of the second for loop, so you every time initialize new variable. More on passing reference type params here.
Just create a copy of the Value of your argb in the getBestPic() method and use it instead of using the original one
private string getBestPic(int[] argb)
{
int[] argbCopy = argb.ToArray();
int numOfpics = 5;
int[] currentBest = new int[2];
currentBest[0] = 255;
currentBest[1] = 150;
for (int i = 0; i < numOfpics; i++)
{
int compare = compareARGB(getAverageRGB(new Bitmap((i + 1).ToString()+".jpg")), argbCopy);
if (compare < currentBest[0])
{
currentBest[0] = compare;
currentBest[1] = i + 1;
}
}
return currentBest[1].ToString() + ".jpg";
}
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).