How can i DrawString each line in another color ?


rule simple:

first line in red second line in green third line in red. have variable called m_text , it's format this:

first line in red second line time&date in green. there empty/space line third line in red fourth line in green empty/space line. , same lines. first in red second in green , each line in red , green red green red green.

variable _colors color[]

in usercontrol code top did:

color[] _colors;  string[] m_text = new string[0];


then:

public void scrollercolors(color[] colors)          {              _colors = colors;          }


then:

public color[] colorlines          {                           {                  return this._colors;              }              set              {                  this._colors = value;              }          }


text scroll:

public string texttoscroll          {                            {                  return string.join("\n", m_text);              }              set               {                  string buffer = value;                    m_text = buffer.split(new char[1] { '\n' });              }          }

then inside onpaint event have:

private void onpaint(object sender, painteventargs e)          {              e.graphics.smoothingmode = smoothingmode.antialias;                e.graphics.fillrectangle(new solidbrush(this.backcolor), this.clientrectangle);                int visiblelines = 0;                font drawfonts1 = new font("arial", 20, fontstyle.bold, graphicsunit.pixel);                  (int = m_text.length - 1; >= 0; i--)              {                    point pt = new point((int)((this.clientsize.width - e.graphics.measurestring(m_text[i], m_font).width) / 2),                      (int)(m_scrollingoffset + this.clientsize.height - (m_text.length - i) * m_font.size));                    if ((pt.y + this.font.size > 0) && (pt.y < this.height))                  {                      visiblelines++;                  }                  if (_colors != null)                  {                      e.graphics.drawstring(m_text[i], drawfonts1, new solidbrush(_colors[i % _colors.length]), pt);                  }              }


drawstring line color lines in m_text in red , green.

, how i'm using in form1:

in top of form1:

scroller1.texttoscroll = combindedstring;  listcolors();


combindedstring text in m_text.

in form1 in bottom have method:

private void listcolors()          {              list<color> allcolors = new list<color>();              knowncolor[] colors = (knowncolor[])enum.getvalues(typeof(knowncolor));              foreach (knowncolor knowcolor in colors)              {                  color color = color.fromknowncolor(knowcolor);                  allcolors.add(color);              }              scroller1.colorlines = new color[] { color.red, color.green };          }

the result after using drawstring is:


drawstring color first line in red second line in green space/empty line fourth line in in green should in red.

should this:

text line ( first line ) in red date&time line in green. empty line.... text line ( fourth line ) in red date&time line in green.

how can fix ?

*** tried t hings since of lines empty should not try color them think:

changed in onpaint event:

f (_colors != null)                  {                      if (m_text[i].length > 0)                      {                          coloring += 1;                      }                      e.graphics.drawstring(m_text[i], drawfonts1, new solidbrush(_colors[coloring % _colors.length]), pt);                  }

coloring int.

still im getting 2 lines in red 2 in green.

tried vincent solution:

if (m_text[i] == string.empty)                      {                      }                      else                      {                          e.graphics.drawstring(m_text[i], drawfonts1, new solidbrush(_colors[i % _colors.length]), pt);                      }





"but checked case there 6 lines 2 of them spaces/empty lines , same problem. it's not sure spaces problem."

well, checked following 9 lines , both solutions worked fine me:

m_text = new[] { "foo", "42", "", "bar", "48", "", "boo", "13", "" };

"mike maybe can , check , see on project here:"

that project appears different, matches question in other thread.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to Share webservice object to all user