Visual C# Drawing is Beneath Everything

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • str1ker
    New Member
    • Jun 2006
    • 23

    Visual C# Drawing is Beneath Everything

    Hi again,

    This problem is a little more complicated, and once again is in C#. I've create a form, with a groupbox, and in it, a picturebox. In the picturebox's paint event, I've drawn a black line through the middle of the picturebox, but the line is behind the picturebox and groupbox.

    How do I get the line to be drawn in front of everything? In Visual BASIC, doing the same thing works fine, but in C#, anything that is drawn, is behind all objects and controls...

    Sincerely,

    Str!ker
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Moving this to the.NET forum.

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      What code are you using to draw the line?

      Comment

      • str1ker
        New Member
        • Jun 2006
        • 23

        #4
        Code:
        private void pictureBox2_Paint(object sender, PaintEventArgs e)
                {
                    int idlePoint;
                    idlePoint = this.trackBar1.Value;
        
                    Point point1 = new Point(229, 210 - idlePoint);
                    Point point2 = new Point(229, 210 - idlePoint);
        
                    Graphics graphics;
                    graphics = this.CreateGraphics();
                    Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
                    graphics.DrawLine(pen, point1, point2);
                }
        That's the code. I've done similar things in BASIC, and that works, but in C# it doesn't...

        Comment

        • str1ker
          New Member
          • Jun 2006
          • 23

          #5
          anybody? this is really annoying for me.

          Comment

          • marmot4
            New Member
            • Jul 2007
            • 1

            #6
            The problem is that you used this.CreateGrap hics() to obtain a graphics object. This gives you a graphics object at the level of the Form, so your line is appearing on the form.

            You want your line to appear on the picturebox, so you shouldn't use this.CreateGrap hics(). Instead just use the graphics object that the event args provides you: e.Graphics;

            Its always best when in a paint event or OnPaint override to use the provided Graphics object rather than creating your own.

            Hope this helps,
            Kevin

            Comment

            Working...