datagridview count cells c# - c#

I have a problem. I need to count cells that I activate, they are yellow, but I dont know how i can do it. In geniral I need to select only maximum 15 cells, so i need to count them, but all my tries seems so far away. I tried to cteate a counter, but it doest work. Please, help.
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
//выделение только ячеек
// создаём массив
int[,] Array = new int[8, 10];
byte numbers = 1;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
Array[i, j] = numbers;
numbers++;
}
}
dataGridView1.RowCount = 8;
dataGridView1.ColumnCount = 10;
// программно записываем массив и задаём стиль ячеек
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
dataGridView1.Columns[j].Width = 30;
dataGridView1.Rows[i].Height = 30;
dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();
}
}
}
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.White;
}
else
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;
}
dataGridView1.CurrentCell = null;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
byte _selected = 0;
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
counter(_selected);
}
}
public void counter(int count)
{
count++;enter code here
MessageBox.Show(count.ToString());
}
Here is how form look.
form
The name of the game is Keno and i try to create it. Maybe i have some mistakes, sorry.

Here's my spin on your code. In this snippet I am using int yellowed to keep track of how many cells are yellow. When a user clicks on a cell, the cell counter sets the yellow count. When the mouse button is up(dataGridView1_CellMouseUp), then only the appropriate number of cells are allowed to be made yellow.
using System.Drawing;
using System.Windows.Forms;
namespace DataGridView_47478857
{
public partial class Form1 : Form
{
DataGridView dataGridView1 = new DataGridView();
int yellowed = 0;
int maxYellowed = 15;
public Form1()
{
InitializeComponent();
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.CellMouseUp += dataGridView1_CellMouseUp;
dataGridView1.CellClick += dataGridView1_CellClick;
this.Controls.Add(dataGridView1);
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
//выделение только ячеек
// создаём массив
int[,] Array = new int[8, 10];
byte numbers = 1;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
Array[i, j] = numbers;
numbers++;
}
}
dataGridView1.RowCount = 8;
dataGridView1.ColumnCount = 10;
// программно записываем массив и задаём стиль ячеек
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
dataGridView1.Columns[j].Width = 30;
dataGridView1.Rows[i].Height = 30;
dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();
}
}
}
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.White;
}
else
{
if (yellowed < maxYellowed)//only color code this cell if the yellow cell count has not been exceeded
{
dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;
yellowed++;
}
}
}
dataGridView1.ClearSelection();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
yellowed = 0;
foreach (DataGridViewRow currentRow in dataGridView1.Rows)
{
foreach (DataGridViewCell currentCell in currentRow.Cells)
{
if (currentCell.Style.BackColor == Color.Yellow)
{
yellowed++;
}
}
}
}
}
}

You can use this link to count cell, but you can use another components or WPF to this game.
https://msdn.microsoft.com/en-us/library/x8x9zk5a(v=vs.85).aspx

Related

How to assign value to variables in a 2Dimensional array?

Working on a simple Tic-Tac-Toe Simulator and this is my code for the click event
private void newGame_button_Click(object sender, EventArgs e)
{
Random rand = new Random();
double randNum = rand.Next(0, 2);
const int ROWS = 3;
const int COLS = 3;
string[,] ticTacToe_2dArray = new string[ROWS, COLS]
{
{displayTTT_label1.Text, displayTTT_label2.Text, displayTTT_label3.Text},
{displayTTT_label4.Text, displayTTT_label5.Text, displayTTT_label6.Text},
{displayTTT_label7.Text, displayTTT_label8.Text, displayTTT_label9.Text}
};
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
randNum = rand.Next(0, 2);
if (randNum == 0)
{
ticTacToe_2dArray[row, col] = "O";
}
else if (randNum == 1)
{
ticTacToe_2dArray[row, col] = "X";
}
}
}
}
Label Grid
Trying to pair the result of the conditional test that assigns an X/O to the next corresponding label.Text variable on my label grid. I am pretty sure the current code is just changing the array variable to X/O instead of the label.Text value.
Your problem is that you are storing copies of strings from your text boxes and then updating the copies.
What you need to do is store the text boxes in the dictionary and assign the "X" and "O" strings to their text properties.
private void newGame_button_Click(object sender, EventArgs e)
{
Random rand = new Random();
double randNum = rand.Next(0, 2);
const int ROWS = 3;
const int COLS = 3;
TextBox[,] ticTacToe_2dArray = new TextBox[ROWS, COLS]
{
{displayTTT_label1, displayTTT_label2, displayTTT_label3},
{displayTTT_label4, displayTTT_label5, displayTTT_label6},
{displayTTT_label7, displayTTT_label8, displayTTT_label9}
};
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
randNum = rand.Next(0, 2);
if (randNum == 0)
{
ticTacToe_2dArray[row, col].Text = "O";
}
else if (randNum == 1)
{
ticTacToe_2dArray[row, col].Text = "X";
}
}
}
}

How to rearrange array?

I have an assignment for school and i got a bit stuck in the process.
The idea is to make a program in C# where you can visualize how the insertion sorting algorithm works and for that I'm using an array of buttons with random generated numbers.
It colours green for comparison and red for swapping.
Why do the buttons remain coloured?
public partial class Form1 : Form
{
Button[] but;
int[] A;
int nr_den = 0;
int s1 = 0;
int s2 = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
nr_den = Convert.ToInt16(textBox1.Text);
s1 = Convert.ToInt16(textBox2.Text);
s2= Convert.ToInt16(textBox3.Text);
A = new int[nr_den+1];
Random r = new Random();
for (int i = 1; i <= nr_den; i++)
{
A[i] = r.Next(s1, s2);
}
but = new Button[nr_den + 1];
for (int i = 1; i <= nr_den; i++)
{
but[i] = new Button();
but[i].Text = A[i].ToString();
but[i].Width = 40;
but[i].Height = 40;
flowLayoutPanel1.Controls.Add(but[i]);
}
}
public void exchange(int[] A, int m, int n)
{
string s;
int temp;
but[m].BackColor = Color.Red;
System.Threading.Thread.Sleep(400);
but[n].BackColor = Color.Pink;
System.Threading.Thread.Sleep(400);
temp = A[m];
s = but[m].Text;
A[m] = A[n];
but[m].Text = but[n].Text;
A[n] = temp;
but[n].Text = s;
but[m].Refresh();
but[n].Refresh();
}
public void sort(int[] A)
{
int i, j;
int N = A.Length;
for (j = 1; j < N; j++)
{
for (i = j; i > 0 && A[i] < A[i - 1]; i--)
{
but[i-1].BackColor = Color.Green;
System.Threading.Thread.Sleep(400);
but[i].BackColor = Color.GreenYellow;
System.Threading.Thread.Sleep(400);
but[i].Refresh();
but[i - 1].Refresh();
exchange(A, i, i - 1);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
for (int i=1;i<=nr_den;i++)
richTextBox1.Text += A[i]+ " ";
richTextBox1.Text += " \n";
sort(A);
}
private void button3_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Clear();
}
}
form
restore color after completion
public void sort(int[] A)
{
int i, j;
int N = A.Length;
for (j = 1; j < N; j++)
{
for (i = j; i > 0 && A[i] < A[i - 1]; i--)
{
but[i - 1].BackColor = Color.Green;
System.Threading.Thread.Sleep(400);
but[i].BackColor = Color.GreenYellow;
System.Threading.Thread.Sleep(400);
but[i].Refresh();
but[i - 1].Refresh();
exchange(A, i, i - 1);
but[i-1].BackColor = SystemColors.Control;
but[i].BackColor = SystemColors.Control;
}
}
}
restore color during process
public void sort(int[] A)
{
int i, j;
int N = A.Length;
for (j = 1; j < N; j++)
{
for (i = j; i > 0 && A[i] < A[i - 1]; i--)
{
but[i - 1].BackColor = Color.Green;
System.Threading.Thread.Sleep(400);
but[i].BackColor = Color.GreenYellow;
System.Threading.Thread.Sleep(400);
but[i].Refresh();
but[i - 1].Refresh();
exchange(A, i, i - 1);
but[i-1].BackColor = SystemColors.Control;
but[i].BackColor = SystemColors.Control;
System.Threading.Thread.Sleep(400);
but[i].Refresh();
but[i - 1].Refresh();
}
}
}

CheckedListBox.SelectedItems.Count = 1?

In C#, I have checked list boxes, that I need to store the data in arrays, but when I start the event that writes the objects to the array, I have to set the size of the array, which I naturally set to the amount of items checked. However, the items checked, for both checked list boxes I have is 1, no matter how many I check. Can someone help?
public partial class Form3 : Form
{
public static object[] dtype;
public static bool loaded = false;
bool typeselecte = false;
bool typeselectd = false;
public Form3()
{
InitializeComponent();
}
private void Form3_Shown(object sender, EventArgs e)
{
if (loaded)
{
int counte = 0;
int countd = 0;
types1.Items.AddRange(dtype);
types2.Items.AddRange(dtype);
if (typeselecte)
{
for (int i = 0; i < types1.Items.Count; i++)
{
if (i == Form1.enumber[counte])
{
types1.SelectedItems[i] = Form1.esearch[i];
counte++;
}
}
}
if (typeselectd)
{
for (int j = 0; j < types2.Items.Count; j++)
{
if (j == Form1.dnumber[countd])
{
types2.SelectedItems[j] = Form1.dsearch[j];
countd++;
}
}
}
}
}
public void dtypes()
{
dtype = new object[types1.Items.Count];
for (int i = 0; i < types1.Items.Count; i++)
{
dtype[i] = types1.Items[i];
}
}
private void button1_Click(object sender, EventArgs e)
{
if (types1.SelectedItems.Count > 0)
typeselecte = true;
if (types2.SelectedItems.Count > 0)
typeselectd = true;
Form1.esearch = new object[types1.SelectedItems.Count];
Form1.dsearch = new object[types2.SelectedItems.Count];
Form1.enumber = new int[types1.SelectedItems.Count];
Form1.dnumber = new int[types2.SelectedItems.Count];
int counte = 0;
int countd = 0;
if (typeselecte)
{
for (int i = 0; i < types1.SelectedItems.Count; i++)
Form1.esearch[i] = types1.SelectedItems[i];
}
if (typeselectd)
{
for (int j = 0; j < types2.SelectedItems.Count; j++)
Form1.dsearch[j] = types2.SelectedItems[j];
}
if (typeselecte)
{
for (int k = 0; k < types1.Items.Count; k++)
{
if (Form1.esearch[k] == types1.Items[k])
{
Form1.enumber[counte] = k;
counte++;
}
else
{
k--;
}
}
}
if (typeselectd)
{
for (int l = 0; l < types2.Items.Count; l++)
{
if (Form1.dsearch[l] == types2.Items[l])
{
Form1.dnumber[countd] = l;
countd++;
}
else
{
l--;
}
}
}
this.Close();
}
}
Form1.esearch and dsearch are object arrays, which the size hasn't been picked yet, and e and dnumber are int arrays that have unknown size as well, I just didn't feel the need to put in that code.
I believe you need to use the property CheckedItems as opposed to SelectedItems.

File upload in ASP.NET and C#

i needed a file upload feature in my project built on ASP.NET 3.5 and C#. I followed the following link to do so.
http://support.microsoft.com/kb/323246.
I created a folder named Data and followed everything as stated in the link.But my files didnt get uploaded in that folder named Data.What should i do to upload a file?
Any exception is thrown? Do you give the permission to the account you program run under? What's size of your file upload? Try a small one first, such as 1K file. ASP.NET has a size limit, and you can change the limit in the config file.
class Proizvod {
public string ceo_red, ime, proizvodjac, ram, tip, kamera,slika, ekran,sifra, cena;
public Proizvod(string x) {
ceo_red = x;
slika = x.Split(',')[0];
sifra = x.Split(',')[1];
ime = x.Split(',')[2];
proizvodjac = x.Split(',')[3];
ram = x.Split(',')[4];
tip = x.Split(',')[5];
kamera = x.Split(',')[6];
ekran = x.Split(',')[7];
cena = x.Split(',')[8];
}
}
List<Proizvod> proizvodi = new List<Proizvod>();
protected void Page_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(#"F:\dji\A8\A8\TextFile1.txt");
for (int i = 0; i < 5; i++)
{
proizvodi.Add(new Proizvod(sr.ReadLine()));
}
for (int i = 0; i < proizvodi.Count; i++)
{
bool isti = false;
for (int j = 0; j < DropDownList1.Items.Count; j++)
{
if (proizvodi[i].proizvodjac == DropDownList1.Items[j].Text) isti = true;
}
if (!isti) DropDownList1.Items.Add(proizvodi[i].proizvodjac);
isti = false;
for (int j = 0; j < DropDownList2.Items.Count; j++)
{
if (proizvodi[i].ram == DropDownList2.Items[j].Text) isti = true;
}
if (!isti) DropDownList2.Items.Add(proizvodi[i].ram);
isti = false;
for (int j = 0; j < DropDownList3.Items.Count; j++)
{
if (proizvodi[i].tip == DropDownList3.Items[j].Text) isti = true;
}
if (!isti) DropDownList3.Items.Add(proizvodi[i].tip);
isti = false;
for (int j = 0; j < DropDownList4.Items.Count; j++)
{
if (proizvodi[i].kamera == DropDownList4.Items[j].Text) isti = true;
}
if (!isti) DropDownList4.Items.Add(proizvodi[i].kamera);
isti = false;
for (int j = 0; j < DropDownList5.Items.Count; j++)
{
if (proizvodi[i].ekran == DropDownList5.Items[j].Text) isti = true;
}
if (!isti) DropDownList5.Items.Add(proizvodi[i].ekran);
}
Table1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
List<Proizvod> trazeni = new List<Proizvod>();
for (int i = 0; i < proizvodi.Count; i++)
{
if (proizvodi[i].proizvodjac == DropDownList1.Text && proizvodi[i].ram == DropDownList2.Text && proizvodi[i].tip == DropDownList3.Text && proizvodi[i].kamera == DropDownList4.Text && proizvodi[i].ekran == DropDownList5.Text)
{
trazeni.Add(proizvodi[i]);
}
}
for (int i = 0; i < trazeni.Count; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 9; j++)
{
TableCell tc = new TableCell();
tc.Text = trazeni[i].ceo_red.Split(',')[j];
tr.Cells.Add(tc);
}
Table1.Rows.Add(tr);
}
Table1.Visible = true;
}
}
}

Cannot redraw points which have been set to NULL

I have the following setup:
A TeeChart control with a Colorgrid, and a Points Series added to it:
grid = tChart2.Series[0] as Steema.TeeChart.Styles.ColorGrid;
points = tChart2.Series[1] as Steema.TeeChart.Styles.Points;
To init them, I do:
Random rnd = new Random();
for (int i = 0; i < 128; i++)
{
for (int j = 0; j < 128; j++)
{
grid.Add(j, rnd.Next(255), i);
}
}
for (int i = 0; i < 20; i++)
{
double x = rnd.Next();
double y = rnd.Next();
points.Add(x, y);
}
tChart2.Refresh();
And then I have a button on my form:
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
for (int i = 0; i < 128; i++)
{
for (int j = 0; j < 128; j++)
{
grid.YValues[j + 128 * i] = rnd.Next(255);
}
}
for (int i = 0; i < 20; i++)
{
points.SetNull(i);
}
for (int i = 0; i < rnd.Next(20); i++)
{
points.XValues[i] = rnd.Next(128);
points.YValues[i] = rnd.Next(128);
}
points.BeginUpdate();
points.EndUpdate();
}
But the points do not get drawn. When I remove the for-loop containing the SetNull() statement, then they do get drawn, but I want to be able to clear the points (or hide the points don't want to be seen) without using the Points.Clear()/Points.Add(x, y) methodology.
I've also tried each of the following, but there's no difference.
points.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.DoNotPaint;
points.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.Ignore;
points.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.Skip;
Does anyone know how to accomplish this?
Ok, the problem is caused when you set null all of points. You must know if you use method SetNull the point color is set transparent to make invisible the point. Therefore, if you want solve your problem you only need reset the colors of points you want visible, doing SetNull again or change the points color manually and combining the operation with TreatNullsStyle set to Ignore. In my opinion, I think the best option is use SetNull again as I do in next code:
public Form1()
{
InitializeComponent();
InitializeChart();
}
Steema.TeeChart.Styles.ColorGrid grid;
Steema.TeeChart.Styles.Points points;
private void InitializeChart()
{
grid = new ColorGrid(tChart1.Chart);
points = new Points(tChart1.Chart);
tChart1.Aspect.View3D = false;
Random rnd = new Random();
for (int i = 0; i < 128; i++)
{
for (int j = 0; j < 128; j++)
{
grid.Add(i, rnd.Next(255), j);
}
}
for (int i = 0; i < 20; i++)
{
double x = rnd.Next(100);
double y = rnd.Next(100);
points.Add(x, y);
}
}
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
for (int i = 0; i < 128; i++)
{
for (int j = 0; j < 128; j++)
{
grid.YValues[j + 128 * i] = rnd.Next(255);
}
}
for (int i = 0; i < 20; i++)
{
points.SetNull(i);
}
for (int i = 0; i < rnd.Next(20); i++)
{
points.XValues[i] = rnd.Next(128);
points.YValues[i] = rnd.Next(128);
points.SetNull(i, false);
}
points.TreatNulls = TreatNullsStyle.Ignore;
}
Could you tell us if previous code works in correct way for you?
I hope will helps.
Thanks,

Categories