Request help for 2 newbie questions
-
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.
-
I want to use my new created control... this control graphically shows the content of an array ;)
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.
-
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.
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'sOnPaint
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-----
-
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'sOnPaint
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-----
-
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'sOnPaint
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-----
-
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.
-
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'sOnPaint
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-----
-
Point of clarification:
OnPaint
is not an event. It's a method that, in the defining class, raises thePaint
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 callbase.OnPaint
) and actually override all the functionality of that method since it's virtual. If you don't callbase.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-----
-
Point of clarification:
OnPaint
is not an event. It's a method that, in the defining class, raises thePaint
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 callbase.OnPaint
) and actually override all the functionality of that method since it's virtual. If you don't callbase.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-----
-
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'sOnPaint
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-----
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!
-
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!
Consider this: when you override such a method like
OnPaint
, the CLR will call your virtual method which uses thecallvirt
(as opposed tocall
) 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 thePaint
) event, there are several IL instructions (both in your implementation and in the event'sadd
andremove
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 likeOnPaint
, you don't need to know the sender because the current instance of your class is the sender. All you need is theEventArgs
(or derivative, likePaintEventArgs
). It simplifies your class.Microsoft MVP, Visual C# My Articles