Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9307283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:21:38+00:00 2026-06-19T00:21:38+00:00

I’m creating a chat application and I want to display names in Bold .

  • 0

I’m creating a chat application and I want to display names in Bold . When the form is first load, I display the History conversation from database on a RichTextBox control using these lines of code and i want to display the name in Bold :

Here’s all the code to make this possible :

string strProvider = "Data Source=" + srv_host + ";Database=" + srv_db + ";User ID=" + srv_user + ";Password=" + srv_pass;
        MySqlConnection myConn = new MySqlConnection(strProvider);
        try
        {
            myConn.Open();
            string strCmd = "SELECT * FROM comments WHERE task_id=@task_id AND ((from_usr=@from AND to_usr=@to) OR (from_usr=@to AND to_usr=@from)) ORDER BY at_time ASC";
            MySqlCommand myCmd = new MySqlCommand(strCmd, myConn);
            myCmd.Parameters.AddWithValue("from", frm_usr);
            myCmd.Parameters.AddWithValue("to", to_usr);
            myCmd.Parameters.AddWithValue("task_id", tid);
            myCmd.ExecuteNonQuery(); // execute now

            MySqlDataReader dr = myCmd.ExecuteReader();

            while (dr.Read())
            {
                string text = dr.GetValue(1).ToString() + ": " + dr.GetValue(6) + Environment.NewLine;
                richTextBox1.AppendText(text);
                richTextBox1.SelectionStart = 0;
                richTextBox1.SelectionLength = dr.GetValue(1).ToString().Length;
                richTextBox1.SelectionFont = new Font(richTextBox1.Font,FontStyle.Bold);
            }

            myConn.Dispose();
        }


        catch (Exception E) { MessageBox.Show(E.Message); }

And these are the lines of code that doesn’t work as expected :

 while (dr.Read())
            {
                string text = dr.GetValue(1).ToString() + ": " + dr.GetValue(6) + Environment.NewLine;
                richTextBox1.AppendText(text);
                richTextBox1.SelectionStart = 0;
                richTextBox1.SelectionLength = dr.GetValue(1).ToString().Length;
                richTextBox1.SelectionFont = new Font(richTextBox1.Font,FontStyle.Bold);
            }

Edit:
dr.GetValue(1).ToString() Holds tha full name of user
dr.GetValue(6).ToString() Hold the message

The problem with code above is that it displays only the first name in Bold but other lines are not affected. See the image enter image description here

Could some one please tell me the reason why the code isn’t working. I couldn’t figure out where is the error.
Thank-you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:21:39+00:00Added an answer on June 19, 2026 at 12:21 am

    The problem you are having here is that richTextBox1.SelectionStart is always zero, meaning that the formatting will only ever be applied to the first line in the text box.

    Try setting this to richTextBox1.SelectionStart = richTextBox1.TextLength.

    Edit:

    Try setting it to

    richTextBox1.SelectionStart = richTextBox1.TextLength - text.Length;
    

    Edit 2:

    I think the Invalid Argument thing is caused by the use of Environment.NewLine. If I use instead "\n", the code works fine. The problem is that Environment.NewLine is of course \r\n on Windows, yet the richtextBox1 seems to be ignoring the \r. This results in richTextBox1.TextLength - text.Length being -1 in the first iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
i want to parse a xhtml file and display in UITableView. what is the
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.