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. Code to move window not working.

Code to move window not working.

Scheduled Pinned Locked Moved C#
csharplinqgraphicsquestionlounge
15 Posts 3 Posters 4 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.
  • U User 12768383

    Ok, I've gotten it to move horizontally, vertically and diagonally fine so i've added boundaries so that it can't move past the screen limits. However when i added that it just shakes back and forth. From what i understand it first increases the x co-ordinates, moving the window to the right. Then the if statement checks if it has gone past the left of the screen and if it has it will reverse the direction the window moves. The second if statement checks that x co-ordinates including the width of the window doesn't past the width of the screen. Is this all correct?

    private void timer4_Tick (object sender, EventArgs e)
    {
    this.Left += WindowVx;
    if (this.Left < 0) {
    WindowVx = -WindowVx;
    }
    if (this.Left + this.Width > ClientSize.Width) {
    WindowVx = -WindowVx;
    }
    this.Refresh ();
    }

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

    So what you want it to do is move all the way right, then when it reaches the edge, turn round and move the other way? To do that isn't difficult - when you reach the screen edge, change the sign on the value you are adding! Then, when you reach the other edge, change it back again.

    Left += deltaX;
    if (Left < 0 || (Left + Width) > ClientSize.Width)
    {
    deltaX = -deltaX;
    }

    You don't need to specify this all the time - you only need it when you have a local variable of the same name as your class level one and you want to access the class level variable. You also don't need to refresh anything - when you update Left and Top they will refresh for you.

    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

    "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

    U 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      So what you want it to do is move all the way right, then when it reaches the edge, turn round and move the other way? To do that isn't difficult - when you reach the screen edge, change the sign on the value you are adding! Then, when you reach the other edge, change it back again.

      Left += deltaX;
      if (Left < 0 || (Left + Width) > ClientSize.Width)
      {
      deltaX = -deltaX;
      }

      You don't need to specify this all the time - you only need it when you have a local variable of the same name as your class level one and you want to access the class level variable. You also don't need to refresh anything - when you update Left and Top they will refresh for you.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      U Offline
      U Offline
      User 12768383
      wrote on last edited by
      #7

      Hm... for some reason the window just moves back and forth kind of like it's vibrating. I'm not really sure what's causing it as the code without the if statement works fine. This is the code right now:

              this.Left += WindowVx;
              if (Left < 0 || (Left + this.Width) > ClientSize.Width){
                  WindowVx = -WindowVx;
              }
      
      OriginalGriffO 1 Reply Last reply
      0
      • U User 12768383

        Hm... for some reason the window just moves back and forth kind of like it's vibrating. I'm not really sure what's causing it as the code without the if statement works fine. This is the code right now:

                this.Left += WindowVx;
                if (Left < 0 || (Left + this.Width) > ClientSize.Width){
                    WindowVx = -WindowVx;
                }
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #8

        Check your "ClientSize" object and see what values it has - at a guess the Width is zero. You know how to use the debugger, yes?

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "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

        U 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Check your "ClientSize" object and see what values it has - at a guess the Width is zero. You know how to use the debugger, yes?

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          U Offline
          U Offline
          User 12768383
          wrote on last edited by
          #9

          I checked and the width is 282 i think. And by debugger you mean like the play button on xamarin studio? That's what i've been using to test my program. Also just in case i'll post the rest of my form information.

                  this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(282, 253);
                  this.ControlBox = false;
                  this.Controls.Add(this.webBrowser1);
                  this.KeyPreview = true;
                  this.MaximizeBox = false;
                  this.MinimizeBox = false;
                  this.Name = "Form1";
                  this.ShowIcon = false;
                  this.ShowInTaskbar = false;
                  this.Text = "test";
                  this.TopMost = true;
                  this.ResumeLayout(false);
          
          OriginalGriffO 1 Reply Last reply
          0
          • U User 12768383

            I checked and the width is 282 i think. And by debugger you mean like the play button on xamarin studio? That's what i've been using to test my program. Also just in case i'll post the rest of my form information.

                    this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
                    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                    this.ClientSize = new System.Drawing.Size(282, 253);
                    this.ControlBox = false;
                    this.Controls.Add(this.webBrowser1);
                    this.KeyPreview = true;
                    this.MaximizeBox = false;
                    this.MinimizeBox = false;
                    this.Name = "Form1";
                    this.ShowIcon = false;
                    this.ShowInTaskbar = false;
                    this.Text = "test";
                    this.TopMost = true;
                    this.ResumeLayout(false);
            
            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #10

            OK: so you can't affect the Left and Top of your current window - because the ClientSize of the current window is going to be at best the same as the width of the window! So, if you are trying to move something within the current window, use its properties: Left, Width, and Top, and the Window Client Area. Your original code shows you being inside the form, so this refers to the whole form, not anything within it. If you are trying to move the form, then you need to get the information on sizing from whatever it is displayed on - probably the desktop (or android equivalent, if that's what you are using).

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "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

            U 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              OK: so you can't affect the Left and Top of your current window - because the ClientSize of the current window is going to be at best the same as the width of the window! So, if you are trying to move something within the current window, use its properties: Left, Width, and Top, and the Window Client Area. Your original code shows you being inside the form, so this refers to the whole form, not anything within it. If you are trying to move the form, then you need to get the information on sizing from whatever it is displayed on - probably the desktop (or android equivalent, if that's what you are using).

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              U Offline
              U Offline
              User 12768383
              wrote on last edited by
              #11

              When you say move the form do you mean the things inside the window? I'm trying to move the entire window and by information on sizing do you mean my screen resolution? If i use Left, Width, and Top, and the Window Client Area to move the thing inside the window, do i need something else to move the window itself? Edit: Oh i just looked it up and form is the window itself.

              OriginalGriffO 1 Reply Last reply
              0
              • U User 12768383

                When you say move the form do you mean the things inside the window? I'm trying to move the entire window and by information on sizing do you mean my screen resolution? If i use Left, Width, and Top, and the Window Client Area to move the thing inside the window, do i need something else to move the window itself? Edit: Oh i just looked it up and form is the window itself.

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

                Backup a second... When you are a form, you have four properties you are interested in: Width and Height - which tell you how big the whole form is; then Top and Left - which tell you where you are relative to the container the form is displayed within. For Windows, that's the Desktop - so Top and Left of zero is the top left hand corner of your monitor. The Client Area of a form is the bit of the form that isn't the title bar and the form border and it measured relative to the form top left corner - so the Client Area will normally be smaller than the Width and Height, and never, ever bigger. So trying to move the form doesn't involve the form's client area for "reaching an edge" - it involves whatever the form is contained within: normally the Desktop. See if you have a Screen object you can access the Width and Height from and try those.

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "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

                U 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Backup a second... When you are a form, you have four properties you are interested in: Width and Height - which tell you how big the whole form is; then Top and Left - which tell you where you are relative to the container the form is displayed within. For Windows, that's the Desktop - so Top and Left of zero is the top left hand corner of your monitor. The Client Area of a form is the bit of the form that isn't the title bar and the form border and it measured relative to the form top left corner - so the Client Area will normally be smaller than the Width and Height, and never, ever bigger. So trying to move the form doesn't involve the form's client area for "reaching an edge" - it involves whatever the form is contained within: normally the Desktop. See if you have a Screen object you can access the Width and Height from and try those.

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                  U Offline
                  U Offline
                  User 12768383
                  wrote on last edited by
                  #13

                  Oh i think the problem is that sometimes the window is created partially outside the screen resolution and for some reason the if statements make the window start vibrating because it's constantly changing directions. So to fix the problem i need to make sure that the window isn't created outside of the screen. I changed a few things and it seemed to fix a majority of the problems:

                  WindowX = (random.Next(0, width - Width));
                  WindowY = (random.Next(0, height - Height));

                          this.Left += WindowVx;
                          if (Left < 0 || (Left + this.Width) > width){
                              WindowVx = -WindowVx;
                          } 
                  
                  
                          this.Top += WindowVy;
                          if (Top < 0 || (Top + this.Height) > height) {
                              WindowVy = -WindowVy;
                          } 
                  

                  However sometimes the window is still created a bit past the bounds for some reason, but I'll work on it tomorrow, for now i need to sleep. Thank you for the help so far though! Oh, and for screen object, do you mean this?:

                      private int width = Screen.PrimaryScreen.WorkingArea.Width;
                      private int height = Screen.PrimaryScreen.WorkingArea.Height;
                  

                  I changed it to this:

                      private int width = Screen.PrimaryScreen.Bounds.Width;
                      private int height = Screen.PrimaryScreen.Bounds.Height;
                  

                  That way it'll go over my task bar.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • U User 12768383

                    Oh i think the problem is that sometimes the window is created partially outside the screen resolution and for some reason the if statements make the window start vibrating because it's constantly changing directions. So to fix the problem i need to make sure that the window isn't created outside of the screen. I changed a few things and it seemed to fix a majority of the problems:

                    WindowX = (random.Next(0, width - Width));
                    WindowY = (random.Next(0, height - Height));

                            this.Left += WindowVx;
                            if (Left < 0 || (Left + this.Width) > width){
                                WindowVx = -WindowVx;
                            } 
                    
                    
                            this.Top += WindowVy;
                            if (Top < 0 || (Top + this.Height) > height) {
                                WindowVy = -WindowVy;
                            } 
                    

                    However sometimes the window is still created a bit past the bounds for some reason, but I'll work on it tomorrow, for now i need to sleep. Thank you for the help so far though! Oh, and for screen object, do you mean this?:

                        private int width = Screen.PrimaryScreen.WorkingArea.Width;
                        private int height = Screen.PrimaryScreen.WorkingArea.Height;
                    

                    I changed it to this:

                        private int width = Screen.PrimaryScreen.Bounds.Width;
                        private int height = Screen.PrimaryScreen.Bounds.Height;
                    

                    That way it'll go over my task bar.

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

                    :thumbsup: Sleep well!

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    "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

                    R 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      :thumbsup: Sleep well!

                      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                      R Offline
                      R Offline
                      Rahul VB
                      wrote on last edited by
                      #15

                      That was an excellent thread ! Very nice OG :thumbsup:

                      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