a question for erel or agraham or anyone else

Discussion in 'Questions (Windows Mobile)' started by Ricky D, Oct 27, 2008.

  1. Ricky D

    Ricky D Well-Known Member Licensed User

    Hi guys.

    I have some labels on a panel. I'd like to draw a line around them.

    Is there a way to do this in C# for Smartdevice in VS2005?

    I see examples on the net for the desktop label - it has a Borderstyle property but alas the humble compact one doesn't.

    I've tried using a Graphics object but I get a null reference error. The Graphics object doesn't have a constructor so it won't compile if I instantiate a new one.

    any help would be appreciated.

    regards, Ricky
  2. agraham

    agraham Expert Licensed User

    You probably need to override the OnPaint event and use the Graphics given to you in the PaintEventArgs. Note however that this is not my area of expertise. Also Google for Custom Controls - there's a lot of info out there.

    protected override void OnPaint(PaintEventArgs e)
       base.OnPaint(e); // call the base class OnPaint
       e.Graphics.xxxx; // 
    do your own thing;
  3. Cableguy

    Cableguy Expert Licensed User

    you could use the b4p form.line to draw a rectangle around any control, like this:

    Form1.Line (Label1.Left-1,Label1.Top-1,Label1.Left+Label1.Width+1,Label1.Top+Label1.Height+1,cRed, B) 'Draws an empty red box
    in c# you just need to set the label.borderstyle to one of these values:
    None - default value
    FixedSingle - draws a single-line rectangle around the control.
    Fixed3D - Draws a 3d border on the control.
    Last edited: Oct 27, 2008
  4. agraham

    agraham Expert Licensed User

    I'm afraid he wants to draw on a Panel!

    In the Compact Framework labels don't have a BorderStyle, as he indicated in his first post, so this is not possible.

    You need to remember that there are significant differences (usually, but not always, omissions) between the full Framework on the desktop and the Compact Framework on the device so you can never make assumptions but need to check the documentation.
  5. BjornF

    BjornF Active Member Licensed User

    A work-around is to first draw an image and then put the label on top of the image. If the image is just slightly higher up, more to the left, wider and higher then you get the effect you want (I think). See jpg

    I know, it isn't very elegant but hey it works :)

    all the best / Björn

    Attached Files:

  6. BjornF

    BjornF Active Member Licensed User

    Sorry, the example wasn't very clear, see this one instead. / Björn

    Attached Files:

  7. bdiscount

    bdiscount Active Member Licensed User

    You can draw label with border and save as bmp etc, then load image to image or image button. there are some draw/paint progs here do a search.
  8. Ricky D

    Ricky D Well-Known Member Licensed User


    hi agraham,

    how do I override the onpaint event for each individual label?

    would it be

    protected override void la_OnPaint(PaintEventArgs e)

    where la is the label?

    regards, Ricky
  9. agraham

    agraham Expert Licensed User

    No, you are mapping B4ppc concepts onto C# and it doesn't work like that. You need to create your own label class inheriting from Label (called subclassing), do it in there, and then use instances of your own Label class instead of Label. You could also do it by subclassing Panel and overriding its OnPaint method You are entering a large world here and I can't walk you through it and Windows Forms programming it is not my area of expertise anyway. You will have to do your own research, there is much to learn.

    If you Google there are a lot of tutorials out there on the Web and you may need a couple of good books as well.
  10. Ricky D

    Ricky D Well-Known Member Licensed User

    ok mate, thanks

    I have come across some stuff on the net.

    I'll see how I go.

    regards, Ricky
  11. Ricky D

    Ricky D Well-Known Member Licensed User

    umm... onPaint not firing

    here is my simple attempt in C# to create a border around the label

    public class MyLabel : Label
    public MyLabel()

            protected override void OnPaint(PaintEventArgs e)
                Pen MyPen = new Pen(Color.Black,2);
                e.Graphics.DrawRectangle(MyPen, this.Left, this.Top, this.Width, this.Height);

            protected override void Dispose(bool disposing)
                if (disposing)
    when I create the label the here1 message pops up. I never see the here message popup - it's the one inside the OnPaint override.

    Any ideas anyone?

    regards, Ricky
  12. agraham

    agraham Expert Licensed User

    I've been looking at the VS2005 docs. The page for Label.OnPaint does say "NET Compact Framework
    Supported in: 2.0, 1.0", however on the Label members page the OnPaint protected method does not show the Compact Framework icon :confused:

    The Panel members page OnPaint protected method does show the Compact Framework icon but leads to the generic Control.OnPaint page, not a specific one for Panel.OnPaint. This page also says "NET Compact Framework Supported in: 2.0, 1.0".

    I'm thoroughly confused therefore as to how to interpret these documents as to what actually exists in the Compact Framework for a specific control. I'd suggest trying it on a Panel and see if it works there.
  13. Ricky D

    Ricky D Well-Known Member Licensed User

    doesn't work on a panel

    I did try that on a panel and same thing - if I could do it on the panel then it would be easy to surround each label with a "border" but alas I haven't got it to work :(

    regards, Ricky
  14. JJM

    JJM Active Member Licensed User


    I do that:
    I put in à Form of the designer a Label at the good position with a black background on this label I put an other one with 1 pixel less for the 4 sides!
    Now my label is round with a frame black of one (or more) pixel.

    Best regards

    JJ M
  15. agraham

    agraham Expert Licensed User

    Sorry but that's not the same. He is trying to do it in VS2005 in C# to make a library, he is not doing in in Basic4ppc itself.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice