2014-02-24 57 views
0

我試圖顯示條碼掃描器的掃描輸出的特定字符串。我已將條形碼掃描器配置爲在每個掃描的條形碼末尾放置一個$。之後,我嘗試在富文本框中顯示過濾的輸出。爲此使用按鍵事件。這是現在發生的事情。我的掃描器讀取條形碼開始鍵入,最後當掃描器按$鍵觸發$ asci的按鍵事件時,我會過濾我的掃描器輸出以顯示richtextbox中的最終結果。但是,當我調試前綴$符號不顯示在相同的事件函數時,我從文本框中獲取文本中的變量字符串。 這是我的代碼:請參閱評論。我已更新我的代碼以顯示代碼行的調試輸出無法從richtextbox中刪除前綴字符

private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e) 
{   
    if (e.KeyChar == (char)36) 
    { 
     String newstring; 

     var str = richTextBox1.Text; 
     int numlines = str.Split('\n').Length; 
     var strArr = str.Split(Environment.NewLine.ToCharArray()); 
     if (numlines > 2) 
     { 
      if (strArr[2].Length == 6) 
      { 
       var final = strArr[1]; 
       newstring = final.Remove(final.Length - 2, 2); 
       richTextBox1.Text = newstring; 
       String test = richTextBox1.Text; ////shows 3740588609843 not $3740588609843 
       int len = richTextBox1.Text.Length; 
       // test = test.Remove(test.Length - 14, 1); //removes 3 rather than $ 
       richTextBox1.Text = test; //result displayed is $740588609843 
       cnic = newstring; 
      } 
      if (strArr[3].Length == 6) 
      { 
       var final = strArr[2]; 
       richTextBox1.Text = final; 
       cnic = final; 
      } 
     } 
     if (numlines < 3) 
     { 
      if (strArr[0].Length == 25) 
      { 
       var final = strArr[0]; 
       newstring = final.Remove(0, 12); 
       richTextBox1.Text = newstring; 
       cnic = newstring; 
      } 
     } 
     if (cnic == null) 
     { 
      MessageBox.Show("Please scan the id card through scanner again!"); 
     } 

    } 
} 

爲什麼發生這種情況?我怎樣才能糾正這?我想刪除$跡象,但儘管它存在於在RichTextBox

回答

0

起始碼沒有檢測到它試試這個:

richTextBox1.Text = test.TrimStart('$'); //result displayed is $740588609843 
+0

的一點是它沒有拿起$符號。試過。沒有工作! – user3345361

+0

在調試中沒有在richtextbox.text中顯示$符號,但在程序中顯示它 – user3345361

+0

@ user3345361:您是否在使用此語句檢查$符號 - > if if(e.KeyChar ==(char)36)'? –