2013-06-01 102 views
0

我的朋友和我一直在比賽,我們有一個跟蹤偵聽器設置,這裏的一些代碼:我不知道如果我的遊戲工作尚未

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Windows.Forms; 
using System.Diagnostics; 

namespace GuiGame { 

/// <summary> 
/// A type of Trace Listener that sends its output to a ListBox. 
/// </summary> 
public class ListBoxTraceListener : TraceListener { 

    private ListBox listBox; // A reference to the listbox that we're writing to. 

    private string stringToAddToListBox = ""; 

    private const char NEW_LINE = '\n'; 

    /// <summary> 
    /// Parameterless constructor. 
    /// Do not want the generic default constructor to be used 
    /// as there is no way to set the ListBoxTraceListener's data. 
    /// This replaces the compiler's generic default constructor. 
    /// Pre: none 
    /// Post: ALWAYS throws an ArgumentException. 
    /// </summary> 
    /// <remarks>NOT TO BE USED!</remarks> 
    public ListBoxTraceListener() { 
     throw new ArgumentException("Parameterless constructor invalid."); 
    } // end ListBoxTraceListener constructor 

    /// <summary> 
    /// Constructor with initialising parameters. 
    /// Pre: the existence of a ListBox on a GUI form. 
    /// Post: initialised object. 
    /// </summary> 
    /// <param name="listBox">The ListBox that we're writing to.</param> 
    public ListBoxTraceListener(ListBox listBox) { 
     this.listBox = listBox; 
    } 

    /// <summary> 
    /// Automatically collects the outputs from all Trace.WriteLine statements. 
    /// Pre: none. 
    /// Post: the string s is displayed in the listBox. 
    /// </summary> 
    /// <param name="s"></param> 
    public override void WriteLine(string s) { 
     Write(s + NEW_LINE); 
    } //end WriteLine 

    /// <summary> 
    /// Automatically collects the outputs from all Trace.Write statements. 
    /// Pre: none. 
    /// Post: the string s is displayed in the listBox, once we receive a NEW_LINE. 
    /// </summary> 
    /// <param name="s"></param> 
    public override void Write(string s) { 
     stringToAddToListBox += s; 

     // If we have one or more complete lines 
     if (stringToAddToListBox.Contains (NEW_LINE)) { 

      // Split the string into multiple lines. 
      // If NEW_LINE is found at the beginning or end of the string, 
      // then the corresponding array element contains an empty string. 
      string[] lines = stringToAddToListBox.Split(NEW_LINE); 

      // Add all the lines to the listbox, except for the last one. 
      // When stringToAddToListBox has a new-line at the end, 
      // the last element in lines[] will be an empty string. 
      int highestLineNumber = lines.Length - 1; 
      for (int i = 0; i < highestLineNumber; i++) { 
       AddToListBox(lines[i]); 
      } 

      // Reset stringToAddToListBox to what remains. (May be an empty string). 
      stringToAddToListBox = lines[highestLineNumber]; 
     } 
    } // end Write 

    /// <summary> 
    /// Adds a complete output-line to the ListBox. 
    /// Pre: none. 
    /// Post: the string listBoxLine is displayed in the listBox . 
    /// </summary> 
    /// <param name="listBoxLine"></param> 
    private void AddToListBox(string listBoxLine) { 
     Debug.Assert(listBox != null, "listBox != null"); 
     listBox.Items.Add(listBoxLine); 
    } // end AddToListBox 
} 

}

在這個階段,我們只是想使用跟蹤監聽器輸出一些文字上的ListBox所以我們知道這是工作,所以我們有一個事件處理程序設置:

private void RollDiceButton_Click(object sender, EventArgs e) 
{ 

} 

我們未能得到任何來自跟蹤監聽器的輸出。作爲跟蹤監聽器的Add方法沒有爲此設置。任何人都可以提供一些建議嗎?我想也許我們正在做一些非常愚蠢而明顯的事情,我們錯過了。

+0

拋出一個無參數構造函數的異常對我來說並不合適。實現一個帶參數的構造函數,並讓編譯器指出如果有人試圖用0參數創建一個新實例時出現錯誤。 –

回答

0

問題最可能的原因是您的應用程序是單線程的(因爲大多數簡單的Windows應用程序都是)。這意味着,雖然您要將消息發送到列表視圖以將新元素附加到列表中,但由於您的原始處理程序尚未返回(RollDiceButton_Click),所以仍不處理消息

來解決這個你身邊,你應該列表本身從目前的處理程序中刷新:

private void AddToListBox(string listBoxLine) { 
    Debug.Assert(listBox != null, "listBox != null"); 
    listBox.Items.Add(listBoxLine); 
    // this would help? 
    listBox.Refresh(); 
} // end 

如果這沒有幫助,請嘗試暫時切換到所有未完成的事件無條件處理

private void AddToListBox(string listBoxLine) { 
    Debug.Assert(listBox != null, "listBox != null"); 
    listBox.Items.Add(listBoxLine); 
    // this would help? 
    Application.DoEvents(); 
} // end 

並回報。

相關問題