【问题标题】:Replace TextBox with int用 int 替换 TextBox
【发布时间】:2013-04-17 09:07:34
【问题描述】:

我有以下,它的工作原理:

我的玩家等级:

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

现在在我的 form1 上有一个文本框和一个按钮。只要 SP 文本框中的值大于 0,该按钮就会将文本框中的值加一。问题是,我声明了六个变量来管理两个值,因为我必须将字符串转换为整数和所有这些废话。有没有办法用本质上是 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());
        */
    }

或者有没有更简单的方法来做到这一点,我完全忽略了。经过大量的试验和错误后,我终于可以开始工作了,我非常兴奋,但我愿意重做。但是,我宁愿只替换文本框,这样我就不会到处转换变量。

【问题讨论】:

  • 如果您正在使用 WinForms,那么有一个 NumericUpDown control 可能对您有用
  • 试过了,但我仍然必须将值从十进制更改为整数。还有向上向下箭头的整个问题,但主要是小数。
  • 直接创建类Player为什么不用int呢?

标签: c# string textbox int


【解决方案1】:

这是快速入门,而不是在装有 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 可以做什么,而不必搜索整个程序:

新玩家类:

// 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!");
        }
    }
}

【讨论】:

  • 我现在要试一试,但要明确一点,没有虚拟对象可以显示 winform 的变量内容,对吗?
  • 加上新的类建议不起作用,因为播放器无法从 player.cs 访问。 this.StatPoints 似乎在 if 语句中使用时有效。
  • 对不起@PaulWilliamFassett,我复制并粘贴了(正如我所说,我现在没有可用的 VS,所以只能通过目视工作)!我已修复 Updated 部分并将其更改为 this... 而不是 thePlayer...this 为您提供正在使用的类的实例。在类中包含该方法是一种更好的方法,因为您可以看到该类的能力并允许您在一个地方进行更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
相关资源
最近更新 更多