2011-11-29 45 views
0

我有這個程序正在從textbox讀取測試字符串,並將其轉換爲字符數組,將化妝數據顯示在屏幕上。我越來越近了。該代碼當前可以拖動文本,將其轉換爲char數組,然後將字節數組中的零用來自包含5位的2維數組的有用數據替換爲字母表中的所有字母。但我有一個問題。代碼似乎只運行一次。如果我第二次點擊按鈕,最終會出現「未處理的indexOutOfRange異常」。另外,它似乎只能在一個字母上工作使用字符串從查找表中生成數據的字節數組

EX:如果我輸入「A」,它會顯示,但是如果我輸入「AA」,我會得到相同的錯誤。 這裏是WordArray []

byte[,] Letters = new byte[18, 5] { { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 }, 
             { 63, 72, 72, 63, 0 }, 
             { 127, 73, 73, 54, 0 } }; 

這裏是click_button方法:

int Aindex = 0; 
    int LettersIndex = 0; 
    private void SendButton_Click(object sender, EventArgs e) 
    { 
     WordIndex = 0; 
     if (Aindex > 0) 
     { 
      Aindex = 0; 
     } 
     string CurrentTextString = textBox1.Text; 
     char[] charArray = CurrentTextString.ToCharArray(); 
     if (textBox1.Text == "") 
     { 

     } 
     else 
     { 
      foreach (char c in charArray) 
      { 
       int index = 0; 
       CharAsciiArray[index] = Convert.ToChar((Convert.ToInt32(c))); 
       textBox2.Text += CharAsciiArray[index] + " "; 
       charCount++; 
       index++; 
      } 
      for (int NumberofBytes = 0; NumberofBytes < charCount; NumberofBytes++) 
      { 

       LettersIndex = 0; 
       // int currentChar = CharAsciiArray[index] - 65; 
       //textBox2.Text += currentChar; 
       int currentCharAscii = (CharAsciiArray[Aindex]); 
       int currentChar = currentCharAscii - 'A'; 
       for (int NumberofBits = 0; NumberofBits < 5; NumberofBits++) 
       { 


        // textBox2.Text += currentChar; 
        WordArray[WordIndex + 3] = Letters[currentChar, LettersIndex]; 
        textBox2.Text += WordArray[WordIndex] + " "; 
        LettersIndex++; 
        WordIndex++; 
       } 
       Aindex++; 
      } 
      SendingData = true; 
      //SendNextByte(); 

      serialPort1.Write(WordArray, 0, WordArray.Length); 
     } 
    } 
+0

哪一行導致異常? – okrumnow

+0

包含「WordArray [WordIndex + 3] = Letters [currentChar,LettersIndex];」的行正在輸出錯誤 –

+0

你在哪裏聲明'WordArray []'?併爲其分配內容? – Flanfl

回答

0

在下面的循環

foreach (char c in charArray) 
    { 
     int index = 0; 
     CharAsciiArray[index] = Convert.ToChar((Convert.ToInt32(c))); 
     textBox2.Text += CharAsciiArray[index] + " "; 
     charCount++; 
     index++; 
    } 

你顯然希望增加在每次迭代的索引,但你每次都將索引重置爲0。

除此之外,請在命名變量時確定要遵循的模式。例如:

爲什麼AIndex不是AnIndex,您將如何命名下一個索引? AnotherIndex?它需要成爲全球性的嗎?爲什麼charArray以小寫字母c開頭,NumberOfBytes以大寫字母N?編寫代碼就好像你必須向你的妻子/丈夫解釋它(誰知道?),並且維護/調試會更容易。

+0

哇,我不能相信我錯過了這個!謝謝!!!! –

相關問題