2011-05-11 72 views
0

我是一個安全關鍵系統的嵌入式系統軟件開發人員,所以我對C#非常熟悉,但熟練掌握基於C的語言。爲了提供一點背景知識,我開發了一個Windows窗體,它將通過串行端口從我的嵌入式軟件發送的串行數據包解釋爲有意義的調試信息。C#文本框控件不用新文本更新

我想要做的是在TextBox控件中顯示每個數據包的每個字節。顯示數據包信息的文本框控件實際上是第一種形式打開的第二種形式。下面是打開從第一第二形式的事件處理程序代碼:

private void ShowRawSerialData(object sender, EventArgs e) 
{ 
    SendSerialDataToForm = true; 
    if (SerialStreamDataForm == null) 
     SerialStreamDataForm = new RawSerialDataForm(); 
    SerialStreamDataForm.Instance.Show(); 
} 

在上面的代碼中,.Instance.Show()指令是由我可以如果窗體打開一個新的形式的裝置已關閉,但如果表單已經打開,則不會顯示新表單。 然後,在接收到的事件處理程序我的串行數據,我這樣做:

// Get bytes from the serial stream 
bytesToRead = IFDSerialPort.BytesToRead; 
MsgByteArray = new Byte[bytesToRead]; 
bytesRead = IFDSerialPort.Read(MsgByteArray, 0, bytesToRead); 
// Now MsgByteArray has the bytes read from the serial stream, 
// send to raw serial form 
if (SendSerialDataToForm == true && SerialStreamDataForm != null) 
{ 
    SerialStreamDataForm.UpdateSerialDataStream(MsgByteArray); 
} 

哪裏MsgByteArray是串行數據包的字節數組好評。這裏是代碼UpdateSerialDataStream:

public void UpdateSerialDataStream(Byte[] byteArray) 
{ 
    String currentByteString = null; 
    currentByteString = BitConverter.ToString(byteArray); 
    currentByteString = "0x" + currentByteString.Replace("-", " 0x") + " "; 
    if (RawSerialStreamTextBox.InvokeRequired) 
    { 
     RawSerialStreamTextBox.Invoke(new SerialTextBoxDelegate(this.UpdateSerialDataStream), new object[] { byteArray }); 
    } 
    else 
    { 
     RawSerialStreamTextBox.Text += currentByteString; 
    } 
    RawSerialStreamTextBox.Update(); 
} 

最終的結果是,RawSerialStreamTextBox.Text的值是否正確與我打算就增加了文本框中的字符串更新!例如,如果我傳遞字節數組{0x01,0x7F,0x7E},那麼通過調試器,我可以看到RawSerialStreamTextBox.Text =「0x01 0x7F 0x7E」的值。

問題是文本框控件本身不顯示新添加的文本。所以,儘管我可以通過調試器確認RawSerialStreamTextBox.Text =「0x01 0x7F 0x7E」,但Windows中的文本框並未顯示「0x01 0x7F 0x7E」,而是保持空白。

任何想法可能發生在這裏?

回答

0

爲了簡化一點,我會UpdateSerialDataStream返回一個字符串(或字符串傳遞給out參數)讓你的事件處理程序會,相反,是這樣的:

// Get bytes from the serial stream 
    bytesToRead = IFDSerialPort.BytesToRead; 
    MsgByteArray = new Byte[bytesToRead]; 
    bytesRead = IFDSerialPort.Read(MsgByteArray, 0, bytesToRead); 
    // Now MsgByteArray has the bytes read from the serial stream, 
    // send to raw serial form 
    if (SendSerialDataToForm == true && SerialStreamDataForm != null) 
    { 
     RawSerialStreamTextBox.Text = UpdateSerialDataStream(MsgByteArray); 
    } 

而且UpdateSerialDataStream會看是這樣的:

public string UpdateSerialDataStream(Byte[] byteArray) 
{ 
    String currentByteString = null; 
    currentByteString = BitConverter.ToString(byteArray); 
    currentByteString = "0x" + currentByteString.Replace("-", " 0x") + " "; 
    return currentByteString; 
} 

你必須將你的代碼處理周圍一點點形式的顯示,但這樣會使表格已經包含文本框來處理自身的更新。

1

我猜你正在設置一個實際顯示的實例以外的Text屬性。健康檢查將會像

RawSerialStreamTextBox.Visible = false; 

它會消失嗎?