Home Ask Login Register

Developers Planet

Your answer is one click away!

Vinoth M February 2016

How to alternatively change BackColor of RTB text

I've got a requirement to change text backcolor in RTB. RTB is getting created in a loop for number of messages from DB.
I implemented a logic to achieve it but feel logic as simple.
So could you be able to give idea to implement same or mine is fine?

Code:

foreach (DataSet.NoteRow note in _ds.Note)
        {
            CreateNotes(note);
        }
private void CreateNotes(string note)
{
//txt : Rich Text Box dynamically created for each note
//test : Selected text in 'note'
int index = txt.Text.ToUpper().IndexOf(test.ToUpper());
txt.Select(index, test.Length);
txt.SelectionFont = new Font(txt.Font.Name, txt.Font.Size, txt.Font.Style^ FontStyle.Bold);
//x global variable initialised as 1
if(x % 2 == 1)
txt.SelectionBackColor = System.Drawing.Color.Gray;
x++;  }

Scenario :
If Notes has 5 messages, then alternative message should have gray backcolor.

Answers


Tanmay Nehete February 2016

try this

private void CreateNotes(string note)
{
//txt : Rich Text Box dynamically created for each note
//test : Selected text in 'note'
int index = txt.Text.ToUpper().IndexOf(test.ToUpper());
txt.Select(index, test.Length);
txt.SelectionFont = new Font(txt.Font.Name, txt.Font.Size, txt.Font.Style^ FontStyle.Bold);
 Random rnd = new Random();

//x global variable initialised as 1
if(x % 2 == 1)
txt.SelectionBackColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
x++;  }

Post Status

Asked in February 2016
Viewed 1,609 times
Voted 12
Answered 1 times

Search




Leave an answer


Quote of the day: live life