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. Properties does not change in custom MouseEnter event incomplete implementation.

Properties does not change in custom MouseEnter event incomplete implementation.

Scheduled Pinned Locked Moved C#
graphicscsharplinqquestion
27 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.
  • _ _Q12_

    Thank you mister OriginalGriff, Now I made the suggested rectifications:

    public class Human : UserControl
    {
        //Name      is from Usercontrol inheritance, 
        //Location  is from Usercontrol inheritance, 
        public void hPaint(Graphics g)
        {
            g.DrawString(Name, new Font("Arial", 9), new SolidBrush(Color.Black), Location.X - 5, Location.Y - 15);
            g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(Location.X, Location.Y, 20, 20));
        }
    
        public override event EventHandler MouseEnter; //???
        //Probably i must override the mouse event somehow...
    }
    
    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #4

    Just add a handler, and add the instance to the form's Controls collection.

    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    _ 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      Just add a handler, and add the instance to the form's Controls collection.

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      _ Offline
      _ Offline
      _Q12_
      wrote on last edited by
      #5

      Make an example for me, Please. I think in Form1 i must do this? >> Controls.Add(female);

      OriginalGriffO 1 Reply Last reply
      0
      • _ _Q12_

        Make an example for me, Please. I think in Form1 i must do this? >> Controls.Add(female);

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #6

        Try this: remove the line

        public override event EventHandler MouseEnter;

        And drag your Human class from the toolbox onto your form. Try it!

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        _ 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Try this: remove the line

          public override event EventHandler MouseEnter;

          And drag your Human class from the toolbox onto your form. Try it!

          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          _ Offline
          _ Offline
          _Q12_
          wrote on last edited by
          #7

          i've made a red square there in the paint event. It is not showing at all. Only the text from the same paint event is visible. When i drag the control from toolbox into my form, a very large grey square appear, but not my red square and its text. I want the area to be as large as that red square (20,20). When I move my mouse over this red square area, I want the text to change. All this graphics must "float" over other controls. In this way, I can intersect many other controls like these between them.

          OriginalGriffO 2 Replies Last reply
          0
          • _ _Q12_

            i've made a red square there in the paint event. It is not showing at all. Only the text from the same paint event is visible. When i drag the control from toolbox into my form, a very large grey square appear, but not my red square and its text. I want the area to be as large as that red square (20,20). When I move my mouse over this red square area, I want the text to change. All this graphics must "float" over other controls. In this way, I can intersect many other controls like these between them.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            Did you add the Paint event handler to your UserControl? Check in the designer!

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            _ 1 Reply Last reply
            0
            • _ _Q12_

              i've made a red square there in the paint event. It is not showing at all. Only the text from the same paint event is visible. When i drag the control from toolbox into my form, a very large grey square appear, but not my red square and its text. I want the area to be as large as that red square (20,20). When I move my mouse over this red square area, I want the text to change. All this graphics must "float" over other controls. In this way, I can intersect many other controls like these between them.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #9

              I tell you what: stop what you are doing. Create a new solution, WinForms, call it "HumanDemo" Add a UserControl to the project, call it Human. In the designer, add event handlers to the Human control: MouseEnter, MouseLeave, Paint. In the Human code, add a field and a property:

                  private bool isIn = false;
                  public string Name { get; set; }
              

              In the Human Constructor, add a line, so it looks like this:

                  public Human()
                      {
                      InitializeComponent();
                      Name = "A name";
                      }
              

              Then edit the three handlers so they look like this:

                  private void Human\_MouseEnter(object sender, EventArgs e)
                      {
                      isIn = true;
                      Invalidate();
                      }
              
                  private void Human\_MouseLeave(object sender, EventArgs e)
                      {
                      isIn = false;
                      Invalidate();
                      }
              
                  private void Human\_Paint(object sender, PaintEventArgs e)
                      {
                      Graphics g = e.Graphics;
                      Rectangle rect = e.ClipRectangle;
                      rect.Inflate(-1, -1);
                      g.DrawRectangle(Pens.Red, rect);
                      if (isIn)
                          {
                          g.DrawString(Name, Font, Brushes.Black, new Point(10, 10));
                          }
                      }
              

              Compile your project. Go to the main form in teh designer, and drag a Human control from your toolbox and drop it on the form. Run your application. Move the mouse into and out of the red rectangle. That's how simple it is!

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              _ 2 Replies Last reply
              0
              • OriginalGriffO OriginalGriff

                Did you add the Paint event handler to your UserControl? Check in the designer!

                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                _ Offline
                _ Offline
                _Q12_
                wrote on last edited by
                #10

                alright, now is showing in Form1 after I add this class object to Controls. I also get rid of the custom paint event i made earlier. If its not good what i did here, please correct me.

                public class Human : UserControl
                {
                public Human()
                {
                Paint += new PaintEventHandler(Human_Paint);
                }

                    void Human\_Paint(object sender, PaintEventArgs e)
                    {
                        e.Graphics.DrawString(Name, new Font("Arial", 9), new SolidBrush(Color.Black), Location.X - 5, Location.Y - 15);
                        e.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(Location.X, Location.Y, 20, 20));
                    }
                }
                
                OriginalGriffO 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  I tell you what: stop what you are doing. Create a new solution, WinForms, call it "HumanDemo" Add a UserControl to the project, call it Human. In the designer, add event handlers to the Human control: MouseEnter, MouseLeave, Paint. In the Human code, add a field and a property:

                      private bool isIn = false;
                      public string Name { get; set; }
                  

                  In the Human Constructor, add a line, so it looks like this:

                      public Human()
                          {
                          InitializeComponent();
                          Name = "A name";
                          }
                  

                  Then edit the three handlers so they look like this:

                      private void Human\_MouseEnter(object sender, EventArgs e)
                          {
                          isIn = true;
                          Invalidate();
                          }
                  
                      private void Human\_MouseLeave(object sender, EventArgs e)
                          {
                          isIn = false;
                          Invalidate();
                          }
                  
                      private void Human\_Paint(object sender, PaintEventArgs e)
                          {
                          Graphics g = e.Graphics;
                          Rectangle rect = e.ClipRectangle;
                          rect.Inflate(-1, -1);
                          g.DrawRectangle(Pens.Red, rect);
                          if (isIn)
                              {
                              g.DrawString(Name, Font, Brushes.Black, new Point(10, 10));
                              }
                          }
                  

                  Compile your project. Go to the main form in teh designer, and drag a Human control from your toolbox and drop it on the form. Run your application. Move the mouse into and out of the red rectangle. That's how simple it is!

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  _ Offline
                  _ Offline
                  _Q12_
                  wrote on last edited by
                  #11

                  i made it work now.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • _ _Q12_

                    i made it work now.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #12

                    See how easy it is? :-D

                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • _ _Q12_

                      alright, now is showing in Form1 after I add this class object to Controls. I also get rid of the custom paint event i made earlier. If its not good what i did here, please correct me.

                      public class Human : UserControl
                      {
                      public Human()
                      {
                      Paint += new PaintEventHandler(Human_Paint);
                      }

                          void Human\_Paint(object sender, PaintEventArgs e)
                          {
                              e.Graphics.DrawString(Name, new Font("Arial", 9), new SolidBrush(Color.Black), Location.X - 5, Location.Y - 15);
                              e.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(Location.X, Location.Y, 20, 20));
                          }
                      }
                      
                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #13

                      Just as an aside: dont; create Font and Brush (or any graphics items) willy nilly - they all use something called Handles which are in short supply. If you create a graphics item, it needs a fresh handle, so unless you specifically Dispose of it when you are finished, you will crash your app with an "out of memory" exception pretty quickly because the whole system will run out of Handles and become unstable. It's a very good idea to create a static class level Font and Brush item, and use them, or use a using block around the item construction so that it is automatically Disposed when it goes out of scope.

                      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        I tell you what: stop what you are doing. Create a new solution, WinForms, call it "HumanDemo" Add a UserControl to the project, call it Human. In the designer, add event handlers to the Human control: MouseEnter, MouseLeave, Paint. In the Human code, add a field and a property:

                            private bool isIn = false;
                            public string Name { get; set; }
                        

                        In the Human Constructor, add a line, so it looks like this:

                            public Human()
                                {
                                InitializeComponent();
                                Name = "A name";
                                }
                        

                        Then edit the three handlers so they look like this:

                            private void Human\_MouseEnter(object sender, EventArgs e)
                                {
                                isIn = true;
                                Invalidate();
                                }
                        
                            private void Human\_MouseLeave(object sender, EventArgs e)
                                {
                                isIn = false;
                                Invalidate();
                                }
                        
                            private void Human\_Paint(object sender, PaintEventArgs e)
                                {
                                Graphics g = e.Graphics;
                                Rectangle rect = e.ClipRectangle;
                                rect.Inflate(-1, -1);
                                g.DrawRectangle(Pens.Red, rect);
                                if (isIn)
                                    {
                                    g.DrawString(Name, Font, Brushes.Black, new Point(10, 10));
                                    }
                                }
                        

                        Compile your project. Go to the main form in teh designer, and drag a Human control from your toolbox and drop it on the form. Run your application. Move the mouse into and out of the red rectangle. That's how simple it is!

                        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                        _ Offline
                        _ Offline
                        _Q12_
                        wrote on last edited by
                        #14

                        Your code is not good at all. Look here in my screenshot: http://i64.tinypic.com/29dijx3.jpg[^] The first red square must be visible under the second square control ! With my code, I can make it show one beneath the other one.

                        OriginalGriffO 1 Reply Last reply
                        0
                        • _ _Q12_

                          Your code is not good at all. Look here in my screenshot: http://i64.tinypic.com/29dijx3.jpg[^] The first red square must be visible under the second square control ! With my code, I can make it show one beneath the other one.

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #15

                          Two things: 1) no image. 2) You didn't think it might have something to do with the order in which you added the controls? Google "Z-order" and start thinking about what you are doing instead of guessing ... :sigh:

                          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          _ 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            Two things: 1) no image. 2) You didn't think it might have something to do with the order in which you added the controls? Google "Z-order" and start thinking about what you are doing instead of guessing ... :sigh:

                            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                            _ Offline
                            _ Offline
                            _Q12_
                            wrote on last edited by
                            #16

                            now? https://i106.fastpic.ru/thumb/2018/1222/4e/7ac3a88bb3620fe04738211836c8234e.jpeg[^]

                            OriginalGriffO 1 Reply Last reply
                            0
                            • _ _Q12_

                              now? https://i106.fastpic.ru/thumb/2018/1222/4e/7ac3a88bb3620fe04738211836c8234e.jpeg[^]

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #17

                              And did you google "Z-order"?

                              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              _ 1 Reply Last reply
                              0
                              • _ _Q12_

                                i know what z-order is - its not that. I intersect the 2 squares, not putting one on top of the other. Both are z-order dependent but with diferent meanings... you will get my point very easy from my screenshot.

                                _ Offline
                                _ Offline
                                _Q12_
                                wrote on last edited by
                                #18

                                Imgur: The magic of the Internet[^]

                                OriginalGriffO 1 Reply Last reply
                                0
                                • OriginalGriffO OriginalGriff

                                  And did you google "Z-order"?

                                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                  _ Offline
                                  _ Offline
                                  _Q12_
                                  wrote on last edited by
                                  #19

                                  i know what z-order is - its not that. I intersect the 2 squares, not putting one on top of the other. Both are z-order dependent but with diferent meanings... you will get my point very easy from my screenshot.

                                  _ 1 Reply Last reply
                                  0
                                  • _ _Q12_

                                    Imgur: The magic of the Internet[^]

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #20

                                    Right - you can't do that with controls at all; you have to use Paint and draw yyour rectancgles your self. Which means that you have to do the "entry" and "exit" code yourself, by handling MouseMove in your Parent container, and manually checking which of your rectangles the mouse is currently in:you cannot use MouseEnter and MouseLeave on your own rectangles because - unless they are Controls and in which case transparent backgrounds don't work - only Controls can react to mouse events. Remove UserControl, and go back to drawing them yourself - but you will have to work out where the mouse is yourself, the system will not help you!

                                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    _ 1 Reply Last reply
                                    0
                                    • OriginalGriffO OriginalGriff

                                      Right - you can't do that with controls at all; you have to use Paint and draw yyour rectancgles your self. Which means that you have to do the "entry" and "exit" code yourself, by handling MouseMove in your Parent container, and manually checking which of your rectangles the mouse is currently in:you cannot use MouseEnter and MouseLeave on your own rectangles because - unless they are Controls and in which case transparent backgrounds don't work - only Controls can react to mouse events. Remove UserControl, and go back to drawing them yourself - but you will have to work out where the mouse is yourself, the system will not help you!

                                      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                      _ Offline
                                      _ Offline
                                      _Q12_
                                      wrote on last edited by
                                      #21

                                      Excellent answer so far, mister OriginalGriff: "you will have to work out where the mouse is yourself, the system will not help you". Can you help me making such "manual" class? I am aware I am asking a lot...

                                      OriginalGriffO 1 Reply Last reply
                                      0
                                      • _ _Q12_

                                        Excellent answer so far, mister OriginalGriff: "you will have to work out where the mouse is yourself, the system will not help you". Can you help me making such "manual" class? I am aware I am asking a lot...

                                        OriginalGriffO Offline
                                        OriginalGriffO Offline
                                        OriginalGriff
                                        wrote on last edited by
                                        #22

                                        I'd do two things: Add a Paint method to the class which takes a single parameter: the Graphics context from the Containing classes Paint event. And I'd add a Contains method returning a bool, which again takes a single parameter: a Point which it checks is inside it's "boundaries" (using the Location and Size information it contains). This could change the Name to be drawn, or that could be done externally, it depends what - exactly - you are trying to do, and how it all fits together as a complete system. The containers MouseMove calls each Human instance's Contains method so they "know" if the mouse in over them or not. (The containers MouseExit event handler can reset them all) This can call Invalidate to force a redraw as needed. The Containers Paint event also calls each Human instance's Paint method so it draws itself. Make sense?

                                        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                        _ 1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          I'd do two things: Add a Paint method to the class which takes a single parameter: the Graphics context from the Containing classes Paint event. And I'd add a Contains method returning a bool, which again takes a single parameter: a Point which it checks is inside it's "boundaries" (using the Location and Size information it contains). This could change the Name to be drawn, or that could be done externally, it depends what - exactly - you are trying to do, and how it all fits together as a complete system. The containers MouseMove calls each Human instance's Contains method so they "know" if the mouse in over them or not. (The containers MouseExit event handler can reset them all) This can call Invalidate to force a redraw as needed. The Containers Paint event also calls each Human instance's Paint method so it draws itself. Make sense?

                                          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                          _ Offline
                                          _ Offline
                                          _Q12_
                                          wrote on last edited by
                                          #23

                                          yes, it makes sense...because I was thinking in the same direction too. I will go with your idea. I am building it now.

                                          _ 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