Valhalla Legends Forums Archive | General Programming | rtbAdd [C#] - Code for you

AuthorMessageTime
dRAgoN
[code]
public void rtbAdd(string[] args)
{
   for(i = 0; i<args.Length; i+=2)
      {
      string strText;
      string arColor;

      strText = args[i];
      arColor = args[i + 1];

      RTB.SelectionStart = RTB.Text.Length;
      RTB.SelectionLength = 0;
      if (arColor == "red") { RTB.SelectionColor = (System.Drawing.Color.Red); }
      if (arColor == "blue") { RTB.SelectionColor = (System.Drawing.Color.Blue); }
      if (arColor == "green") { RTB.SelectionColor = (System.Drawing.Color.Green); }
      RTB.SelectedText = strText;
      RTB.SelectionStart = RTB.Text.Length;
      }
}
[/code]

looks rather simple dosent it.

anyways to send your text with color here.

[code]
private void cmdRTBTest_Click(object sender, System.EventArgs e)
{
   string[] a = new string[6];
   a[0] = "Hello ";
   a[1] = "red";
   a[2] = "Blah ";
   a[3] = "blue";
   a[4] = "Argh \n";
   a[5] = "green";
   rtbAdd(a);
}
[/code]

If you look over Groks Visual Basic rtbAdd, This pretty much does the exact same except for where i set the colors "manualy", still works wonders.

Thx for that great VB function there Grok.

Spht you may add this to BotDev too if you want ^^.

~l)ragon
April 14, 2003, 3:27 AM
Etheran
[code]
public void rtbAdd(RichTextBox RTB, object[] args)
      {
         for(int i = 0; i<args.Length; i+=2)
         {
            string strText;
            Color arColor;
            
            strText = (string)args[i];
            arColor = (Color)args[i + 1];
            
            RTB.SelectionStart = RTB.Text.Length;
            RTB.SelectionLength = 0;
            RTB.SelectionColor = RTB.SelectionColor = arColor;
            RTB.SelectedText = strText;
            RTB.SelectionStart = RTB.Text.Length;
         }
      }
[/code]
this would definatly be more desirable, I'm sure.
Here's a sample of the usage:
[code]
private void button1_Click(object sender, System.EventArgs e)
   {
         object[] test = new object[3*2];
         test[0] = (object)"test";
         test[1] = (object)Color.Blue;
         test[2] = (object)"test";
         test[3] = (object)Color.Green;
         test[4] = (object)"test";
         test[5] = (object)Color.Yellow;
         rtbAdd(this.richTextBox1,test);
   }
[/code]
also
[code]
private void button1_Click(object sender, System.EventArgs e)
   {
         object[] test = { (object)"test", (object)Color.Blue, (object)"test", (object)Color.Green, (object)"test", (object)Color.Yellow };
         rtbAdd(this.richTextBox1,test);
   }
[/code]

I'm almost sure there's even a better way to do this..
April 14, 2003, 5:53 AM
K
Just posting this from memory, may not be exactly correct...
[code]// WriteTextFormatted by Kaegan
public void WriteTextF(params object[] o)
{
   Type tCurrent;
   Color cCurrent = Color.White;
   Font fCurrent = new Font("Courier New",8.25f);

   for(int i = 0; i < o.Length; i++)
   {
      tCurrent = o.GetType();

      if (tCurrent == typeof(string))
      {
         // write text to textbox
         RTB.SelectionStart = RTB.Text.Length;
         RTB.SelectionLength = 0;
         RTB.SelectionFont = fCurrent;
         RTB.SelectionColor = cCurrent;
         RTB.SelectedText = (string)(o[i]);
         RTB.SelectionStart = RTB.Text.Length;
      }
      //update color choice
      else if (tCurrent == typeof(Color))
      {
         cCurrent = (Color)(o[i]);
      }
      //update font choice
      else if (tCurrent == typeof(Font))
      {
         fCurrent = (Font)(o[i]);
      }
      // bad type! bad!
      else
      {
         throw new Exception("Unsupported Type");
      }   


   }


}

// usage:
Font f = new Font("Tahoma",12);
WriteTextF("White courier",Color.Gold,"gold courier",f, "gold tahoma", "more gold tahoma", Color.Red, "red tahoma.");
[/code]

Dragon: if you want to get the color associated with a string, why dont you use the Color.FromName() function?
April 14, 2003, 12:28 PM
dRAgoN
lol Keagan where have you been all this time 8p

I sopose ill have to look into it a little later this was like the spur of the moment thing.
April 14, 2003, 8:48 PM
Etheran
very nice, I didn't even think of using GetType and I didn't know about params array.
April 14, 2003, 10:52 PM
K
[quote author=dRAgoN link=board=5;threadid=1037;start=0#msg7848 date=1050353293]
lol Kaegan where have you been all this time 8p
[/quote]

You know...Here...There...mostly not here. ;)
April 14, 2003, 11:07 PM

Search