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. I have a little problem with my application.

I have a little problem with my application.

Scheduled Pinned Locked Moved C#
winformsquestiongraphicsalgorithmshelp
8 Posts 3 Posters 1 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.
  • A Offline
    A Offline
    Alex Manolescu
    wrote on last edited by
    #1

    Hello, i have a little problem with my application. I'll try to describe as simple and organised as I can. Objective: I have two user controls in my main form. I've linked them with a simple line that updates (refresh the form) when one of the user controls was moved. I want the user of my application to move (with the mouse) one of the user controls, that I've created, in the main form. The user controls are added directly to the main form like this: this.Control.Add(uc1);. Problem: The problem is when the user move one of the user controls. The refresh/redraw of the form loses the line partialy or absolute. Sometimes the line is visible, sometimes it isn't. I have NO PROBLEM with the algorithm. It's a simple 2D line. I draw the line in the Paint event of the main form. Observations: 1. I've tryed to draw on a panel, the result was the same. 2. I've tryed setting the DoubleBuffered property to TRUE, the result was the same. 3. I've observe that pictureBox control donsen't have this problem. I've tested with a simple "line tracking the mouse" application and seen that there is no problem when redrawing. So my next question was: - Can I draw/use/add an user control on a pictureBox control? or a better way to draw GDI+ on Windows Forms to get rid of this redrawing problem? I've tryed to add the user controls in the pictureBox control but they didn't appear. Thank you!

    Alex M.

    OriginalGriffO 1 Reply Last reply
    0
    • A Alex Manolescu

      Hello, i have a little problem with my application. I'll try to describe as simple and organised as I can. Objective: I have two user controls in my main form. I've linked them with a simple line that updates (refresh the form) when one of the user controls was moved. I want the user of my application to move (with the mouse) one of the user controls, that I've created, in the main form. The user controls are added directly to the main form like this: this.Control.Add(uc1);. Problem: The problem is when the user move one of the user controls. The refresh/redraw of the form loses the line partialy or absolute. Sometimes the line is visible, sometimes it isn't. I have NO PROBLEM with the algorithm. It's a simple 2D line. I draw the line in the Paint event of the main form. Observations: 1. I've tryed to draw on a panel, the result was the same. 2. I've tryed setting the DoubleBuffered property to TRUE, the result was the same. 3. I've observe that pictureBox control donsen't have this problem. I've tested with a simple "line tracking the mouse" application and seen that there is no problem when redrawing. So my next question was: - Can I draw/use/add an user control on a pictureBox control? or a better way to draw GDI+ on Windows Forms to get rid of this redrawing problem? I've tryed to add the user controls in the pictureBox control but they didn't appear. Thank you!

      Alex M.

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

      Try adding Control.Parent.Invalidate() when you move your control. That way the line should be re-drawn. You can't add the controls to a PictureBox - it isn't a container and doesn't know what to do with them.

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "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

      A S 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Try adding Control.Parent.Invalidate() when you move your control. That way the line should be re-drawn. You can't add the controls to a PictureBox - it isn't a container and doesn't know what to do with them.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        A Offline
        A Offline
        Alex Manolescu
        wrote on last edited by
        #3

        Thank you for you're quick reply! :) I've read about pictureBox and see that isn't a container. So it was a foolish from me to try adding a user control to a pictureBox control. I've tryed to Refresh()/Invalidate() + Update() method(s) when I move one of the user control, but the result is still the same. To be more clear, see my code:

        DesenTabel t1, t2;

        private void AddUserControls()
        {
        t1 = new DesenTabel();
        t2 = new DesenTabel();

        this.Controls.Add(t1);
        this.Controls.Add(t2);
        }

        private void Form_Paint(object sender, PaintEventArgs e)
        {
        // here I draw the line
        e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), t1.Location, t2.Location);
        }

        private void t1_Move(object sender, EventArgs e)
        {
        // this.Refresh() is equivalent to:

        this.Invalidate();
        this.Update();
        }

        private void t2_Move(object sender, EventArgs e)
        {
        // this.Refresh() is equivalent to:

        this.Invalidate();
        this.Update();
        }

        The line isn't drawing well. It intrerupts on the way of linking the two tables because of the redraw! How can I get rid of this problem ? Thank you!

        Alex M.

        OriginalGriffO 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Try adding Control.Parent.Invalidate() when you move your control. That way the line should be re-drawn. You can't add the controls to a PictureBox - it isn't a container and doesn't know what to do with them.

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          S Offline
          S Offline
          Som Shekhar
          wrote on last edited by
          #4

          Usually, it makes more sense to create a line control. or use VisualBasic PowerPack to add a LineShape. Or the best of all, draw all on a canvas. What OriginalGriff suggested will work but will have too much flickering. Also, the line will only be drawn when you have finished moving the control. Atleast my experience has been troublesome so far.

          A 1 Reply Last reply
          0
          • A Alex Manolescu

            Thank you for you're quick reply! :) I've read about pictureBox and see that isn't a container. So it was a foolish from me to try adding a user control to a pictureBox control. I've tryed to Refresh()/Invalidate() + Update() method(s) when I move one of the user control, but the result is still the same. To be more clear, see my code:

            DesenTabel t1, t2;

            private void AddUserControls()
            {
            t1 = new DesenTabel();
            t2 = new DesenTabel();

            this.Controls.Add(t1);
            this.Controls.Add(t2);
            }

            private void Form_Paint(object sender, PaintEventArgs e)
            {
            // here I draw the line
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), t1.Location, t2.Location);
            }

            private void t1_Move(object sender, EventArgs e)
            {
            // this.Refresh() is equivalent to:

            this.Invalidate();
            this.Update();
            }

            private void t2_Move(object sender, EventArgs e)
            {
            // this.Refresh() is equivalent to:

            this.Invalidate();
            this.Update();
            }

            The line isn't drawing well. It intrerupts on the way of linking the two tables because of the redraw! How can I get rid of this problem ? Thank you!

            Alex M.

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

            private void AddUserControls()
            {
            t1 = new DesenTabel();
            t2 = new DesenTabel();

            this.Controls.Add(t1);
            this.Controls.Add(t2);
            }

            Is your problem: add

            t1.Move += new EventHandler(node_Move);
            t2.Move += new EventHandler(node_Move);
            ...
            void node_Move(object sender, EventArgs e)
            {
            Invalidate();
            }

            to tie the Move event to the handlers...

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            "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

            A 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              private void AddUserControls()
              {
              t1 = new DesenTabel();
              t2 = new DesenTabel();

              this.Controls.Add(t1);
              this.Controls.Add(t2);
              }

              Is your problem: add

              t1.Move += new EventHandler(node_Move);
              t2.Move += new EventHandler(node_Move);
              ...
              void node_Move(object sender, EventArgs e)
              {
              Invalidate();
              }

              to tie the Move event to the handlers...

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

              A Offline
              A Offline
              Alex Manolescu
              wrote on last edited by
              #6

              I've debugged my application and the events are working fine. The move event for re-drawing is triggered. I've modifyed and simplyfied the AddUserControls method to be more clear to you. The events exists! My mistake for not showing that. Probably I need to make a canvas to add custom shapes and controls on it, but I still hope this isn't the answer :) Thank you!

              1 Reply Last reply
              0
              • S Som Shekhar

                Usually, it makes more sense to create a line control. or use VisualBasic PowerPack to add a LineShape. Or the best of all, draw all on a canvas. What OriginalGriff suggested will work but will have too much flickering. Also, the line will only be drawn when you have finished moving the control. Atleast my experience has been troublesome so far.

                A Offline
                A Offline
                Alex Manolescu
                wrote on last edited by
                #7

                Hi, i'll try drawing all to a canvas, in order to make all go nice and well. I'll bring back news! Cheer's!

                Alex M.

                S 1 Reply Last reply
                0
                • A Alex Manolescu

                  Hi, i'll try drawing all to a canvas, in order to make all go nice and well. I'll bring back news! Cheer's!

                  Alex M.

                  S Offline
                  S Offline
                  Som Shekhar
                  wrote on last edited by
                  #8

                  Alex Manolescu wrote:

                  I'll bring back news!

                  All the best.

                  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