eathapeking
eathapeking

Reputation: 329

Messagebox will not stop showing

I am trying to write this code so that when click start Obj from "FORM1" will call this method to use and enable timer1.

When I click the start button a dog picture will start to move to right side until reach X= 620 then it will show messagebox " win"

However the message box keeps showing and doesn't stop after dogpic reach goal-line

class dog
{
    public int startpost;
    public int TrackLenght = 620;
    public PictureBox dogpic = null;
    public int Location = 0;
    public Random random=new Random();

    public void ResetStart()
    { 
        dogpic.Location = new System.Drawing.Point(40, startpost);
    }

    public bool testrun()
    {
        Point p = dogpic.Location;

        if (p.X < TrackLenght)
        {
            int distance = random.Next(5);

            p.X = p.X + distance;
            dogpic.Location = p;
            Location = dogpic.Location.X;
            return false;
        }
        else
        {
            MessageBox.Show(dogpic.Name + " win");

            return true;
        }
    }
}

Upvotes: 2

Views: 216

Answers (4)

Zaksh
Zaksh

Reputation: 963

On click of button you should call ResetStart() function which will enable timer and do your work and on reaching end point it should disable timer.

class dog
{
    public int startpost;
    public int TrackLenght = 620;
    public PictureBox dogpic = null;
    public int Location = 0;
    public Random random=new Random();

    public void ResetStart()
    { 
        dogpic.Location = new System.Drawing.Point(40, startpost);
        timer.Enabled=true;
    }

    public bool testrun()
    {
        Point p = dogpic.Location;

        if (p.X < TrackLenght)
        {
            int distance = random.Next(5);

            p.X = p.X + distance;
            dogpic.Location = p;
            Location = dogpic.Location.X;
            return false;
        }
        else
        {
            MessageBox.Show(dogpic.Name + " win");
            timer.Enabled=false;    
            return true;
        }
    }
}

Hope it will work.

Upvotes: 1

iair007
iair007

Reputation: 92

Try reseting p.X after you win.

Dont see it your code but I think you should do something like this:

public bool testrun()
    {

        Point p = dogpic.Location;

        if (p.X < TrackLenght)
        {
            int distance = random.Next(5);

            p.X = p.X + distance;
            dogpic.Location = p;
            Location = dogpic.Location.X;
           return false;
        }
        else
        {

            MessageBox.Show(dogpic.Name + " win");
            ResetStart()
            return true;
        }}}

Upvotes: 1

King King
King King

Reputation: 63317

//suppose dog1 is an instance of your dog class
//here is the Tick event handler of your timer1
private void timer1_Tick(object sender, EventArgs e){
    timer1.Enable = !dog1.testrun();
}

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

You can use a timer.

timer.Interval=5000;
timer.Enabled=true;
MessageBox.Show(dogpic.Name + " win");

You can associate it with the tck event.

private void timer_Tick(object sender,EventArgs evt) {
    timer.Enabled=false;
}

Upvotes: 0

Related Questions