tags:

views:

84

answers:

3

I'm a newbie when it comes to coding C#, so please don't be too harsh on me. I've coded with ActionScript before, and notice that it's very similar.

Anyway, I need to basically build a simple application where 2 characters give each other "money"... or ints. The character name should be dynamic, and the buttons should play off of what the names are.

Please help! This is what I have so far:

namespace Lab_2
{
    public partial class Form1 : Form
    {
        Guy firstName;
        Guy secondName;
        int bank = 100;

        public Form1()
        {
            InitializeComponent();

            firstName = new Guy() { Cash = 100, Name = "Joe" };
            secondName = new Guy() { Cash = 50, Name = "Bob" };

            firstName = textBox1.Text;
            secondName = textBox2.Text;

            UpdateForm();
        }

        public void UpdateForm()
        {
            name1CashLabel.Text = firstName.Name + " has $" + firstName.Cash;
            name2CashLabel.Text = secondName.Name + " has $" + secondName.Cash;
            bankCashLabel.Text = "The bank has $" + bank;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "Give $10 to " + firstName.Name;

            if (bank >= 10)
            {
                bank -= firstName.ReceiveCash(10);
                UpdateForm();
            }
            else
            {
                MessageBox.Show("The bank is out of money.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bank += secondName.GiveCash(5);
            UpdateForm();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            secondName.ReceiveCash(firstName.GiveCash(10));
            UpdateForm();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            firstName.ReceiveCash(secondName.GiveCash(5));
            UpdateForm();
        }

        private void name1_Click(object sender, EventArgs e)
        {
            firstName.Name = textBox1.Text;
        }
    }
}
+1  A: 

The key bit seems to be the name1_Click method which updates the firstName object's Name to the contents of the textbox. Once you've done that you want to refresh the button's caption.

I'd create a new method:

public void RefreshButtonCaptions()
{
    button1.Text = "Give $10 to " + firstName.Name;
    button2.Text = "Give $10 to " + secondName.Name;
}

And then call it from name1_Click:

private void name1_Click(object sender, EventArgs e)
{
    firstName.Name = textBox1.Text;
    RefreshButtonCaptions()
}
Michael Shimmins
A: 

If you wanna just show new names on buttons, you can update them so simple like so:

private void name1_Click(object sender, EventArgs e) 
{ 
    firstName.Name = textBox1.Text; 
    button1.Text = "Give $10 to " + firstName.Name;
}

And maybe the same for name2_click (if you have):

private void name2_Click(object sender, EventArgs e) 
{ 
    secondName.Name = textBox2.Text; 
    button2.Text = "Give $10 to " + secondName.Name;
}
Dr TJ
A: 

Thanks!! This answered my problems! :) I know where I'll be returning when I need help further on in the book.

classicrock985