为什么我的C#应用​​程序中的按钮不起作用?

时间:2010-10-01 01:46:39

标签: c# winforms

我正在自己的书中做这个实验室,我做了一个鲨鱼比赛的应用程序。有一个单选按钮,可以动态更新右侧的标签,以及实际开始比赛的按钮。一切都用来工作,然后我重命名了一些东西,现在没有任何作用。

应用程序截图:

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

表格类:

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 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();
        }
    }
}

鲨鱼班:

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;
        }
    }
}

如果需要,我可以提供更多资源,但这是它的主要要点。

3 个答案:

答案 0 :(得分:2)

当你重命名它们时,你可能是通过编辑代码而不是通过更改控件属性来实现的。

Winforms中的Winforms设计师在幕后为您创建了代码,用于连接发明内容。此代码使用控件名称。查找名为formname_designer.cs的文件。请注意,有些行仍然具有旧的控件名称。您可以更改此代码

这就是为什么在你开始时控制好名字的好习惯。

答案 1 :(得分:2)

使用Visual Studio,确保以下内容:

1)对于每个单选按钮,请确认CheckedChanged事件已连接到您的myChanged功能。
2)确认“Bets”Button.Click事件已连接到您的Bet_Click功能 3)验证“比赛!” Button.Click事件与您的raceBtn_Click函数相关联。

重命名事物的一种安全方法是右键单击变量名称Refactor,Rename。这将确保正确重命名对变量的任何引用

答案 2 :(得分:1)

确保控件上的事件仍然连接到代码中的正确事件处理程序。有时当您重命名时,此链接可能会被破坏。