I've filled grid with Images in code and I'm tryig to add mouse events to them
public FilesWindow()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Image image = new Image();
image.MouseRightButtonDown += new MouseButtonEventHandler(setPicture);
myGrid.Children.Add(image);
image.Source = new BitmapImage(randomPic());
Grid.SetRow(image, i);
Grid.SetColumn(image, j);
}
}
}
private void setPicture(object sender, MouseButtonEventArgs e)
{
}
but I don't know how to access this specyfic Image (the one I click). Clicking itself (ex. with writing on console) is working but how to change picture in a cell that was clicked?
You can cast sender (which is type object) to Image:
Image image = (Image)sender;
Related
so i i hade 10x10 grid with pics and i want to swap by drag and drop and i actually cant get
the imgages the targeted and the selected in order to make the spaw
i made all from view model code
in the evens the sender is a grid and the e is DragEventArgs
public MainViewModel()
{
GameGrid = new Grid { AllowDrop = true };
GameGrid.DragOver += GameGrid_DragOver;
GameGrid.Drop += GameGrid_Drop;
for (int row = 0; row < 10; row++)
{
GameGrid.RowDefinitions.Add(new RowDefinition());
for (int col = 0; col < 10; col++)
{
if (row == 0)
GameGrid.ColumnDefinitions.Add(new ColumnDefinition());
var image = new Image { Source = logic.GetImage(), CanDrag = true, AllowDrop = true };
Grid.SetRow(image, row);
Grid.SetColumn(image, col);
GameGrid.Children.Add(image);
}
}
}
private void GameGrid_DragOver(object sender, Windows.UI.Xaml.DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Move;
}
private async void GameGrid_Drop(object sender, Windows.UI.Xaml.DragEventArgs e)
{
if (logic.IsValidSwap(selected, selected))
{
}
}
// but i cant get the imeges to send the func is validswap
Instead of assigning in the event to the grid:
GameGrid.DragOver += GameGrid_DragOver;
GameGrid.Drop += GameGrid_Drop;
It should be assigned to each image inside the for loop:
image.Drop += GameGrid_Drop;
image.DragOver += GameGrid_DragOver;
And than the sender will be the image—you can know the col and row and make switches.
I have code that creates a row of 5 pictureboxes at runtime. I have added (I think) the code to add a click event handler to each picturebox as it is created.
int xPos = 95;
for (int index = 0; index < 5; index++)
{
keepImage[index] = new PictureBox();
keepImage[index].Width = 120;
keepImage[index].Height = 41;
keepImage[index].Left = xPos;
keepImage[index].Top = 360;
keepImage[index].Click += new EventHandler(keepImage_Click);
keepImage[index].BackColor = Color.Transparent;
keepImage[index].SizeMode = PictureBoxSizeMode.CenterImage;
this.Controls.Add(keepImage[index]);
xPos += 125;
}
The code works - it creates and displays the pictureboxes. I have been looking on here to find out how to find which one of the pictureboxes is clicked on...
public void keepImage_Click(object sender, EventArgs e)
{
PictureBox index = sender as PictureBox;
// identify which button was clicked and perform necessary actions
Debug.Write(index);
}
This code was taken from a solution found on here, but how do I adapt it for my needs? I have tried but so far, no luck.
At runtime the debug shows System.Windows.Forms.PictureBox, SizeMode: CenterImage but not the actual index.
Thanks for any suggestions.
EDIT
After trying one of the solutions mentioned in the comments, I now get the following error...
You can follow second approach from Get the index of array of picturebox clicked. But there is a typo issue with this answer it should be (sender as PictureBox).
So in your case, you can use Control.Tag property to store index of pictures as:
int xPos = 95;
for (int index = 0; index < 5; index++)
{
//Other codes
keepImage[index].Tag = index; //Set tag from index
this.Controls.Add(keepImage[index]);
xPos += 125;
}
Then click event would be like:
public void keepImage_Click(object sender, EventArgs e)
{
int index = int.Parse((sender as PictureBox).Tag.ToString());
Debug.Write(index);
}
I am making a N*M size SUDOKU game. Every number are on a button.
When the program start all button is empty and I would like if I click to a button it is make a little panel on it with buttons for each number to choose one.
private void adatB_Click(object sender, EventArgs e)
{
Button button = sender as Button;
int[] hely = button.Tag.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
Panel szamok = new Panel
{
Location = MousePosition,
Size = new Size(100, 100)
};
Controls.Add(szamok);
TableLayoutPanel minitabla = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = szorzat,
RowCount = szorzat,
};
for (int i = 0; i < szorzat; i++)
{
minitabla.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
minitabla.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
}
szamok.Controls.Add(minitabla);
Button[,] szamokB = new Button[meret[0], meret[1]];
int d = 1;
for (int i = 0; i < meret[0]; i++)
{
for (int j = 0; j < meret[1]; j++)
{
szamokB[i, j] = new Button();
szamokB[i, j].Tag= hely[0]+","+hely[1];
szamokB[i, j].Text = d.ToString();
szamokB[i, j].Dock = DockStyle.Fill;
szamokB[i, j].Click += szamokB_Click;
minitabla.Controls.Add(szamokB[i, j], i, j);
d++;
}
}
}
private void szamokB_Click(object sender, EventArgs e)
{
Button button = sender as Button;
int[] hely = button.Tag.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
adatB[hely[0], hely[1]].Text = button.Text;
}
The problem with it when I click a button the pane isn't created.
meret[0] variable is the N, meret[1] is M, adatB is the arry of buttons with the positons in tag.
And If I choosed the number how can I close that panel?
First of all, you should calculate the mouseposition correctly.
From MSDN:
Gets the position of the mouse cursor in screen coordinates.
You should use something like this:
Location = new Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y)
You will probably need this, to bring your panel to the front:
Controls.Add(szamok);
szamok.BringToFront();
To close the panel you can store the chooser panel and you can remove it from the controls later, use something like this:
public partial class Form1 : Form
{
private Panel myPanel = null;
private void adatB_Click(object sender, EventArgs e)
{
...
Panel szamok = new Panel
{
Location = new Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y),
Size = new Size(100, 100)
};
if (this.myPanel != null)
{
this.Controls.Remove(this.myPanel);
}
this.myPanel = szamok;
Controls.Add(szamok);
szamok.BringToFront();
...
}
private void szamokB_Click(object sender, EventArgs e)
{
if (this.myPanel != null)
{
this.Controls.Remove(this.myPanel);
this.myPanel = null;
}
...
}
}
I have a stackpanel that I have dynamically added buttons to, based on a 2D array defined elsewhere. So essentially it builds a grid of buttons, but added as children of the stackpanel. What I need to be able to do, is specify a child button based on column and row number. Here's the Stackpanel.
<StackPanel Name="myArea" HorizontalAlignment="Center" VerticalAlignment="Center"/>
And here's some of the code in question.
Grid values = new Grid();
values.Width = 320;
values.Height = 240;
values.HorizontalAlignment = HorizontalAlignment.Center;
values.VerticalAlignment = VerticalAlignment.Center;
int x = valueBoard.GetLength(0);
int y = valueBoard.GetLength(1);
for (int i = 0; i < x; i++)
{
ColumnDefinition col = new ColumnDefinition();
values.ColumnDefinitions.Add(col);
}
for (int j = 0; j < y; j++)
{
RowDefinition row = new RowDefinition();
values.RowDefinitions.Add(row);
}
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++)
{
Button button = new Button();
button.Content = "";
button.Click += ButtonClick;
button.MouseRightButtonUp += RightClick;
Grid.SetColumn(button, i);
Grid.SetRow(button, j);
values.Children.Add(button);
}
myArea.Children.Add(values);
So now, on click of a button, I want to hide all adjacent buttons in the grid. Here's my click event:
private void ButtonClick(object sender, RoutedEventArgs e)
{
int col = Grid.GetColumn((Button)sender);
int row = Grid.GetRow((Button)sender);
Button source = e.Source as Button;
source.Visibility = Visibility.Hidden;
}
How would I, in ButtonClick, hide the neighbor buttons with the col and row values? Is there some sort of getter or setter than I can feed those values into and edit the neighbor's Visibility property?
try the following:
private void ButtonClick(object sender, RoutedEventArgs e)
{
int col = Grid.GetColumn((Button)sender);
int row = Grid.GetRow((Button)sender);
Button source = e.Source as Button;
source.Visibility = Visibility.Hidden;
Grid pGrid = source.Parent as Grid;
if (pGrid == null) throw new Exception("Can't find parent grid.");
for (int i = 0; i < pGrid.Children.Count; i++) {
Button childButton = pGrid.Children[i] as Button;
if(childButton == null) continue;
int childCol = Grid.GetColumn(childButton);
int childRow= Grid.GetRow(childButton);
if(Math.Abs(childCol - col) < 2 && Math.Abs(childRow - row) < 2) childButton.Visibility = Visibility.Hidden;
}
}
this iterates through your children and if it is type of Button it will check, if the row and column is next to your Source and will hide it.
This question already has answers here:
Pass extra parameters to an event handler?
(10 answers)
Closed 2 years ago.
I use the loop for a two dimensions array of buttons. I don't know how to know exactly which buttons in array were clicked or not
Here are my code:
for (int i = 0; i < 100 ; i++)
{
for (int j=0 ; j< 100; i++)
{
arrButton[i, j] = new Button();
arrButton[i,j].Size = new Size(size1button, size1button);
arrButton[i,j].Location = new Point(j*size1button, i*size1button);
arrButton.Click += new EventHandler(arrButton_Click);
}
}
Can I use parameters i, j for mouse click event like:
private void arrButton_Click(object sender, EventArgs e, int i, int j)
{
//my idea : add i, j to another int[,] array to keep track of buttons which were clicked
}
If this exits, how to write it correctly? Or can you recommend or method to know exactly where the button was clicked in array ?
Try this
public class Indeces
{
public int IndexI { get; set; }
public int IndexJ { get; set; }
}
Now in loop set Tag
for (int i = 0; i < 100 ; i++)
{
for (int j=0 ; j< 100; i++)
{
arrButton[i, j] = new Button();
arrButton[i,j].Size = new Size(size1button, size1button);
arrButton[i,j].Location = new Point(j*size1button, i*size1button);
arrButton.Click += new EventHandler(arrButton_Click);
arrButton.Tag = new Indeces {IndexI = i,IndexJ = j};
}
}
Get values from Tag here as
private void arrButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
var indeces = (Indeces) button.Tag;//get indeces here
var i = indeces.IndexI;
var j = indeces.IndexJ;
}
You cannot change the EventHandler signature to include your i and j.
However, you can get that information from what is already passed to the arrButton_Click method. Since you set the location of each button as new Point(j*size1button, i*size1button), you can get each i and j component back by dividing the location of your button by size1button.
To get that location, you can use the sender, which is your Button (a cast is necessary):
private void arrButton_Click(object sender, EventArgs e)
{
Button btnClicked = (Button) sender;
int i = btnClicked.Location.Y / size1button;
int j = btnClicked.Location.X / size1button;
}
Also, the code you're currently using to create the buttons have a couple errors.
First, you're never incrementing j; the second loop does i++.
Second, if you want your buttons to appear, you have to add them to your Form's Controls.
Finally, I don't think you can have 10 000 active buttons on your form, try a lower number, like 25.
So the corrected code would look like:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arrButton[i, j] = new Button();
arrButton[i, j].Size = new Size(size1button, size1button);
arrButton[i, j].Location = new Point(j*size1button, i*size1button);
arrButton[i, j].Click += arrButton_Click;
Controls.Add(arrButton[i,j]);
}
}
You can also notice that I removed your declaration of new EventHandler, which was redundant.
If you are only interested in Location then whats wrong with this? -
private void arrButton_Click(object sender, EventArgs e)
{
var button = sender as Button;
//now use button.Location
}
But if you want more data other than just location. Here is an example
Use a custom button class -
public class CustomButton<T> : Button {
public T Data{get;set;}
public CustomButton(T data){
this.Data = data; //i didn't compile it, so data type might mismatch.
}
}
Then use this button class -
for (int i = 0; i < 100 ; i++)
{
for (int j=0 ; j< 100; i++)
{
arrButton[i, j] = new CustomButton<T> (...some data);
arrButton[i,j].Size = new Size(size1button, size1button);
arrButton[i,j].Location = new Point(j*size1button, i*size1button);
arrButton.Click += new EventHandler(arrButton_Click);
}
}
In the event handler Cast to CustomButton and voila, there is your location -
private void arrButton_Click(object sender, EventArgs e)
{
var cButton = sender as CustomButton<T>;
// cButton.Datais your point. Have fun
}
BTW, you cannot change the default signature of event handlers, if you want you have to implement your own event/delegate.