Nasdomlan Urban3p
Nasdomlan Urban3p

Reputation: 49

ListBox change the element's text color by condition

How you can change the text color of a line if, for example, the word "Error" appears in it, I can imagine how to make a check, but there is no way to change the color of one element. I would be grateful for your help!

private void LoadFTP()
{
    listBox2.Items.Clear();
    FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create("ftp://ServerIP/Logs/"+path);
    reqFTP.UsePassive = false;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("Login", "Password");
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    using (Stream stream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            listBox2.Items.Add(line);
            listBox2.TopIndex = listBox2.Items.Count - 1;
        }
        // Console.WriteLine("Upload File Complete, status {0}", responseString); 
        stream.Close();
    }
    response.Close(); //Closes the connection to the server
}

P.S. I don't understand English well, but you have a more adequate community than the Russian one.

Upvotes: 0

Views: 117

Answers (1)

Dodikaedr
Dodikaedr

Reputation: 79

You can try use ListView instead ListBox

private void FillListView()
{
    for(int i = 0; i < 10; i++)
    {
        if (i == 9)
            listView1.Items.Add(new ListViewItem($"number {i}") {ForeColor = Color.Green });
        else
            listView1.Items.Add(new ListViewItem($"number {i}"));
    }
}

But if you need exactly ListBox i think you can do it only through Draw_Item event.

Upvotes: 1

Related Questions