用int替换TextBox

时间:2013-04-17 09:07:35

标签: c# string textbox int

好。到目前为止,我有以下内容,它的工作原理(如果这听起来很愚蠢的话,对我来说是完全新的编程):

我的玩家类:

    public Player(string Str, string SP)
    {
        Strength = Str;
        StatPoints = SP;
    }
    public string StatPoints
    {
        get;
        set;
    }
    public string Strength
    {
        get;
        set;
    }

现在我的form1上有一个文本框和一个按钮。只要SP文本框中的值大于0,该按钮就会将文本框中的值增加1。问题是,我正在声明六个变量以管理两个值,因为我必须将字符串转换为整数和所有垃圾。有没有办法用一些固有的int替换文本框?这是我到目前为止的字符表代码。

    private void AddButton_Click(object sender, EventArgs e)
    {

        Player PCStats = new Player(StrBox.Text, SPBox.Text);
        int IntPCSP = Convert.ToInt16(PCStats.StatPoints);
        int IntPCStr = Convert.ToInt16(PCStats.Strength);

        if (IntPCSP >= 1 && IntPCStr <= 7)
        {
            IntPCStr++;
            IntPCSP--;
            PCStats.Strength = IntPCStr.ToString();
            PCStats.StatPoints = IntPCSP.ToString();
            StrBox.Text = PCStats.Strength;
            SPBox.Text = PCStats.StatPoints;
        }
        else
        {
            MessageBox.Show("Earn more experience!");
        }
        /*
        MessageBox.Show("PCStats.StatPoints equals" + PCStats.StatPoints);
        MessageBox.Show("PCStats,Strength equals" + PCStats.Strength);
        MessageBox.Show("IntPCSP Equals" + IntPCSP.ToString());
        MessageBox.Show("IntPCStr Equals" + IntPCStr.ToString());
        */
    }

或者是否有更简单的方法来做到这一点我完全被忽视了。经过大量的试验和错误后,我终于让这一点工作了,我感到非常兴奋,但我愿意重做它。我宁愿只是替换文本框,所以我不是在整个地方转换变量。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是快速的,不是在带有Visual Studio的计算机上,而是应该给你一个开始。另外,尝试命名变量等以获得更多含义。此外,这是为了修复您原样的内容,但请进一步了解有关将逻辑移入Player类的更新/建议......

我的播放器类:

public Player(int strength, int statPoints)
{
    this.Strength = strength;
    this.StatPoints = statPoints;
}

public int StatPoints { get; set; }

public int Strength { get; set; }

我的表格:

private void AddButton_Click(object sender, EventArgs e)
{
    Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(SPBox.Text));

    if (thePlayer.StatPoints > 0 && thePlayer.Strength < 8)
    {
        thePlayer.Strength++;
        thePlayer.StatPoints--;
        StrBox.Text = thePlayer.Strength.ToString();
        SPBox.Text = thePlayer.StatPoints.ToString();
    }
    else
    {
        MessageBox.Show("Earn more experience!");
    }
}

显然,您需要检查文本框中的值是否为整数。您可以使用另一个控件,屏蔽文本框等,或者使用int.Parse替换int.TryParse代码,以便在转换之前检查它。只是一些想法让你前进!

- 更新 -

您可以做的另一件事是更多Player类的逻辑。这样做更好,因为它将逻辑包含在一个地方,这样您就可以看到Player可以 DO 而不必搜索整个程序:

新玩家类:

// The Player class
public class Player
{
    // Constructor
    public Player(int strength, int statPoints)
    {
        this.Strength = strength;
        this.StatPoints = statPoints;
    }

    // Method to gain strength if enough StatPoints
    public bool GainStrength()
    {        
        bool playerHasEnoughStatPoints = true;

        if (this.StatPoints < 1)
        {
            playerHasEnoughStatPoints = false;
        }
        else if (this.Strength < 8)
        {
            this.Strength++;
            this.StatPoints--;
        }

        return playerHasEnoughStatPoints;
    }

    // Property for StatPoints
    public int StatPoints { get; set; }

    // Property for Strength
    public int Strength { get; set; }
}

新表单:

// The Form or similar
public class MyFormOrSimilar
{   
    // When button pressed try and add strength to the player
    protected void AddButton_Click(object sender, EventArgs e)
    {
        // Create new INSTANCE of the player and try to give them strength
        Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(SPBox.Text));
        if (thePlayer.GainStrength())
        {
            StrBox.Text = thePlayer.Strength.ToString();
            SPBox.Text = thePlayer.StatPoints.ToString();
        }
        else
        {
            MessageBox.Show("Earn more experience!");
        }
    }
}