Assign different text name in an array button using different function - c#

I have a 2x2 button array and I want to give it a different text name.
Button[,] btnSeat = new Button[2, 2];
private void initializeBoard()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
btnSeat[i, j] = new Button();
btnSeat[i, j].Width = 90;
btnSeat[i, j].Height = 90;
pnlSeat.Controls.Add(btnSeat[i, j]);
}
}
IT will have an output like this
How can I assign a text using different function
public void assignName()
{
//assign button names
}

Is there anything stopping you from passing the button as a parameter?
public void assignName(Button button)
{
button.Text = "ex";
}
Edit
Since your naming convention is known ahead of time, you can just use a map.
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
Then if you want to use the separate function, you would need to pass in 3 parameters.
public void assignName(Button button, int row, int column)
{
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
buttonText.Text = buttonNames[row][column];
}
Notes
I would either make the name map static at the class level and assigned in a static constructor or dispense with the function, declare the name map in the initializeBoard method and just use it in there.
For example
private void initializeBoard()
{
string[,] buttonNames = new string[2,2];
buttonNames[0,0] = "dog";
// continue
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
btnSeat[i, j] = new Button();
btnSeat[i, j].Width = 90;
btnSeat[i, j].Height = 90;
btnSeat[i, j].Text = buttonNames[row][column];
pnlSeat.Controls.Add(btnSeat[i, j]);
}
}
}
Does that help?

Related

datagridview count cells 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

how to get array button to a place on form?

private Button[,] arrButton = new Button[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
arrButton[i, j] = new Button();//getting System.IndexOutOfRangeException
arrButton[i, j].Size = new Size(30, 30);
arrButton[i, j].Location = new Point(i * 30, j * 30);
arrButton[i, j].Click += new EventHandler(arrButton_Click);
this.Controls.Add(arrButton[i, j]);
}
}
this.ClientSize = new Size(300, 300);
After create, array button come up top left corner on form.
how to get array button to a place on form
#choz comment is correct, but a couple things to also consider.
Put the row value and column value in constant variables and use the variables, instead of directly using a number repeatedly.
Example:
private const ROW = 10;
private const COL = 10;
private Button[,] arrButton = new Button[ROW, COL];
...
for (int i = 0; i < ROW; i++)
{
// Change from your code -|
// |----------
// V
for (int j = 0; j < COL; j++)
{
// Create your buttons
}
}
If you are going to use constant numerals, then reference the GetUpperBound() from your array to know the last index value of each dimension.
Example:
private Button[,] arrButton = new Button[10, 10];
...
// GetUpperBound(0) = last index of rows (9 in this case)
for (int i = 0; i <= arrButton.GetUpperBound(0); i++)
{
// Change from your code -|
// |----------
// V GetUpperBound(1) = last index of columns (9 in this case)
for (int j = 0; j <= arrButton.GetUpperBound(1); j++)
{
// Create your buttons
}
}
Syntax error! Your second loop should be:
for (int j = 0; j < 10; j++)
You put "i" instead of "j",
Normally, when you get an System.IndexOutOfRangeException. The debugger error is telling you that the loop or loops you are using are counting the elements more than you specific. Check The count of elements you tinkering with.
I think you are making a mistake in inner loop. Can you please paste this and try to run:
private Button[,] arrButton = new Button[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)//Changed i to j
{
arrButton[i, j] = new Button();
arrButton[i, j].Size = new Size(30, 30);
arrButton[i, j].Location = new Point(i * 30, j * 30);
arrButton[i, j].Click += new EventHandler(arrButton_Click);
this.Controls.Add(arrButton[i, j]);
}
}
this.ClientSize = new Size(300, 300);

using dynamically created labels c#

So I have
TableLayoutPanel table = new System.Windows.Forms.TableLayoutPanel();
public int d;
private void button1_Click(object sender, EventArgs e)
{
d = (int)numericUpDown1.Value;
table.ColumnCount = d;
table.RowCount = d;
this.Controls.Add(table);
int k = 1;
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
Label lab = new System.Windows.Forms.Label();
lab.Size = new Size(50, 50);
lab.Text = (k).ToString();
lab.TabIndex = k++;
lab.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(lab, j, i);
}
table.Show();
}
That is one Button Click method.
I have Button2 Click method and I want to change lab.TabIndex or/and lab.Text.
How can I do that?
And second question:
how can i do something on Click one of that labels? Let's say that i want to change a color one of the labels i click...how can I do that?
I'm a beginner so...have mercy :)
define a array of labels (global variable, like you did with table):
Label[] labels;
in button1_Click, add the line of code:
labels = new Label[d*d]; // array of d*d labels
inside the loop, define the specific label, like:
labels[i*d+j] = new System.Windows.Forms.Label();
So your loop will look like:
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
labels[i*d+j] = new System.Windows.Forms.Label();
labels[i*d+j].Size = new Size(50, 50);
labels[i*d+j].Text = (k).ToString();
labels[i*d+j].TabIndex = k++;
labels[i*d+j].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(labels[i*d+j], j, i);
}
you can access them from other buttons also in the same way (labels[n])

C# - How to access property of an array object?

I dont know how to show clearly, so:
Example - I create an array of button like this:
Button[,] _button = new Button[3, 3];
public MainPage()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
_button[i, j] = new Button();
_button[i, j].Name = "btn" + i.ToString() + j.ToString();
_button[i, j].Tag = 0;
//Add Click event Handler for each created button
_button[i, j].Click += _button_Click;
boardGrid.Children.Add(_button[i, j]);
Grid.SetRow(_button[i, j], i);
Grid.SetColumn(_button[i, j], j);
}
} // end MainPage()
private void _button_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
if (...)
b.Tag = 1;
else
b.Tag = 2;
}// end Click Event
Now how can I compare the Tag of 2 buttons in that array like:
b[1,1].Tag == b[1,2].Tag ? ...<do st>... : ....<do st>...
If you need to find position of control in array consider to set Control.Tag to that position instead of search:
_button[i, j].Tag = new System.Drawing.Point{ X = j, Y = i};
And instead of searching just
Point position = (Point)((Button)sender).Tag;
Or if you need more information (like Position + 0/x/empty choice you have) - have custom class to hold all information you need:
enum CellState { Empty, Player1, Player2 };
class TicTacToeCell
{
public Point Position {get;set;}
public CellState State {get;set;}
}
Now when you have position and state - use _buttons array to index access other ones:
Check same row:
Point position = (Point)((Button)sender).Tag;
int player1CountInThisRow = 0;
for (var col = 0; col < 3; col++)
{
if (((TicTacToeCell)(_button[position.Y, col].Tag).State == CellState.Player1)
{
player1CountInThisRow ++;
}
}
This is more of a long-winded clarification than a definite answer, but it may uncover what you're really trying to do:
In the code that you show b is (presumably) a single Button, not an array of buttons. Do you mean:
_button[1,1].Tag == _button[1,2].Tag ? ...<do st>... : ....<do st>...
Or are you trying to compare b (the event sender) to a button relative to it in the array?

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