2017-07-12 26 views
0

我想從一個文本文件中添加一個計算機列表到一個列表框,並根據計算機上的ping是否成功,它應該爲綠色成功或紅色失敗。問題在於它使用所有項目上最後一臺計算機的最後一種顏色。我相信這是因爲在for循環完成之前Drawitem事件不會被提出,但我想不出一個辦法解決這個問題。任何幫助表示讚賞。 (針對ping的縮減代碼)如何添加具有特定文本顏色的列表框項目?

private logitem linez; 

    struct logitem 
    { 
     public string text; 
     public Brush color; 
    }; 
private void browsebutton_Click(object sender, EventArgs e) 
    { 
string textfile = ofd.FileName; 
      if (textfile != "") 
      { 
       string[] lines = System.IO.File.ReadAllLines(@textfile); 
       foreach (string line in lines) 
       { 
        if (line != "") 
        { 
         string liner; 
         liner = line.Replace(" ", ""); 
         if (ping successfull) 
         { 
         listBoxAddGreen(); 
         } 
         else (ping fails) 
         { 
         listBoxAddRed(); 
         } 
        } 
        changeCount(); 

       } 
} 
     public void listBoxAddGreen() 
    { 
     linez.color = Brushes.Green; 
     listBox1.Items.Add(linez.text); 
    } 
        public void listBoxAddRed() 
    { 
     linez.color = Brushes.Red; 
     listBox1.Items.Add(linez.text); 
    } 

     private void listBox1_DrawItem(object sender, 
System.Windows.Forms.DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     Brush myBrush = linez.color; 
     e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), 
      e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); 
     e.DrawFocusRectangle(); 
    } 
+0

你打算使用'ListView'嗎?更換顏色更容易。 –

+0

你有繪製模式設置爲'OwnerDrawnFixed'嗎? –

+0

你必須**熟悉MVVM。使用它你的任務將在幾分鐘內解決。 1)在視圖模型中進行ping操作; 2)每個ping項目都應該有它的視圖模型(例如'ItemVM')和'PingOk'屬性來顯示ping是否成功; 3)將'ListBox'的'ItemsSource'綁定到'ItemVM'的集合; 4)根據「PingOk」屬性的值在''ListBoxItem''的XAML中設置觸發器來改變'Foreground'。對於你的任務,「ListBox」比ListView更有用,因此堅持使用這個項目控件。 – Maxim

回答

0

我玩了一下,發現我們可以用ListBox做同樣的事情。訣竅是添加一個存儲顏色和文本的項目。你這樣做,是有一個結構,我使用的是類:

class ColoredItem 
{ 
    public string Text; 
    public Color Color; 
}; 

現在,我們可以在很大程度上創造有色項目同樣是在我其他的答案,在這裏我們ping服務器,並設置顏色爲Green如果ping成功,否則將其設置爲Red。我們將Text屬性設置爲server名稱。請注意,我們只需要調用listBox1.Add(item)直接了,因爲我們依靠我們的自定義繪製方法來改變顏色:

public static bool PingServer(string serverName) 
{ 
    try { return new Ping().Send(serverName)?.Status == IPStatus.Success; } 
    catch { return false; } 
} 

private void browsebutton_Click(object sender, EventArgs e) 
{ 
    var serversFile = @"f:\public\temp\servers.txt"; 

    var servers = File.ReadAllLines(serversFile) 
     .Where(l => !string.IsNullOrWhiteSpace(l)) 
     .Select(l => l.Replace(" ", "")); 

    foreach (var server in servers) 
    { 
     var color = PingServer(server) 
      ? Color.Green 
      : Color.Red; 

     ColoredItem coloredItem = new ColoredItem {Color = color, Text = server}; 

     listBox.Items.Add(coloredItem); 
    } 
} 

現在,在我們的DrawItem方法,我們試圖將項目轉換爲我們的ColoredItem類的一個實例。請注意,DrawItemEventArgs包含我們要添加的項目的Index。當我們調用DrawString方法,我們現在可以使用我們的服務器名稱item.Text,並item.Color爲畫筆顏色:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    var item = listBox1.Items[e.Index] as ColoredItem; 

    if (item != null) 
    { 
     e.Graphics.DrawString(
      item.Text, 
      e.Font, 
      new SolidBrush(item.Color), 
      e.Bounds); 
    } 
} 

我們需要確保的是,對於ListBoxDrawMode設置爲唯一的其他東西OwnerDrawFixed,所以我們的自定義繪製代碼被稱爲:

private void Form1_Load(object sender, EventArgs e) 
{ 
    listBox1.DrawMode = DrawMode.OwnerDrawFixed; 
} 

樣本輸出

enter image description here

+0

工作很好,我認爲主要問題是在我的DrawItem方法中,該方法通過將其轉換爲ColoredItem類來解決。此外,您編寫代碼的方式更簡單,更有說服力。我現在唯一需要弄清的是如何讓結果更快顯示。 (在繪圖之前,它會等待所有物品被平鋪) –

0

如果您願意使用ListView控件,這非常容易。首先,視圖設置爲List

private void Form1_Load(object sender, EventArgs e) 
{ 
    listView1.View = View.List; 
} 

然後,您可以創建一個通用的功能,將彩色項添加到ListView。基本上都是這樣做是首先添加的項目,然後用它的索引(這將是最後一個,或Count - 1)更改前景色:

private static void AddColoredItemToListView(ListView listView, string item, Color color) 
{ 
    listView.Items.Add(item); 
    listView.Items[listView.Items.Count - 1].ForeColor = color; 
} 

既然你不包括Ping方法,我會這裏包括一個簡單的例子:

public static bool PingServer(string serverName) 
{ 
    try { return new Ping().Send(serverName)?.Status == IPStatus.Success; } 
    catch { return false; } 
} 

現在,要調用該方法,我們所做的只是讀取我們的服務器文件,並ping每臺服務器。如果調用PingServer返回true,我們設置顏色變量Green,否則我們將它設置爲Red,然後我們調用的方法我們上面創建:

private void browsebutton_Click(object sender, EventArgs e) 
{ 
    var serversFile = @"f:\public\temp\servers.txt"; 

    var servers = File.ReadAllLines(serversFile) 
     .Where(l => !string.IsNullOrWhiteSpace(l)) 
     .Select(l => l.Replace(" ", "")); 

    foreach (var server in servers) 
    { 
     var color = PingServer(server) 
      ? Color.Green 
      : Color.Red; 

     AddColoredItemToListView(listView1, server, color); 
    } 
} 

樣本輸出

enter image description here

或者,如果你願意,你可以改變BackColor代替:

enter image description here

相關問題