2016-05-31 19 views
0

私の問題は、テキストボックスオブジェクトに値を書き込めないということです。ここ は、テキストボックスが作成されたコードの一部である:ここで配列に格納されたテキストボックスに値を書き込む

int number = 10; 

TextBox[,] arrayTextBoxenHochpunkte= new TextBox[10,3]; 
for(int i=0; i<number; i++) 
{ 
    TextBox newTextBox = new TextBox(); 
    newTextBox.Location = new Point(0, (3+(i * 25))); 
    newTextBox.Size = new Size(25, 26); 
    newTextBox.Text = "H" + Convert.ToString(i + 1); 
    newTextBox.ReadOnly = true; 
    panel1.Controls.Add(newTextBox); 

    arrayTextBoxenHochpunkte[i, 0] = newTextBox; 

    TextBox newTextBox2 = new TextBox(); 
    newTextBox2.Location = new Point(28, (3+(i * 25))); 
    newTextBox2.Size = new Size(50, 26); 
    newTextBox2.ReadOnly = true; 
    panel1.Controls.Add(newTextBox2); 

    arrayTextBoxenHochpunkte[i, 1] = newTextBox2; 

    TextBox newTextBox3 = new TextBox(); 
    newTextBox3.Location = new Point(83, (3+(i * 25))); 
    newTextBox3.Size = new Size(50, 26); 
    newTextBox3.ReadOnly = true; 
    panel1.Controls.Add(newTextBox3); 

    arrayTextBoxenHochpunkte[i, 2] = newTextBox3; 
} 

は、テキストボックスに値を書き込む必要がある方法です。私は間違っ

private void addHochpunkt(float xValue, float yValue) 
{ 
    int i; 
    for (i = 0; i < 10; i++) 
    { 
     if (!(string.IsNullOrWhiteSpace(arrayTextBoxenHochpunkte[i,1].Text))) 
     { 
     continue; 
     } 
     arrayTextBoxenHochpunkte[i, 1].Text = "xValue"; 
     arrayTextBoxenHochpunkte[i, 2].Text = "yValue"; 
    } 
} 

何をしているのですか?

+1

変更 ''「はxValueを」 ''を使用しようとすると、ローカルの1のように、有名なとNullReferenceExceptionをトリガするグローバルな空のままに初期化しますそれを文字列に変換したい –

+0

配列をグローバルに宣言しましたか?上記のコードは、テキストボックスのローカル配列を使用しているようです。 – Steve

+0

'' xValue yValue "の文字列値をすべて代入しているだけで値を使用していません。 ' arrayTextBoxenHochpunkte [i、1] .Text = xValue.ToString(); arrayTextBoxenHochpunkte [i、2] .Text = yValue.ToString(); '値の変換の使い方を理解する必要があります – MethodMan

答えて

0

グローバルに配列TextBox[,] arrayTextBoxenHochpunkteを宣言した場合は、同じ名前の配列をローカルに宣言し直すことはありません。そのように、あなたが() `` `` xValue.ToStringにグローバル1つの

public class Form1 : Form 
{ 
    private TextBox[,] arrayTextBoxenHochpunkte; 

    public void SomeMethod() 
    { 
     int number = 10; 
     // This line declares an array with the same name and thus 
     // hides the global one declared at the form/class level 
     // All your initialization goes into this local one that is 
     // lost when you exit from this method..... remove it... 
     // TextBox[,] arrayTextBoxenHochpunkte= new TextBox[10,3]; 

     // This line initializes the global array so it is available 
     // also in other methods..... 
     arrayTextBoxenHochpunkte= new TextBox[10,3]; 
     ..... 
     initialization code 
     ..... 

    } 

    private void addHochpunkt(float xValue, float yValue) 
    { 
     int i; 
     for (i = 0; i < 10; i++) 
     { 
      // Here you try to reference the global array, but 
      // your current code leaves the global one without initialization 
      // thus a NullReferenceException..... 
      if (!(string.IsNullOrWhiteSpace(arrayTextBoxenHochpunkte[i,1].Text))) 
      { 
      continue; 
      } 
      // I suppose that you want to write the value 
      // of the two variables not their names. 
      arrayTextBoxenHochpunkte[i, 1].Text = xValue.ToString(); 
      arrayTextBoxenHochpunkte[i, 2].Text = yValue.ToString(); 
     } 
    } 
} 
関連する問題