the timer control does not work in a C# program.

B

Brian

I typed in a program from a book on C# that creates a ping pong ball game.
When I run the program the ball stays in the top left corner of the form
and does not move.

The code for the timer is

private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}

I have activated the timer.
I checked the code for the MoveBall.

Is there something else I have not done?
 
A

Arne Vajhøj

I typed in a program from a book on C# that creates a ping pong ball game.
When I run the program the ball stays in the top left corner of the form
and does not move.

The code for the timer is

private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}

I have activated the timer.
I checked the code for the MoveBall.

Is there something else I have not done?

Is timer1_Tick getting called?

Arne

PS: It would be easier to help if we have some more
complete code.
 
B

Brian

Arne Vajhøj said:
Is timer1_Tick getting called?

Arne

PS: It would be easier to help if we have some more
complete code.

Thanks for offering o help.

I'll try and send you the code when I go back to my desktop computer.
 
B

Brian Cryer

Brian said:
I typed in a program from a book on C# that creates a ping pong ball game.
When I run the program the ball stays in the top left corner of the form
and does not move.

The code for the timer is

private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}

I have activated the timer.
I checked the code for the MoveBall.

Is there something else I have not done?

As well as being enabled, the timer must have an interval. The interval is
in milliseconds, setting it very high would mean that calls to timer1_Tick
are very infrequent, which is why you might think they are being missed.

Something else to check - which I doubt is the problem here - is whether the
timer is getting disabled elsewhere in your code.

Arne's suggestion is a good one too. The problem might not be with the timer
but with MoveBall, so set a breakpoint there and see if it's being called
and step into MoveBall to see what it is or is not doing.

Hope this helps.
 
B

bradbury9

El miércoles, 7 de noviembre de 2012 02:22:08 UTC+1, Brian escribió:
I typed in a program from a book on C# that creates a ping pong ball game..

When I run the program the ball stays in the top left corner of the form

and does not move.



The code for the timer is



private void timer1_Tick(object sender, EventArgs e)

{

MoveBall();

}



I have activated the timer.

I checked the code for the MoveBall.



Is there something else I have not done?

If I remember correctly, there was a property in the Timer control called something similar to 'RaiseEvent' or 'CanRaiseEvent' that must be set to true so the Tick event fires
 
B

Brian

Brian Cryer said:
As well as being enabled, the timer must have an interval. The interval
is in milliseconds, setting it very high would mean that calls to
timer1_Tick are very infrequent, which is why you might think they are being missed.

Something else to check - which I doubt is the problem here - is whether
the timer is getting disabled elsewhere in your code.

Arne's suggestion is a good one too. The problem might not be with the
timer but with MoveBall, so set a breakpoint there and see if it's being
called and step into MoveBall to see what it is or is not doing.

Hope this helps.

The default setting for the timer is a value of 100.
I have posted the code in hope that an error can be found.
 
B

Brian

bradbury9 said:
El miércoles, 7 de noviembre de 2012 02:22:08 UTC+1, Brian escribió:

If I remember correctly, there was a property in the Timer control called
something similar to 'RaiseEvent' or 'CanRaiseEvent' that must be set to
true so the Tick event fires

From what the book told me the only thing I couldn't do was to drag the
timer on to the form but it locates itself below the form. There are no
error messages so the compiler must accept the timer.

The only properties that can be set for the timer are
Name. Timer1
Enabled. True
GenerateMember True
Interval. 100
Modifiers. Private
Tag
 
B

Brian

Peter Duniho said:
[...]
I have posted the code in hope that an error can be found.

You have posted the code where? I don't see it anywhere here. Keep in mind
that newsgroups not specifically marked as for binaries typically won't
allow attachments. Some ISPs may allow them through, but many will not.

Finally, it sounds as though you are using the Visual Studio Designer to
create your program. Which is great, but it means you need to put a little
more effort in if you want to post the code. To post a proper
concise-but-complete code example for a program created using the Designer,
you need to remember to copy the code out from the *.Designer.cs file as
well.

Pete

It was posted before I posted this message. If you look at the posts under
the subject of this post then you will find the code listing.
 
B

Brian

Peter Duniho said:
[...]
Arne's suggestion is a good one too. The problem might not be with the timer
but with MoveBall, so set a breakpoint there and see if it's being called
and step into MoveBall to see what it is or is not doing.

Though, Arne's best advice is to post a usable code example.

With just the one method, not even showing the usage of the Timer instance
or the contents of MoveBall(), never mind being a proper
concise-but-complete code example, there are literally dozens of plausible
theories as to why the code might not work as expected, and infinite
numbers of implausible theories.

The OP just needs to learn how to ask a good question.

Pete

I am asking why the program does not work and suspect it is something to do
with the timer control.
As I am new to C# i can only ask less technical questions.
 
B

bradbury9

I am asking why the program does not work and suspect it is something to do

with the timer control.

As I am new to C# i can only ask less technical questions.

What Pete suggests is that it would be wise to provide some code so others could help you with what is wrong or missing.

Wrong example:
Name. Timer1
Enabled. True
GenerateMember True
Interval. 100
Modifiers. Private
Tag



Good example:

this.cbAños.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbAños.FormattingEnabled = true;
this.cbAños.SelectedIndexChanged += new System.EventHandler(this.cbAños_SelectedIndexChanged);
....
private void cbAños_SelectedIndexChanged(object sender, EventArgs e)
{
realizarBusqueda();
}


That way I can show:
a) What events are being subscribed (this.cbAños.SelectedIndexChanged)
b) what code is being executed (cbAños_SelectedIndexChanged)
c) Other possible relevant properties (DropDownStyle, FormattingEnabled)
 
B

Brian Cryer

Brian said:
Peter Duniho said:
[...]
I have posted the code in hope that an error can be found.

You have posted the code where? I don't see it anywhere here. Keep in
mind
that newsgroups not specifically marked as for binaries typically won't
allow attachments. Some ISPs may allow them through, but many will not.

Finally, it sounds as though you are using the Visual Studio Designer to
create your program. Which is great, but it means you need to put a
little
more effort in if you want to post the code. To post a proper
concise-but-complete code example for a program created using the
Designer,
you need to remember to copy the code out from the *.Designer.cs file as
well.

Pete

It was posted before I posted this message. If you look at the posts under
the subject of this post then you will find the code listing.

I for one am not seeing it at my end. Have you included it in the body of a
post (which should show up) or as an attachment (which probably won't make
it through to me and others)?
 
B

Brian

Peter Duniho said:
[...]
I have posted the code in hope that an error can be found.

You have posted the code where? I don't see it anywhere here. Keep in mind
that newsgroups not specifically marked as for binaries typically won't
allow attachments. Some ISPs may allow them through, but many will not.

Finally, it sounds as though you are using the Visual Studio Designer to
create your program. Which is great, but it means you need to put a little
more effort in if you want to post the code. To post a proper
concise-but-complete code example for a program created using the Designer,
you need to remember to copy the code out from the *.Designer.cs file as
well.

Pete

Sorry I hit e-mail reply instead of feedback reply
Here is the full code

Here is the complete code for the Ping Pong game.
The ball starts at the top left of the form instead of moving.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pong_Game
{
public partial class Form1 : Form
{
int paddle_x = 0;
int paddle_y = 255;
int paddle_width = 35;
int paddle_height = 20;
Graphics g;
int x = 0;
int y = 0;
int dx = 8;
int dy = 5;


public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(MovePaddle);
this.Paint += new PaintEventHandler(paint1);

}

private void paint1(Object sender, PaintEventArgs e)
{
g = e.Graphics;
SolidBrush blueBrush = new SolidBrush(Color.Red);
g.FillRectangle(blueBrush, paddle_x, paddle_y, paddle_width,
paddle_height);
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillEllipse(brush, x, y, 10, 10);
}

private void MovePaddle(Object sender, MouseEventArgs e)
{
paddle_y = e.Y;
paddle_x = e.X;
Invalidate();

}

private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}

private void MoveBall()
{
int newBall_x = x + dx;
int newBall_y = y + dy;
if ((newBall_x < 0 || (newBall_x > 300))) dx = - dx;
if ((newBall_y < 0 || (newBall_y > 255))) dy = - dy;

if (((newBall_x > paddle_x) && (newBall_x < (paddle_x +
paddle_width)))&&((newBall_y > paddle_y) && (newBall_y < (paddle_y +
paddle_height))))
{
if (dx > 0 && dy > 0)
{
dx = -dy;
}
if (dx < 0 && dy > 0)
{
dy = -dy;
}

}
x = x + dx;
y = y + dy;
Invalidate();
}
}
}


Regards Brian

------------- Begin Message -------------

The code for the timer is

private void timer1_Tick(object sender, EventArgs e)
{
MoveBall();
}

I have activated the timer.
I checked the code for the MoveBall.

Is there something else I have not done?

Is timer1_Tick getting called?

Arne

PS: It would be easier to help if we have some more
complete code.
 
B

Brian

Brian Cryer said:
Brian said:
Peter Duniho said:
On Thu, 08 Nov 2012 06:24:33 GMT, Brian wrote:

[...]
I have posted the code in hope that an error can be found.

You have posted the code where? I don't see it anywhere here. Keep in >> mind
that newsgroups not specifically marked as for binaries typically won't
allow attachments. Some ISPs may allow them through, but many will not.

Finally, it sounds as though you are using the Visual Studio Designer to
create your program. Which is great, but it means you need to put a >> little
more effort in if you want to post the code. To post a proper
concise-but-complete code example for a program created using the >> Designer,
you need to remember to copy the code out from the *.Designer.cs file as
well.

Pete

It was posted before I posted this message. If you look at the posts under
the subject of this post then you will find the code listing.

I for one am not seeing it at my end. Have you included it in the body of
a post (which should show up) or as an attachment (which probably won't
make it through to me and others)?

Sorry.
I accidentally selected reply by e-mail instead of selecting follow up.
You should now see this in my previous reply.
 
A

Anders Eriksson

Hello Brian,
You need to start the timer!

public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(MovePaddle);
this.Paint += new PaintEventHandler(paint1);
// add this line
timer1.Start();
 
A

Anders Eriksson

Hello Brian,
You need to start the timer!

public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(MovePaddle);
this.Paint += new PaintEventHandler(paint1);
// add this line
timer1.Start();
 
B

Brian

Peter Duniho said:
[...]
As I am new to C# i can only ask less technical questions.

If you ask a question about your program, you need to show your program.

There's no "less technical question" here. Either you show all the code,
or you don't. Either you care whether anyone helps you, or you don't.

Being new to C# is irrelevant. Anyone asking a question about their code,
no matter what their experience or the nature of their question, needs to
show their code.

Take this advice to heart. You seem to be new to asking questions in a
public forum as well. Accept the advice given with as much credulity and
seriousness as you would advice that is specifically about programming.

(Which is to say, view it with a critical eye, but dismiss it outright at
your own risk)

Pete

I do appreciate any help I can get and well include the complete code in
the future.
 
B

Brian

Anders Eriksson said:
Hello Brian,
You need to start the timer!


// add this line
timer1.Start();
Thanks Anders for your help.
I did activate the timer but this code must be needed in addition to
setting the timer to active in the timer properites.
 
B

Brian

Peter Duniho said:
According to Brian (in a different post), he has set the Timer.Enable
property to "true" in the Designer, which _should_ cause it to be started
when it's initialized.

Brian: the code you posted was _NOT_ a concise-but-complete code example.
The "complete" means that the code will compile and run with no additional
code. This is exactly why I said that you need to take care to include all
the code that's in your *.Designer.cs file as well, which you failed to do
here.

Ideally you would also include the Program.cs file's code as well, but if
you are okay with people just making an attempt to debug by inspection
rather than actually running your code, that's less important.

But the Designer-generated code is very important. It contains all the
information about how you've configured the control objects, including your
Timer instance, in the Designer.

Still, I continue to suspect that you did not subscribe your event handler
properly, as I indicated in a separate reply yesterday.

Pete

Hi Pete.
I typed in the code for the Ping Pong out of a book called Microsoft Visual
C# 2005 Express Edition Programming for the absolute beginner.
I tried to download the source file for the Ping Pong from the web address
in the book but it was no longer available.
The only mistake I found so far was some brackets missing which were
missing in the code that was in the book.
So all I did was to copy and paste into the body of a post the code that I
had typed from the book.
So I'm thinking that either there is something wrong with the code or the
problem is with my computer. If someone that has Microsoft Visual C# copied
the code that I have pasted then would it work for their computer. I'm
using the latest version of Microsoft Visual C# express edition so maybe
the code of more suitable for the 2005 version of Microsoft Visual C#.
I don't know anything about the Designer-generated code you wrote about, is
this a file that is generated when the program is compiled?
 
B

Brian

Peter Duniho said:
[...]
So all I did was to copy and paste into the body of a post the code that I
had typed from the book.

But that won't configure the event handler for the Timer object.

Obviously you did more than just type the code in, since you have stated
that you _do_ have a timer object. That doesn't get put into the Form as a
Designer-controlled object without using the Designer.

So you need to go that extra step: select the Timer control in the
Designer, go to the properties window, activate the events tab, click in
the space next to the Tick event, and select your timer1_Tick method there.

Alternatively, add as the last line in your Form1 constructor:

this.timer1.Tick += new EventHandler(timer1_Tick);

Or more concisely:

this.timer1.Tick += timer1_Tick;

Which does exactly the same thing, just with shorter syntax.
So I'm thinking that either there is something wrong with the code or the
problem is with my computer.

It is neither. You just need to learn how the Visual Studio Designer
works, and take it into account when you are writing programs or entering
them from some hard-copy.
[...]
I don't know anything about the Designer-generated code you wrote about, is
this a file that is generated when the program is compiled?

Not when it's compiled. When you created the new project for the program.
If you go to the Solution Explorer window, you'll see that your Form1.cs
file has a + next to it. If you click on the +, it will expand to show you
the Form1.Designer.cs file.

Pete

Thanks Pete.

In adding the code > this.timer1.Tick += timer1_Tick; What does it
do?

What is the Form1 constructor:?

Sorry about all the questions which might seem like silly things to ask but
I hope it makes more sense to me as I educate myself more on C#.
 
B

Brian

Peter Duniho said:
It's helpful to consider the old-syntax version first:

this.timer1.Tick += new EventHandler(timer1_Tick);

This creates a new instance of the type System.EventHandler, which is a
delegate type. A delegate is a way to refer to a method as an object,
optionally with an instance bound to it (as occurs in this example).

A delegate can be "invoked" which is just like calling the method directly
from a program statement, but through the delegate object instead.

In the newer-style syntax:

this.timer1.Tick += timer1_Tick;

...the compiler "infers" what you mean. You'll find that there are
numerous places in C# where the compiler infers meaning, allowing you to
type less than you'd otherwise be required to. In this case, the compiler
sees that the expression on the right side of the = needs to resolve to a
delegate type, and that you've typed a "method group" name there. Given
those pieces of information, the compiler _infers_ that you also mean to
create a new delegate instance of the type that matches what's needed, so
supplies the necessary code as if you'd typed the whole "new
EventHandler(timer1_Tick)" out.


The thing that looks like a method but which doesn't have a return type and
is named the same name as the class. That's a constructor. It's what's
called when the object is created. It goes along with the "new" operator.

Constructors are used to initialize an instance of a type, or in the case
of a static constructor, static members of the type. They look like
methods and have formal parameter lists like methods. They are called just
like methods, except you use the "new" operator preceeding the constructor
name and argument list.


It will. Some of the things you ask though, you can probably learn a lot
more than what is feasible to explain in a newsgroup, simply by using your
favorite web search, or even searching in MSDN. (E.g. "what is a
constructor"...entering that as a search term in Bing turns up all sorts of
useful pages).

Pete

Thanks Pete for your help.
Its like being in a strange new world with the need to know some
terminology better. I have some books on C# from the library to try and
educate myself. Yes I agree that searching on the internet can be useful in
finding answers. I use Google a lot my searches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top