2016-05-31 5 views
0

내 문제는 텍스트 상자 객체에 값을 쓸 수 없다는 것입니다.배열에 저장된 텍스트 상자에 값을 쓰는 것

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"; 
    } 
} 

내가 잘못 뭐하는 거지 : 여기

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; 
} 

이 텍스트 상자에 값을 작성해야하는 방법입니다 : 여기 는 텍스트 상자가 생성 된 코드의 일부이다?

+1

변화'' "가 xValue을"''사용하려고하면 지방의 하나로서, 유명한 NullReferenceException이 트리거 글로벌 빈 떠나 초기화 문자열로 변환하려고합니다. –

+0

배열을 전역 적으로 선언 했습니까? 위의 코드는 텍스트 상자의 로컬 배열을 사용하는 것 같습니다. – Steve

+0

당신은 모든 값을 사용하지 않고''xValue yValue ''의 문자열 값을 다음과 같이 지정해야합니다. 'arrayTextBoxenHochpunkte [i, 1] .Text = xValue.ToString(); arrayTextBoxenHochpunkte [i, 2] .Text = yValue.ToString();'값 변환 사용법을 이해해야합니다. – MethodMan

답변

0

배열을 전역으로 선언 한 경우 TextBox[,] arrayTextBoxenHochpunkte 동일한 이름의 배열을 다시 로컬로 선언하지 마십시오. 이런 식으로 당신은 당신이()``xValue.ToString``에 글로벌 한

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(); 
     } 
    } 
} 
관련 문제