PictureBox Control Arrays
-
Hi all i've created a Form (Form1.cs) and Control Array handler (PictureBoxArray.cs) which all works fine but my problem is I want to be able to access the *clicked on* picturebox but it doesn't seem to be working, well I cant make it work is a bit more truthful. I'm sure its fairly simple but I can't work out a way of doing it and now I've been looking so long I'm kind of blinded by it! I have this in my PictureBoxArray.cs
private void pictureBox\_MouseDown(object sender, MouseEventArgs e) { System.Windows.Forms.MessageBox.Show("You have clicked button " + ((System.Windows.Forms.PictureBox) sender).Tag.ToString()); }
but but from here I can't do things like change the background colour, if I put a button into the main form (Form1) and do a :
public void button2\_Click(object sender, System.EventArgs e) { pictureBoxArray\[0\].BackColor = Color.Red; }
then it works fine but I want to be able to change the colour of the one I clicked on so I need to know which one it is (does that make sense ?) I folowed the msdn guide on creating the control array here http://msdn.microsoft.com/en-us/library/aa289500(VS.71).aspx[^] I sure it is probably fairly simple but like i said I've just lost it now ! thanks in advance for any help tim
-
Hi all i've created a Form (Form1.cs) and Control Array handler (PictureBoxArray.cs) which all works fine but my problem is I want to be able to access the *clicked on* picturebox but it doesn't seem to be working, well I cant make it work is a bit more truthful. I'm sure its fairly simple but I can't work out a way of doing it and now I've been looking so long I'm kind of blinded by it! I have this in my PictureBoxArray.cs
private void pictureBox\_MouseDown(object sender, MouseEventArgs e) { System.Windows.Forms.MessageBox.Show("You have clicked button " + ((System.Windows.Forms.PictureBox) sender).Tag.ToString()); }
but but from here I can't do things like change the background colour, if I put a button into the main form (Form1) and do a :
public void button2\_Click(object sender, System.EventArgs e) { pictureBoxArray\[0\].BackColor = Color.Red; }
then it works fine but I want to be able to change the colour of the one I clicked on so I need to know which one it is (does that make sense ?) I folowed the msdn guide on creating the control array here http://msdn.microsoft.com/en-us/library/aa289500(VS.71).aspx[^] I sure it is probably fairly simple but like i said I've just lost it now ! thanks in advance for any help tim
So you want to change the color on the picture box you clicked on last, is this correct? If yes, then you could for example store the last selected picture box in a variable:
PictureBox lastSelectedPictureBox = null;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
lastSelectedPictureBox = sender as PictureBox;
}and in your button click handler:
public void button2_Click(object sender, System.EventArgs e)
{
lastSelectedPictureBox.BackColor = Color.Red;
}You could also set the "selected" property of the last selected
PictureBox
into theTag
property, if it's not already used for other purposes. Btu then you'd need to loop over the entire array to get the last selected picture box, so I prefer the method above. regards -
So you want to change the color on the picture box you clicked on last, is this correct? If yes, then you could for example store the last selected picture box in a variable:
PictureBox lastSelectedPictureBox = null;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
lastSelectedPictureBox = sender as PictureBox;
}and in your button click handler:
public void button2_Click(object sender, System.EventArgs e)
{
lastSelectedPictureBox.BackColor = Color.Red;
}You could also set the "selected" property of the last selected
PictureBox
into theTag
property, if it's not already used for other purposes. Btu then you'd need to loop over the entire array to get the last selected picture box, so I prefer the method above. regardshi greeg thanks for that, that worked out just fine, what I actually wanted to do was control from within the PictureBoxArray.cs and not from the button_Click, but I just did a little method in there and it ow swaps the colours depending on which one you click on which is perfect for me. thank you again tim