开发者

Why aren't the buttons in my C# Application working?

I am doing this lab out of a book on my own, and I made an application in which sharks are racing. There is a radio button that should update a label on the right dynamically, as well as a button that actually starts the race. Everything used to work and then I renamed a few things, and now nothing works.

Screenshot of application:

image http://cl.ly/f08f4e22761464e0c2f3/content

Form Class:

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 project1
{
    public partial class Game : Form
    {
        private Shark[] sharks;
        private Guy[] guys;
        private Guy selectedGuy;

        public Game()
        {
            InitializeComponent();

            Random moreRandom = new Random();

            int start = myTrack.Location.X;
            int finish = myTrack.Width - 65;

            sharks = new Shark[4]
                {
                    new Shark() {myRandom = moreRandom, myPictureBox = myShark1, myPBStart = start, trackLength = finish},
                    new Shark() {myRandom = moreRandom, myPictureBox = myShark2, myPBStart = start, trackLength = finish},
                    new Shark() {myRandom = moreRandom, myPictureBox = myShark3, myPBStart = start, trackLength = finish},
                    new Shark() {myRandom = moreRandom, myPictureBox = myShark4, myPBStart = start, trackLength = finish}
                };

            guys = new Guy[3]
                {
                    new Guy() {myName="Joe", cash=50, myRadioButton=rbGuy1, myLabel=labelBet1},
                    new Guy() {myName="Bob", cash=75, myRadioButton=rbGuy2, myLabel=labelBet2},
                    new Guy() {myName="Al", cash=45, myRadioButton=rbGuy3, myLabel=labelBet3}
                };

            selectedGuy = guys[0];
            rbGuy1.Tag = guys[0];
            rbGuy2.Tag = guys[1];
            rbGuy3.Tag = guys[2];            

            updateGui();
        }

        private void myChanged(object sender, EventArgs e)
        {
            selectedGuy = getSelectedGuy(sender);
            betterLabel.Text = selectedGuy.myName;
        }

        private void betAmountValue(object sender, EventArgs e)
        {
            updateMin();
        }

        private void Bet_Click(object sender, EventArgs e)
        {
            int bet = (int) betAmount.Value;
            int myFish = (int) sharkNumber.Value;
            selectedGuy.placeBet(bet, myFish);
            updateGui();
        }

        private void raceBtn_Click(object sender, EventArgs e)
        {
            betBtn.Enabled = false;

            bool noWinner = true;
            while(noWinner)
            {
                for (int dogFish = 0; dogFish < sharks.Length; dogFish++)
                {
                    Application.DoEvents();
                    if(sharks[dogFish].Swim())
                    {
                        showWinner(dogFish);
                        collectBets(dogFish);
                        noWinner = false;
                    }
                }
            }

            updateGui();

            betBtn.Enabled = true;
        }

        private void showWinner(int fish)
        {
            MessageBox.Show(string.Format("Winner Winner People Dinner! \nShark {0} won!", fish + 1));
        }

        private void collectBets(int fish)
        {
            for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
            {
                guys[guyNumber].collect(fish + 1);
                guys[guyNumber].resetBet();
            }
        }

        private void updateMin()
        {
            minBetLabel.Text = string.Format("Minimum bet: 5 bucks", betAmount.Value);
        }

        private Guy 开发者_JS百科getSelectedGuy(object sender)
        {
            RadioButton rb = (RadioButton)sender;
            return (Guy)rb.Tag;
        }

        private void updateGui()
        {
            for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
            {
                guys[guyNumber].updateLabels();
            }

            for (int fish = 0; fish < sharks.Length; fish++)
            {
                sharks[fish].startPosition();
            }

            updateMin();
        }
    }
}

Shark Class:

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace project1
{
    public class Shark
    {
        public int myPBStart; // Where the PictureBox starts
        public int trackLength; // How long the racetrack is
        public PictureBox myPictureBox = null; // The PictureBox object
        public int location = 0; // My location on the racetrack
        public Random myRandom; // An instance of Random

        public Shark()
        {
            location = 0;
            myPictureBox = new PictureBox();
            myRandom = new Random();
            trackLength = 100;
            myPBStart = 0;
        }

        public bool Swim()
        {
            int distance = myRandom.Next(1, 4);
            location += distance;

            movePB(distance);

            return location > trackLength;
        }

        private void movePB(int distance)
        {
            Point p = myPictureBox.Location;
            p.X += distance;
            myPictureBox.Location = p;
        }

        public void startPosition()
        {
            location = myPBStart;

            Point p = myPictureBox.Location;
            p.X = location;
            myPictureBox.Location = p;
        }
    }
}

I can supply more resources if needed, but this is the main gist of it.


Using Visual Studio, make sure of the following:

1) For each radio button, verify the CheckedChanged event is hooked up to your myChanged function.
2) Verify the "Bets" Button.Click event is hooked up to your Bet_Click function.
3) Verify the "Race!" Button.Click event is hooked up to your raceBtn_Click function.

A safe way to rename things is to right click on the variable name, Refactor, Rename. This will ensure any references to the variable are renamed properly


when you renamed them you probably did it by editing the code rather than by changing the control properties.

THe Winforms designer in VS created code for you behind the scenes that wires the invents up. This codes uses the control names. Look for a file called formname_designer.cs. Notice that there are lines that still have the old control names. You can change this code

This is why its a good habit to give controls nice names when you start.


Make sure that the events on your controls are still connected to the correct event handlers in your code. Sometimes when you rename things, this link can get broken.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜