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. Request help for 2 newbie questions

Request help for 2 newbie questions

Scheduled Pinned Locked Moved C#
csharpc++questionalgorithmshelp
18 Posts 5 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.
  • M Mazdak

    bouli wrote: I have inserted a label and? how can I link it to my new control? What do you mean exactly? You mean you add that label to your UserControl? It is now in your .cs file as a private member. You can use it like this there:

    lbale1.Text = "Hello";

    User the search textbox at the top of this page to find articles in this site about UserControl for motr information. Mazy No sig. available now.

    B Offline
    B Offline
    bouli
    wrote on last edited by
    #8

    I want to use my new created control... this control graphically shows the content of an array ;)

    M 1 Reply Last reply
    0
    • B bouli

      I want to use my new created control... this control graphically shows the content of an array ;)

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #9

      bouli wrote: I want to use my new created control Right click on your ToolBox and select 'Customize ToolBox' option. From .NET Component tab, with Browse button , select assembly of your control and click OK. It is now added to your toolbox and you can drag and drop it on your form. Mazy No sig. available now.

      B 1 Reply Last reply
      0
      • M Mazdak

        bouli wrote: What I wanna do is to write my own control that will get some methods. these methods will control the drawing

        public class myControl : System.Windows.Forms.UserControl
        {
        public myControl()
        {
        }
        ....

        private void Control_Paint(object sender,PaintEventHandler e)
        {
        //do drawing
        }
        }

        Mazy No sig. available now.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #10

        For one thing, you didn't hook-up your Paint event handlers. Remember, he's a n00b. Also, never use events in a derived class - it's too slow and doesn't provide the control you might need (such as NOT calling the base class's OnPaint handler). Instead, always override the related method for an event when deriving from a class:

        public class MyControl : UserControl
        {
        public MyControl()
        {
        }
        protected override void OnPaint(PaintEventArgs e)
        {
        base.OnPaint(e); // Allows base class to paint and raise the Paint event.
        // Draw
        }
        }

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        M B T 4 Replies Last reply
        0
        • H Heath Stewart

          For one thing, you didn't hook-up your Paint event handlers. Remember, he's a n00b. Also, never use events in a derived class - it's too slow and doesn't provide the control you might need (such as NOT calling the base class's OnPaint handler). Instead, always override the related method for an event when deriving from a class:

          public class MyControl : UserControl
          {
          public MyControl()
          {
          }
          protected override void OnPaint(PaintEventArgs e)
          {
          base.OnPaint(e); // Allows base class to paint and raise the Paint event.
          // Draw
          }
          }

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          M Offline
          M Offline
          Mazdak
          wrote on last edited by
          #11

          Heath Stewart wrote: protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Allows base class to paint and raise the Paint event. // Draw } Yah, Thanks for correction. :) Mazy No sig. available now.

          1 Reply Last reply
          0
          • H Heath Stewart

            For one thing, you didn't hook-up your Paint event handlers. Remember, he's a n00b. Also, never use events in a derived class - it's too slow and doesn't provide the control you might need (such as NOT calling the base class's OnPaint handler). Instead, always override the related method for an event when deriving from a class:

            public class MyControl : UserControl
            {
            public MyControl()
            {
            }
            protected override void OnPaint(PaintEventArgs e)
            {
            base.OnPaint(e); // Allows base class to paint and raise the Paint event.
            // Draw
            }
            }

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            B Offline
            B Offline
            bouli
            wrote on last edited by
            #12

            Ok, it works!! Thanks :D

            1 Reply Last reply
            0
            • M Mazdak

              bouli wrote: I want to use my new created control Right click on your ToolBox and select 'Customize ToolBox' option. From .NET Component tab, with Browse button , select assembly of your control and click OK. It is now added to your toolbox and you can drag and drop it on your form. Mazy No sig. available now.

              B Offline
              B Offline
              bouli
              wrote on last edited by
              #13

              ok I understood how to use and draw my own controls in C# :D It's very different from MFC...

              1 Reply Last reply
              0
              • H Heath Stewart

                For one thing, you didn't hook-up your Paint event handlers. Remember, he's a n00b. Also, never use events in a derived class - it's too slow and doesn't provide the control you might need (such as NOT calling the base class's OnPaint handler). Instead, always override the related method for an event when deriving from a class:

                public class MyControl : UserControl
                {
                public MyControl()
                {
                }
                protected override void OnPaint(PaintEventArgs e)
                {
                base.OnPaint(e); // Allows base class to paint and raise the Paint event.
                // Draw
                }
                }

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                B Offline
                B Offline
                bouli
                wrote on last edited by
                #14

                Ok, it works that way... it's almost like MFC... just override the event...;P

                H 1 Reply Last reply
                0
                • B bouli

                  Ok, it works that way... it's almost like MFC... just override the event...;P

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #15

                  Point of clarification: OnPaint is not an event. It's a method that, in the defining class, raises the Paint event before or after optionally performing some action (like any default painting). When you override this, you pre-empt the event being raised (which is why you call base.OnPaint) and actually override all the functionality of that method since it's virtual. If you don't call base.OnPaint, any painting that the base class, or its base class, etc., need to do won't be performed. For more information about events in delegates in .NET, see Handling and Raising Events[^] in the MSDN Online Library.

                  -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                  B 1 Reply Last reply
                  0
                  • H Heath Stewart

                    Point of clarification: OnPaint is not an event. It's a method that, in the defining class, raises the Paint event before or after optionally performing some action (like any default painting). When you override this, you pre-empt the event being raised (which is why you call base.OnPaint) and actually override all the functionality of that method since it's virtual. If you don't call base.OnPaint, any painting that the base class, or its base class, etc., need to do won't be performed. For more information about events in delegates in .NET, see Handling and Raising Events[^] in the MSDN Online Library.

                    -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                    B Offline
                    B Offline
                    bouli
                    wrote on last edited by
                    #16

                    Ok, thanks for the explainations :) Thanks :)

                    1 Reply Last reply
                    0
                    • H Heath Stewart

                      For one thing, you didn't hook-up your Paint event handlers. Remember, he's a n00b. Also, never use events in a derived class - it's too slow and doesn't provide the control you might need (such as NOT calling the base class's OnPaint handler). Instead, always override the related method for an event when deriving from a class:

                      public class MyControl : UserControl
                      {
                      public MyControl()
                      {
                      }
                      protected override void OnPaint(PaintEventArgs e)
                      {
                      base.OnPaint(e); // Allows base class to paint and raise the Paint event.
                      // Draw
                      }
                      }

                      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                      T Offline
                      T Offline
                      TuringTest1
                      wrote on last edited by
                      #17

                      Heath, why are events in a derived class too slow? First, i'm assuming the fact it's derived is a nonissue, your point is just that OnPaint is exposed directly for that purpose-- let me know if i misunderstand this. But why would events be slow, aren't they just callbacks? TIA. ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!

                      H 1 Reply Last reply
                      0
                      • T TuringTest1

                        Heath, why are events in a derived class too slow? First, i'm assuming the fact it's derived is a nonissue, your point is just that OnPaint is exposed directly for that purpose-- let me know if i misunderstand this. But why would events be slow, aren't they just callbacks? TIA. ________________________________________ Gosh, it would be awful pleas'n, to reason out the reason, for things I can't explain. Then perhaps I'd deserve ya, and be even worthy of ya.. if I only had a brain!

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #18

                        Consider this: when you override such a method like OnPaint, the CLR will call your virtual method which uses the callvirt (as opposed to call) IL instruction. This is polymorphism. This one call does it all. When you instead handle an event in the derived class from the base class (like handling the Paint) event, there are several IL instructions (both in your implementation and in the event's add and remove accessors, not to mention whatever they require to add the handler to the callback chain) just to wire-up the event! When the event is fired, the collection of handlers is enumerated and each one is invoked with takes several more IL instructions (some to enumerate and jump back, and a couple to invoke the delegate). I hope this makes sense. Besides, when you override the event handler like OnPaint, you don't need to know the sender because the current instance of your class is the sender. All you need is the EventArgs (or derivative, like PaintEventArgs). It simplifies your class.

                        Microsoft MVP, Visual C# My Articles

                        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