• Welcome to Valhalla Legends Archive.
 

rtbAdd [C#] - Code for you

Started by l)ragon, April 13, 2003, 10:27 PM

Previous topic - Next topic

l)ragon


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;
      }
}


looks rather simple dosent it.

anyways to send your text with color here.


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);
}


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
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

Etheran

#1

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;
         }
      }

this would definatly be more desirable, I'm sure.
Here's a sample of the usage:

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);
   }

also

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);
   }


I'm almost sure there's even a better way to do this..

K

#2
Just posting this from memory, may not be exactly correct...
// 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.");


Dragon: if you want to get the color associated with a string, why dont you use the Color.FromName() function?

l)ragon

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.
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

Etheran

very nice, I didn't even think of using GetType and I didn't know about params array.

K

Quote from: dRAgoN on April 14, 2003, 03:48 PM
lol Kaegan where have you been all this time 8p

You know...Here...There...mostly not here.   ;)