<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Farbe Listbox Einträge...]]></title><description><![CDATA[<p>Hi!<br />
Ich habe in der FAQ das Beispiel zum Färben der Liste gelesen, jetzt habe ich aber das Problem das meine verschiedenen Items immer die gleiche Farbe annehmen, also nie unterschiedliche...</p>
<p>Kann mir jemand sagen wie ich das machen muss??</p>
<p>Am liebsten wäre mir ja ein zusätzlichen Parameter für die Farbe bei Listbox-&gt;Items-&gt;Add, aber die Funktion werde ich mir wohl selber basteln müssen...</p>
<p>Hier ismal mein Quellcode</p>
<pre><code class="language-cpp">#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
TColor LBColors[3] = {clBlue, clGreen, clRed};
int txtcol=1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
      TRect &amp;Rect, TOwnerDrawState State)
{

   // ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[2];  // Textfarbe
  if(txtcol == 1)
  {
 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[1];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[Index]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);
    }

      if(txtcol == 2)
  {
 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[2];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[Index]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);
    }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

txtcol=2;
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
<p>Schonmal vielen vielen Dank für die Antworten!!</p>
<p>MfG Straightflush</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/169994/farbe-listbox-einträge</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 20:01:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/169994.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Jan 2007 18:49:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 18:49:32 GMT]]></title><description><![CDATA[<p>Hi!<br />
Ich habe in der FAQ das Beispiel zum Färben der Liste gelesen, jetzt habe ich aber das Problem das meine verschiedenen Items immer die gleiche Farbe annehmen, also nie unterschiedliche...</p>
<p>Kann mir jemand sagen wie ich das machen muss??</p>
<p>Am liebsten wäre mir ja ein zusätzlichen Parameter für die Farbe bei Listbox-&gt;Items-&gt;Add, aber die Funktion werde ich mir wohl selber basteln müssen...</p>
<p>Hier ismal mein Quellcode</p>
<pre><code class="language-cpp">#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
TColor LBColors[3] = {clBlue, clGreen, clRed};
int txtcol=1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
      TRect &amp;Rect, TOwnerDrawState State)
{

   // ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[2];  // Textfarbe
  if(txtcol == 1)
  {
 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[1];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[Index]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);
    }

      if(txtcol == 2)
  {
 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[2];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[Index]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);
    }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

txtcol=2;
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
<p>Schonmal vielen vielen Dank für die Antworten!!</p>
<p>MfG Straightflush</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207371</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Wed, 10 Jan 2007 18:49:32 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:05:16 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Ja du siehst doch, das in dem Event eine Farbe zugewiesen wird. Du must also nur nach einer deinen Vorstellungen entsprechenden Bedingung eine Farben auswählen.</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207391</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:05:16 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:16:58 GMT]]></title><description><![CDATA[<p>tschuldigung aber das verstehe ich jetzt beim besten Willen net... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Was heißt das konkret??</p>
<p>wenn ich Index verändere passiert au nix...<br />
ich peils einfach net, nicht den blaßesten schimmer...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207396</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207396</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:16:58 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:18:49 GMT]]></title><description><![CDATA[<p>Ab dem ersten Buttonklick bleibt <em>txtcol</em> unverändert. Und weil <em>txtcol</em> deine Bedingung ist ändert sich auch die Farbe nicht mehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207405</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207405</guid><dc:creator><![CDATA[Jansen]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:18:49 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:25:58 GMT]]></title><description><![CDATA[<p>hab den Quelltext nochmal geändert...</p>
<p>Also: der erste Eintrag ist grün, drücke ich auf dem zweiten kommt noch ein Eintrag hinzu und beide werden rot, ich möchte aber gerne das der erste weiterhin grün bleibt...<br />
Was muss ich dafür machen?</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------

#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
TColor LBColors[3] = {clBlue, clGreen, clRed};
int txtcol=1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
      TRect &amp;Rect, TOwnerDrawState State)
{

 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[txtcol];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[0]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 txtcol=2;
 ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1207409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207409</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:25:58 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:34:06 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>du hastdas Prinzip noch nicht verstanden. Der Draw-Event wird nicht einmal beim Einfügen eines Items usgeführt, und das Ergebnis bleibt dann fortan gleich.</p>
<p>Sondern der Draw-Event wird <em>jedesmal</em> ausgeführt, wenn ein Item neugezeichnet werden muß. Deshalb muß die Farbwahl nicht beim Einfügen eines Items, sondern im Event selber ausgeführt werden. Beim Einfügen kann maximal die Auswahl vorbereitet werden.</p>
<p>Du must also dafür sorgen, das auch im Event noch aus dem Item heraus festgestellt werden kann welche Farbe es hat, unabhängig davon ob außerhalb irgendwelche nicht-Item bezogenen Werte gesetzt sind.</p>
<p>Am einfachsten wäre das hier</p>
<pre><code class="language-cpp">#include &lt;vcl.h&gt;
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
const TColor LBColors[3] = {clBlue, clGreen, clRed}; 
//int txtcol=1; Keine globalen Variablen verwenden!
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
      TRect &amp;Rect, TOwnerDrawState State)
{

 ListBox1-&gt;Canvas-&gt;Font-&gt;Color = LBColors[ListBox1-&gt;ItemIndex % 3];
 ListBox1-&gt;Canvas-&gt;TextRect(Rect, Rect.Left, Rect.Top,
                           ListBox1-&gt;Items-&gt;Strings[0]);

  if(State.Contains(odFocused))
    ListBox1-&gt;Canvas-&gt;DrawFocusRect(Rect);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ListBox1-&gt;Items-&gt;Add(&quot;Text&quot;);
}
//---------------------------------------------------------------------------
</code></pre>
<p>So wird jede der drei Farben abwechselnd für die Items verwendet.</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207415</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:34:06 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 19:34:10 GMT]]></title><description><![CDATA[<p>Wenn die Zeilennummer das Kriterium sein soll dann musst du halt auch auf die Zeilennummer hin prüfen, um die aktuell zu zeichnende Farbe festzulegen.</p>
<p>Wie der Name sagt: das OnDrawItem wird für jedes Item (hier also für jede Zeile) aufgerufen, und der Index (die Zeilennummer) wird passenderweise als Parameter der Funktion übergeben.<br />
Versuch doch erst nochmal, nachzuvollziehen, was das FAQ-Beispiel genau macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207416</guid><dc:creator><![CDATA[Jansen]]></dc:creator><pubDate>Wed, 10 Jan 2007 19:34:10 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 20:03:57 GMT]]></title><description><![CDATA[<p>d.h. ich muss den parameter ändern...<br />
aber wie?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207437</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Wed, 10 Jan 2007 20:03:57 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 20:08:42 GMT]]></title><description><![CDATA[<p>Nicht ändern, auslesen bzw. benutzen. Wie im FAQ-Beispiel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207452</guid><dc:creator><![CDATA[Jansen]]></dc:creator><pubDate>Wed, 10 Jan 2007 20:08:42 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 22:33:03 GMT]]></title><description><![CDATA[<p>es macht doch nur sinn ihn &quot;auszulesen&quot; und zu &quot;benutzen&quot; wenn etwas passieren soll wenn ich auf ein Item geklickt habe...<br />
Ich will doch aber von Anfang an unterschiedliche Farben...</p>
<p>Kannst du mir vll nen kleines Beispiel coden?</p>
<p>es reicht auch schon eine Zeile, wenn sie das nötige erklärt, ich kapiers nämlich ehrlich gesagt gerade garnicht, tut mir ja au irgendwie leid weil ihr euch alle Mühe gebt mich draufzubringen was ich falsch mache...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207560</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Wed, 10 Jan 2007 22:33:03 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Wed, 10 Jan 2007 22:43:35 GMT]]></title><description><![CDATA[<p>Was genau verstehst du am FAQ-Beispiel nicht? Offenbar funktioniert es doch, ohne Klicken oder sonstwas.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207563</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207563</guid><dc:creator><![CDATA[Jansen]]></dc:creator><pubDate>Wed, 10 Jan 2007 22:43:35 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Thu, 11 Jan 2007 08:43:02 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>du befindest dich im Event <em>DrawItem</em> nicht im Event <em>OnClick</em>, somit macht es schon Sinn den Parameter <em>Index</em> &quot;auszulesen&quot;, oder zu &quot;benutzen&quot;. Er wird doch in dem Moment benötigt wenn die Funktion <em>DrawItem</em> aufgerufen wird. Und das passiert jedesmal wenn du der ListBox ein Item hinzufügst und <strong>nicht</strong> wenn du darauf klickst!</p>
<p>mfg, Micha!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207662</guid><dc:creator><![CDATA[_guest_]]></dc:creator><pubDate>Thu, 11 Jan 2007 08:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Thu, 11 Jan 2007 16:53:27 GMT]]></title><description><![CDATA[<p>was ich nicht kapiere ist:</p>
<p>der erste Eintrag wird grün gezeichnet... is ja logisch<br />
wenn jetzt der 2te Eintrag geschrieben wir habe ich die Farbe auf rot gestellt, Index müsste doch jetzt auch auf 1 stehen so das dieses neue Element rot wird, warum wird aber das erste Element dann auch rot??</p>
<p>Werden bei Index alle vorhandenen Werte bei onDraw durchlaufen??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1208119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1208119</guid><dc:creator><![CDATA[Straightflush]]></dc:creator><pubDate>Thu, 11 Jan 2007 16:53:27 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Thu, 11 Jan 2007 17:02:47 GMT]]></title><description><![CDATA[<p>OnDrawItem wird immer beim Neuzeichnen der Listbox aufgerufen. Also für alle darin vorkommenden Items. Index kennzeichnet dabei den Item der gerade gezeichnet werden soll. Du mußt also deine Farbe entsprechend dem Wert von Index in OnDrawItem anpassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1208123</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1208123</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 11 Jan 2007 17:02:47 GMT</pubDate></item><item><title><![CDATA[Reply to Farbe Listbox Einträge... on Thu, 11 Jan 2007 17:03:55 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Eine Veränderung der Daten erfordert potentiel ein neuzeichnen der gesamten Items, nicht nur des Neuen. Deshalb wird der OnDraw-Event ausgeführt, und zwar nicht nur für das neue, sondern für alle sichtbaren. Deshalb kannst du keine absoluten Farbangaben im Draw-Event verwenden, sondern die Farbe muß immer in Abhängigkeit des Items bzw. des Index sein.<br />
Du darfst den Draw-Event nicht linear und abhängig zu anderen Methoden senden. Sondern im Draw-Event must du arbeiten, <em>nur</em> auf Basis der Daten, die durch in den Parametern übergebene Itemindex repreäsentiert werden.<br />
Das bedeutet jede (itemabhängige) Farbbestimmung muß zwangsläufig mit der Auswertung des ItemIndex anfangen.</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1208125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1208125</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Thu, 11 Jan 2007 17:03:55 GMT</pubDate></item></channel></rss>