The code below display the index of each button when clicked the buttons are in an array but all this is done in form load. But I want to do the same thing in the click event and not in the form load how would I go about on doing this:
Image:
Code:
namespace _2DArray
{
public partial class Form1 : Form
{
private Button[,] b;
public Form1()
{
InitializeComponent();
b = new Button[2, 2];
b = new Button[,] { {button1,button2 },
{button3, button4}};
}
public int x = 0;
public int y = 0;
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
b[i, j].Click += new System.EventHandler(this.ClickedButton);
b[i, j].Name = "X: " + i + " " + "Y: " + j;
}
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button: " + s.Name);
}
}
}
Answered like so:
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
int x = int.Parse(s.Name.Split()[1]);
int y = int.Parse(s.Name.Split()[3]);
MessageBox.Show("you have clicked button: " + x +" "+ y);
}
Related
I want to have multiple cards with text and images or just red or blue color filled up. this is how the form should look like. So I am thinking about creating multiple picture boxes or labels But the problem is that the picture boxes are overlapping.
private void button1_Click(object sender, EventArgs e)
{
Createlabels();
}
private void Createlabels()
{
var n = 5;
PictureBox[] p = new PictureBox[n];
for (int i = 0; i < n; i++)
{
p[i] = new PictureBox();
p[i].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\red1.Png");
p[i].SizeMode = PictureBoxSizeMode.Zoom;
p[i].Left = i * 100;
this.Controls.Add(p[i]);
}
}
private void button2_Click(object sender, EventArgs e)
{
Createlabels2();
}
private void Createlabels2()
{
var n = 10;
PictureBox[] q = new PictureBox[n];
for (int j = 0; j < n; j++)
{
q[j] = new PictureBox();
q[j].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\blue.Png");
q[j].SizeMode = PictureBoxSizeMode.Zoom;
q[j].Left = j * 100;
this.Controls.Add(q[j]);
}
}
}
This is the code I did so far my output but it should look like this one
private void button1_Click(object sender, EventArgs e)
{
var n = 12;
Button[] p = new Button[n];
for (int i = 0; i < n; i++)
{
p[i] = new Button();
p[i].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\red1.Png");
p[i].Text = i + " Reserved";
p[i].Left = i * 100;
this.Controls.Add(p[i])
}
}
private void button2_Click(object sender, EventArgs e)
{
Createlabels2();
}
private void Createlabels2()
{
var n = 12;
Button[] q = new Button[n];
for (int j = 0; j < n; j++)
{
q[j] = new Button();
q[j].Image = Image.FromFile(#"C:\Users\sania\Desktop\c sharp project\blue.Png");
//"ImagePanel" is a TableLayoutPanel
q[j].Text = j + 10 + " Booked";
q[j].Top = j * 100;
this.Controls.Add(q[j]);
}
}
}
When I click on a button in my Floqlayoutpanel they should hide at the place where I clicked on them. But instead they disappear and all the other buttons move.
They should hide at their place
But this is what happens
How I create my Buttons:
private void CreateButton()
{
int buttonIndex = 0;
for (int i = 0; i < 16; i++)
{
Button button = new Button();
button.Name = $"Button_{buttonIndex}";
button.Width = 100;
button.Height = 100;
button.Click += OnButtonClick;
button.BackgroundImage = BackSideImage();
flowLayoutPanel1.Controls.Add(button);
buttonIndex++;
}
}
How I hide my Buttons:
private void CompareCards()
{
if (clickedCards.Count >= 3)
{
if (clickedCards[0].PairIndex == clickedCards[1].PairIndex)
{
clickedCards[0].Button.Hide();
clickedCards[1].Button.Hide();
}
else
{
clickedCards[0].Button.BackgroundImage = BackSideImage();
clickedCards[1].Button.BackgroundImage = BackSideImage();
}
clickedCards.Clear();
}
}
Instead of hiding your button, you can make it invisible like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int x = 0; x < 9; x++)
{
var button = new Button
{
Name = "Test-" + x,
Text = "Test-" + x,
Width = 100,
Height = 100
};
button.Click += OnButtonClick;
flowLayoutPanel1.Controls.Add(button);
}
}
private void OnButtonClick(object sender, EventArgs e)
{
//Instead of this...
//((Button)sender).Hide();
//Do this...
var button = ((Button) sender);
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderColor = BackColor;
button.FlatAppearance.MouseOverBackColor = BackColor;
button.FlatAppearance.MouseDownBackColor = BackColor;
button.Text = string.Empty;
button.Enabled = false;
}
}
I created 5 PictureBox "Shapes" and I want them to move to the left automatically when the program is launched. So in the timer1_Tick method, I use "Shapes[i].Left -= 2", it's said that "Shapes" isn't in the actual context, so How can I make the Shapes[i] global from the "CreatePipes" method?
public partial class Form1 : Form
{
int i = 0;
int N = 5;
int yspeed;
int gravity = 2;
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
yspeed = -15;
}
}
private void Form1_Load(object sender, EventArgs e)
{
CreatePipes(1);
}
public void CreatePipes(object Number)
{
PictureBox[] Shapes = new PictureBox[N];
for (i = 0; i < N; i++)
{
Shapes[i] = new PictureBox();
Shapes[i].Name = "ItemNum_" + i.ToString();
Shapes[i].Location = new Point(300 + 120 * i, 250);
Shapes[i].Size = new Size(30, 1000 );
Shapes[i].BackColor = Color.Green;
Shapes[i].Visible = true;
this.Controls.Add(Shapes[i]);
}
}
private void bird_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
for (i = 0; i < N; i++)
{
Shapes[i].Left -= 2; //So the problem is here. Shapes[i] isn't in the actual context. But I don't know to to make it global from CreatePipes
}
yspeed += gravity;
bird.Top += yspeed;
}
}
}
You have to declare PictureBox[] Shapes above CreatePipes function, in Form1 class. Then in CreatePipes func, change PictureBox[] Shapes = new PictureBox[N]; to Shapes = new PictureBox[N];
I'm using a for-loop to add values into an array of PictureBox and binding a click event to each one. I'm looking for a way of getting the data of a PictureBox after clicking on it. Since it's an array, I was thinking of sending the value of the loop counter, which would identify which one was clicked.
My code looks like this:
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
label1.Text = "here I need the value of the picboxes[i] image location";
}
It can seem stupid, but I thought of something like:
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click(i))
and
private void PictureBoxes_Click(object sender, EventArgs e, int i)
In short: when I click in a PictureBox created in a array via code, how do I get its values (inside the click event handler)?
EDIT!
Sorry for finding it only after making this question, but I've found this solution and it may apply to my case, right?
try do do this
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].Name = (i+1).ToString();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
string j = p.Name;
label1.Text = j;
}
You can use the following (anoynomous method) lambda expression
picboxes[i].Click += (sender, eventArguments) => PictureBoxes_Click(sender, eventArguments, i);
Use Tag
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].Tag = (i+1).ToString();
picboxes[i].ImageLocation = #FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
string j = p.tag.tostring();
label1.Text = j;
}
I have a small program that hold 4 button in a 2D Array what I want to do is display its 'X' and 'Y' coordinates of the Array in a message box (when clicked)
I have tried a number of ways some don't work and some work but I cant get it to show the 'X' and 'Y' values
The image below shows what I have so far:
And This is the code i have come up with:
namespace _2DArray
{
public partial class Form1 : Form
{
private Button[,] b;
public Form1()
{
InitializeComponent();
b = new Button[2, 2];
b = new Button[,] { {button1,button2 },
{button3, button4}};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s);
}
}
}
Here is the answer to your question if i read it right. You are trying to get the X and Y coordinates of the button right?
Here is the code for a button click:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(button1.Location.ToString());
}
try assigning some sort of pointer like give name of the button to keep track of it coordinates
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
b[i, j].Click += new System.EventHandler(this.ClickedButton);
b[i, j].Name =i+" "+j;
}
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s.Name);
}
Use this code
private void Form1_Load(object sender, EventArgs e) {
for (int x = 0; x < 2; x++) {
for (int y = 0; x < 2; y++) {
b[x, y].Tag = new Point(x, y);
b[x, y].Click += new System.EventHandler(this.ClickedButton);
}
}
}
private void ClickedButton(object sender, EventArgs e) {
Button s = (Button) sender;
MessageBox.Show("you have clicked button:" + s.Tag.ToString());
}
then clicking on button1 will show the message "you have clicked button:{X = 0, Y = 0}" etc
Tag is a property that each control has, it's description is "User-defined data associated with the object" so you can set it to whatever object you like.
I know this is probably a bit late for the op but hopefully it will help someone else.