Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. PictureBox Control Arrays

PictureBox Control Arrays

Scheduled Pinned Locked Moved C#
helpvisual-studiocomdata-structurestutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tim_gunning
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • T tim_gunning

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 the Tag 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

      T 1 Reply Last reply
      0
      • L Lost User

        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 the Tag 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

        T Offline
        T Offline
        tim_gunning
        wrote on last edited by
        #3

        hi 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups