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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Change currently image?

Change currently image?

Scheduled Pinned Locked Moved C#
question
8 Posts 3 Posters 1 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.
  • D Offline
    D Offline
    dennis_max27
    wrote on last edited by
    #1

    First i had this:

    private void pbEdit_OnMouseEnter(object sender, EventArgs e)
    {
    pbTextEdit.Image = Image.FromFile("C:/data/edit_color.gif");

    } Now I wanna use 1 function for different pictureboxes: My code i was thinking of but didn't work :( this.Image = Image.FromFile("C:/data/edit_color.gif"); Dennis,

    D 1 Reply Last reply
    0
    • D dennis_max27

      First i had this:

      private void pbEdit_OnMouseEnter(object sender, EventArgs e)
      {
      pbTextEdit.Image = Image.FromFile("C:/data/edit_color.gif");

      } Now I wanna use 1 function for different pictureboxes: My code i was thinking of but didn't work :( this.Image = Image.FromFile("C:/data/edit_color.gif"); Dennis,

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      If I understood your question correct, you need to have a single method to change images of multiple picture boxes. And that method should be the MouseEnter event handler. Right? You can have single event handler for all the picture boxes. This way:

      picBox1.MouseEnter += new EventHandler(EventHandlerName);
      picBox2.MouseEnter += new EventHandler(EventHandlerName);

      Same way you can do this fro others as well. Now, in order to change the image, use the following:

      PictureBox picBox = sender as PictureBox;
      picBox.Image = Image.FromFile("Set file path here");

      In case all the picture boxes need to show the different images, you can keep the image file path in the Tag property of the PictureBox and then use it like this:

      picBox.Image = Image.FromFile(picBox.Tag.ToString());

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      D 1 Reply Last reply
      0
      • D dan sh

        If I understood your question correct, you need to have a single method to change images of multiple picture boxes. And that method should be the MouseEnter event handler. Right? You can have single event handler for all the picture boxes. This way:

        picBox1.MouseEnter += new EventHandler(EventHandlerName);
        picBox2.MouseEnter += new EventHandler(EventHandlerName);

        Same way you can do this fro others as well. Now, in order to change the image, use the following:

        PictureBox picBox = sender as PictureBox;
        picBox.Image = Image.FromFile("Set file path here");

        In case all the picture boxes need to show the different images, you can keep the image file path in the Tag property of the PictureBox and then use it like this:

        picBox.Image = Image.FromFile(picBox.Tag.ToString());

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        D Offline
        D Offline
        dennis_max27
        wrote on last edited by
        #3

        Im having 10 picturesboxes that im using as button. When im mouse over over now it is changed from image. and when i click on that button its openeing in every 10 buttons a different window. Have that now :) But all 10 picatureboxes must have the same mousenter and mouseleave event. SO i didnt't want to make 10 the same mouseenter and mouseleave events. Is it possible in 1?

        D 1 Reply Last reply
        0
        • D dennis_max27

          Im having 10 picturesboxes that im using as button. When im mouse over over now it is changed from image. and when i click on that button its openeing in every 10 buttons a different window. Have that now :) But all 10 picatureboxes must have the same mousenter and mouseleave event. SO i didnt't want to make 10 the same mouseenter and mouseleave events. Is it possible in 1?

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Offcourse it is. Did you saw the first code block I have written?

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          D 1 Reply Last reply
          0
          • D dan sh

            Offcourse it is. Did you saw the first code block I have written?

            50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

            D Offline
            D Offline
            dennis_max27
            wrote on last edited by
            #5

            I've made this already: picBox1.MouseEnter += new EventHandler(EventHandlerName); picBox2.MouseEnter += new EventHandler(EventHandlerName); But I must declare in the eventhandler that it is changed the only the image the one where the mouse is hovering then, not all images. 1 eventhandler where he only works with the image where it's mouseentering and mouse leaving then. It's difficult to explain for me.

            D 1 Reply Last reply
            0
            • D dennis_max27

              I've made this already: picBox1.MouseEnter += new EventHandler(EventHandlerName); picBox2.MouseEnter += new EventHandler(EventHandlerName); But I must declare in the eventhandler that it is changed the only the image the one where the mouse is hovering then, not all images. 1 eventhandler where he only works with the image where it's mouseentering and mouse leaving then. It's difficult to explain for me.

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              Did you read the second and the third code blocks? The object sender will be the picturebox which fires the event. This means, that if you do a mouse enter on picturebox1, the sender will be picturebox1. If you do mouse enter on picturebox2, sender will be picturebox2. Now, if you cast sender to a picturebox, and change its image, it will change image of only the concerned picturebox.

              50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

              L 1 Reply Last reply
              0
              • D dan sh

                Did you read the second and the third code blocks? The object sender will be the picturebox which fires the event. This means, that if you do a mouse enter on picturebox1, the sender will be picturebox1. If you do mouse enter on picturebox2, sender will be picturebox2. Now, if you cast sender to a picturebox, and change its image, it will change image of only the concerned picturebox.

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                d@nish wrote:

                Did you read ... ?

                I like rhetorical questions. :laugh:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                D 1 Reply Last reply
                0
                • L Luc Pattyn

                  d@nish wrote:

                  Did you read ... ?

                  I like rhetorical questions. :laugh:

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  D Offline
                  D Offline
                  dennis_max27
                  wrote on last edited by
                  #8

                  Fixed the problem now. Thaks for the reply's!!

                  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