2013-01-24 18 views
-2

我有一個雙維度字符串數組當我攻擊使用我的按鈕事件處理如何從一個字符串中取兩個整數值,然後將它們放回兩個單獨的整數中?

private void AttackHandler(object sender, EventArgs e) 
     { 
      Button clickedButton; 
      string tagValue = ""; 

      clickedButton = (Button)sender; 
      tagValue = (string)clickedButton.Tag; 
      theSea.attackLocation(tagValue); 

     } 

這顯然發出一個字符串,如0保存每個按鈕的具體座標

string[,] gridPass = new string[20, 20]; 


    private void Form1_Load(object sender, EventArgs e) 
    { 
     foreach (int row in Enumerable.Range(0, 20)) 
     { 
      foreach (int col in Enumerable.Range(0, 20)) 
      { 

       Button b = new Button(); 
       b.Size = new System.Drawing.Size(30, 30); 
       b.Location = new Point(row * 30, col * 30); 
       gridPass[row, col] = row.ToString() + " - " + col.ToString(); 
       b.Tag = gridPass[row, col]; 
       b.Text = gridPass[row, col]; 
       this.Controls.Add(b); 
       b.Click += new EventHandler(AttackHandler); 
      } 


     } 

- 1或8 - 4無論按鈕的座標是什麼。 當我將該字符串傳遞給我的Sea類中的attackLocation方法時,我希望能夠提取這兩個數字以引用它們與我的Sea類中的數組,以查看是否有船。我需要這些X和Y值來引用另一個數組中基本完全相同的位置。所以我可以做類似的事情。

public void attackLocation(string attackCoords) 
    { 
     MessageBox.Show("Attacking " + attackCoords); 
     x = however to convert it back; 
     y = however to convert it back; 
     foreach (Ship s in shipList) 
     { 
      if (grid[x,y] == 0) 
      { 
       MessageBox.Show("Attacked this block before."); 
      } 

回答

0

您可以使用String.Split提取連字符分隔值,並在其上應用String.Trim之前,我們把它傳遞給int.Parse到字符串轉換爲數字把空格去掉。

new Regex(@"(\d+) - (\d+)") 

使用正則表達式的匹配的字符串您想提取的數字::這個正則表達式

//b.Tag = "0 - 1";  
string []arr = b.Tag.ToString().Split('-');  
int num1 = int.Parse(arr[0].Trim()); 
int num2 = int.Parse(arr[1].Trim()); 
+0

現在我還記得使用了類似的東西一上學期我想這正是我需要的,我要去嘗試它只要我去讓我的女朋友下班:(謝謝! – dsquaredtech

+0

我希望如此....... – Adil

+0

一切看起來像它會正常工作,但有一件事我已閱讀修剪下的塗鴉 – dsquaredtech

3

創建一個類以保存行和列值並將Tag設置爲該對象。那麼你不需要做字符串轉換。

class SeaPoint 
{ 
    public int Row { get; set; } 
    public int Column { get; set; } 
} 

負載:

 foreach (int col in Enumerable.Range(0, 20)) 
     { 
      Button b = new Button(); 
      b.Size = new System.Drawing.Size(30, 30); 
      b.Location = new Point(row * 30, col * 30); 
      gridPass[row, col] = row.ToString() + " - " + col.ToString(); 
      b.Tag = new SeaPoint() { Row = row, Column = col }; // <--- Changed. 
      b.Text = gridPass[row, col]; 
      this.Controls.Add(b); 
      b.Click += new EventHandler(AttackHandler); 
     } 

而且AttackHandler:

private void AttackHandler(object sender, EventArgs e) 
{ 
    Button clickedButton = (Button)sender; 
    var seaPoint = (SeaPoint)clickedButton.Tag; // <-- Changed 
    theSea.attackLocation(seaPoint); // rewrite attackLocation to accept a SeaPoint. 
} 
+0

你也可以使用內建的'Point','X'就是Column,'Y'將行 –

+2

@ sa_ddam213同意。或者你可以使用'int []'。關鍵是您不必傳輸字符串並在任何地方解析它們。 –

+0

我看到你說這個解決方案是我所來的那個似乎在工作的那個,所以我試了一下,但後來用這些字符串處理了所有這些問題。現在我的問題是,因爲我已經通過了attackLocation一個點按鈕的tagvalue如何使if(grid [point] == 1){MessageBox.Show(「Attacked Before」);}來檢查網格[,]獲取關於它的位置的信息 – dsquaredtech

相關問題