locked
How can I refresh my windows form? RRS feed

  • Question

  • Ok, when I hit a key to move my image, It won't move. Heres the code I have:

            private void Form1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
            {
                int playerNewX = player.Location.X;
                int playerNewY = player.Location.Y;

                if (e.KeyCode == Keys.W)
                {
                    playerNewY =+ 50;
                    Point newLocation = new Point(playerNewX, playerNewY);
                    player.Location = newLocation;
                    player.Refresh();
                }
            }

    The image player won't move across the screen like I want. I have tried to refresh the screen using Form1.Refresh(); but It gives me jazz about using it as a type and not a method.

     

    I am using visual studio 2010


    Semper Fi
    Saturday, May 29, 2010 7:58 PM

Answers

  • Hi,

    Please replace the statement :playerNewY =+50; to

    playerNewY +=50;

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Louis.fr Thursday, June 3, 2010 10:01 AM
    • Marked as answer by Harry Zhu Sunday, June 6, 2010 4:21 AM
    Thursday, June 3, 2010 2:52 AM

All replies

  • You need to call Refresh() not Form1.Refresh(), Refresh is not a static method of the Form1 class.
    Saturday, May 29, 2010 8:02 PM
  • You need to call Refresh() not Form1.Refresh(), Refresh is not a static method of the Form1 class.

    That's why you see "this" throughout the Form1.Designer.cs.  "this" refers to this instance.  Not necessary but supposedly helps to clarify.  Refresh(); or this.Refresh();
    Saturday, May 29, 2010 8:14 PM
  • This forum is for C# language questions. You are not getting the best support you could possibly get by asking off-topic questions here. Note that using the appropriate forum benefits you more than anyone else. I think you can get better guidance in a forum for forms and graphics and such.

    The .Net classes make it possible for developers to be unaware of how Windows does graphics at the API level, but you would likely benefit from a better understanding of graphics programming in Windows. At the Windows API level, most graphics must be done in a paint message handler.



    Sam Hobbs; see my SimpleSamples.Info
    Sunday, May 30, 2010 2:16 AM

  • I think you misread the question.  It is perfectly valid for this group as it is C#, it is using typical constructs and the original code was only slightly off.  My guess is they come from a vb background.

    --
    Mike
    Sunday, May 30, 2010 2:27 AM
  • Code written in C# is not automatically a C# language question just because it is written in C#. Mike, I think you really misunderstood a lot of what I said. The most important point is that the question is more likely to get the best help if it is asked in a forum where graphics specialists are more likely to participate. It is not a matter of breaking rules; it is a matter of getting the best assistance possible.

    The question is about forms and controls. There are other forums for that. This question is about the Windows Media Player (right?); the Windows Media Player certainly is not part of the C# language. If you can find any of those in the language specification that can be obtained from Standard ECMA-334 C# Language Specification then I will apologize.



    Sam Hobbs; see my SimpleSamples.Info
    Sunday, May 30, 2010 5:44 AM
  • The question in this thread is essentially: "How do I address my current instance of an object?" 
    Sunday, May 30, 2010 6:21 AM
  • Try this.Invalidate();
    Sunday, May 30, 2010 7:13 AM
  • I think you didn't read the OP's post. He tried calling Refresh()  (which would solve his problem the POV of graphics) but he got a compiler error. That's good enough for a language question. There are other questions on this forum that are way more of topic than this.
    Sunday, May 30, 2010 11:16 AM
  • I think you didn't read the OP's post. He tried calling Refresh()  (which would solve his problem the POV of graphics) but he got a compiler error. That's good enough for a language question. There are other questions on this forum that are way more of topic than this.


    The original question says the "image player won't move across the screen like I want".



    Sam Hobbs; see my SimpleSamples.Info
    Sunday, May 30, 2010 5:57 PM
  • Hi,

    Please replace the statement :playerNewY =+50; to

    playerNewY +=50;

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Louis.fr Thursday, June 3, 2010 10:01 AM
    • Marked as answer by Harry Zhu Sunday, June 6, 2010 4:21 AM
    Thursday, June 3, 2010 2:52 AM