views:

42

answers:

2

Hy! I would like to make a logging system with a listbox, that highlights some of my lines, depends on my will :P So, if I have 4 kind of reporting, like, general, warning, error, debug.

I add with somelistbox.Items.Add("Starting"); <-- I would like to drawn this as grey somelistbox.Items.Add("Error!"); <-- I would like to drawn this as red But there is no way to do it, because I dont want to color my lines , based on item number. I want to color them based on logtype.

So I would like to add a new thing, like typeoflog, but I dont know how to do it. I mean somelistbox.Items.Add("Error!",Type.Error);

I've got this code, which colors items , depends on item number, but Thats not what I'm looking for.

    private void general_log_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        Brush myBrush = Brushes.Black;
        switch (actualLogType)
        {
            case LogTypes.General:
                myBrush = Brushes.Black;
                break;
            case LogTypes.Warning:
                myBrush = Brushes.Orange;
                break;
            case LogTypes.Error:
                myBrush = Brushes.Purple;
                break;
            case LogTypes.Debug:
                myBrush = Brushes.AntiqueWhite;
                break;
        }
        e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
A: 

If it is not WPF listbox, you need to use Win32 API. Is that what you are after?

UPDATE

Using WIn32 API is not necessary: http://www.codeproject.com/KB/combobox/ColorListBoxIcons.aspx

Aliostad
Its not WPF listbox. How to do that?
Dominik
Well, there is a managed way as well, look at this: http://www.codeproject.com/KB/combobox/ColorListBoxIcons.aspx
Aliostad
+1  A: 

A ListBox can store more than just strings, it can also store objects. You want to take advantage of that here, the list item in your case has more state than just the text. Add a little nested helper class:

    private enum itemType { error, warning }

    private class listObject {
        public listObject(string txt, itemType type) { Text = txt; Type = type; }
        public string Text;
        public itemType Type;
        public override string ToString() { return Text; }
    }

Note how the ToString() override generates the text that the user sees. Now just add items to the listbox like this:

        listBox1.Items.Add(new listObject("blah", itemType.warning));

And cast the object back to your class in the DrawItem event handler:

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
        var obj = listBox1.Items[e.Index] as listObject;
        // etc..
    }

And use obj.Type to determine colors.

Hans Passant
This is what Iam looking for. Will try, thank you!!
Dominik
Your code is freaking cool, but I cant get it to work yet. Could you write me the whole DrawnItem method please? Thank you very very much!here is how I try it http://pastie.org/1175240
Dominik
You do actually have to use a different brush if you want a different color. Use switch(obj.Type) {} to create it. Just like you did it in your original code snippet.
Hans Passant
Iam getting crash on the obj declatarion line =/ http://pastie.org/1175285
Dominik
You need to check e.Index and only do the custom drawing code if the value >= 0. Add an if() statement.
Hans Passant
Hmm, I debugged it now, and the problem is, that the DrawnItem event is not called when I click my button :/ private void bot_stop_Click(object sender, EventArgs e) { general_log_add_item("ASDASD", Logging.LogTypes.General); }
Dominik
Well, the item is not visible perhaps. Scroll the list box. Please start a new question, this is getting pretty far removed from the original one.
Hans Passant