2013-04-17 28 views
0

好的。到目前爲止,我有以下內容,它的工作原理(完全新的編程所以裸陪我,如果這聽起來很愚蠢):用int代替TextBox

我的播放器類:

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

還是有更簡單的方法來做到這一點,我完全忽略了。在經歷了很多試驗和錯誤之後,我終於得到這份工作讓我感到非常興奮,但我願意重做它。我寧願只是替換文本框,所以我不會將變量全部轉換。任何幫助是極大的讚賞。

+0

如果你正在做的WinForms然後有一個[NumericUpDown控件(http://msdn.microsoft.com/en-gb/library/sys tem.windows.forms.numericupdown.aspx)這可能對你有用 – musefan

+0

嘗試過,但我仍然必須將值從十進制更改爲整數。還有上下箭頭的問題,但大多是小數點。 –

+0

直接創建類Player時,爲什麼不使用int? – Tonix

回答

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.Parseint.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!"); 
     } 
    } 
} 
+0

我現在要試一試,但要清楚的是,沒有虛擬對象可以顯示一個winform的變量的內容,是否正確? –

+0

加上新的類建議不起作用,因爲Player無法從player.cs訪問。但是,如果在if語句中使用this.StatPoints似乎可行。 –

+0

對不起@PaulWilliamFassett,我複製並粘貼(如我說我沒有VS現在可用,所以看到工作)!我修復了** Updated **部分,並將其更改爲'this ...'而不是'thePlayer ...'。這個''給你正在使用的類的實例。讓班級中的方法是一種更好的方法,因爲您可以看到班級的能力,並允許您在一個地方進行更改。 – Belogix