Announcement

Collapse
No announcement yet.

Verschiedene Farben in einer ListView

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Verschiedene Farben in einer ListView

    Ich habe zur Zeit ein Problem und wäre dankbar, wenn mir einer helfen könnte.

    Ich möchte in einer ViewList verschiedene Zeilen variabel farbig darststellen.

    Habe aber in der Online-Hilfe nichts brauchbares gefunden.

  • #2
    Hallo,

    das folgende Beispiel für eine eigene Ereignisbehandlungsmethode für das <b>DrawItem</b>-Ereignis stammt aus der .NET Framework-Hilfe (suche nach <i>DrawItemEventHandler class, samples</i>, <i>ListBox Control Sample</i>). In diesem Beispiel wird gezeigt, wie man die Darstellung kontrollieren kann, wenn die <b>DrawMode</b>-Eigenschaft der ListBox entsprechend gesetzt wird:
    <pre>
    private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {
    /*
    * The method e.drawFocusRect();
    * should be called to fill in a handy focus rectangle if this item
    * is currently selected. This call should be made AFTER all other
    * drawing, else it will get overwritten!
    * The following method should generally be called before drawing.
    * It is actually superfluous here, since the subsequent drawing
    * will completely cover the area of interest.
    */
    e.DrawBackground();
    DrawMode mode = listBox1.DrawMode;
    if (mode == DrawMode.Normal) {
    // NORMAL Draw: The control handles the drawing.
    return;
    }
    else if (mode == DrawMode.OwnerDrawFixed ||
    mode == DrawMode.OwnerDrawVariable) {
    /*
    * In either owner-draw mode, the system provides the context
    * into which the owner custom-draws the required graphics.
    * The context into which to draw is e.graphics.
    * The index of the item to be painted is e.index.
    * The painting should be done into the area described by e.rect.
    */
    Brush brush = new SolidBrush(listBoxColors[e.Index]);
    e.Graphics.FillRectangle(brush, e.Bounds);
    e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
    bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
    /*
    * The ul-corner of the text's bounding box is placed on the
    * point specified in the drawString call.
    * The '+1's are to leave the background border as drawn above.
    */
    string selText = selected ? "SEL." : "";
    e.Graphics.DrawString("Bmp Nummer" + e.Index.ToString() + " " + selText, Font, new SolidBrush(Color.Black),
    e.Bounds.X + 1, e.Bounds.Y + 1);
    /*
    * After all other drawing is complete, the next method will
    * add a rectangle that indicates whether this item has the
    * focus.
    */
    e.DrawFocusRectangle();
    }
    }
    </pre&gt

    Comment


    • #3
      Vielen Dank,
      das hat mir auf jedenfall weitergeholfen.

      Darauf hätte ich auch selber kommen können, wenn ich mir die Events mal genauer angeschaut hätte. ;-

      Comment

      Working...
      X