none
How to make a string bold?

    Question

  • I have a String named grid. How can I make parts of it bold?

    i.e

    grid += "hello";

    grid += "1234"; 

    label1.Text = grid;

    How could I make the "1234" part bold?

    Thanks

    Sunday, November 21, 2010 7:12 PM

Answers

  • Try to add this piece of code:

    label1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);

    You cannot make only a part of a string bold! You cannot color a string, you color a control, in your case this would be a label.

    The only way is to add 2 labels, in case if you want to have one normal and the other bold text.

    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 7:16 PM
  • Or another option is to use a RichTextBox. A RichTextBox does allow you to make part of its text bold or in color.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 8:36 PM
  • Like this:

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string mystring=@"my first string";
    if (richTextBox1.Find(mystring)>0))
    {
    	int my1stPosition=richTextBox1.Find(mystring);
    	richTextBox1.SelectionStart=my1stPosition;
    	richTextBox1.SelectionLength=mystring.Length; 
    	richTextBox1.SelectionFont=fnt;
    	richTextBox1.SelectionColor=Color.CadetBlue;
    } 
    
    
    
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 9:37 PM
  • hi,

    after selecting some text on richTextBox run following block code:

        private void Button1_Click(object sender, EventArgs e)
        {
    
            if (richTextBox1.SelectionFont != null)
            {
              Font currentFont = richTextBox1.SelectionFont;
              FontStyle newFontStyle = richTextBox1.SelectionFont.Style ^ FontStyle.Bold;
    
              richTextBox1.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
            }
        }


    .Net adventurer

    If (a post answers your question) {please click " Mark As Answer" on that post}  else if (that is helpfull) {"Mark as Helpful"}

    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 9:52 PM
  • I have a blog post that describes this:

    http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 10:06 PM
  • Use AppendText instead of the Text property.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Monday, November 22, 2010 2:06 AM
  • Here is an example:

    richTextBox1.AppendText(addedText) 
    
    

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:44 AM
    Monday, November 22, 2010 2:07 AM

All replies

  • Try to add this piece of code:

    label1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);

    You cannot make only a part of a string bold! You cannot color a string, you color a control, in your case this would be a label.

    The only way is to add 2 labels, in case if you want to have one normal and the other bold text.

    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 7:16 PM
  • Or another option is to use a RichTextBox. A RichTextBox does allow you to make part of its text bold or in color.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 8:36 PM
  • Thanks for the answers! but how do I change some parts of the text in a RichTextBox?
    Sunday, November 21, 2010 9:29 PM
  • Like this:

    Font fnt=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);
    string mystring=@"my first string";
    if (richTextBox1.Find(mystring)>0))
    {
    	int my1stPosition=richTextBox1.Find(mystring);
    	richTextBox1.SelectionStart=my1stPosition;
    	richTextBox1.SelectionLength=mystring.Length; 
    	richTextBox1.SelectionFont=fnt;
    	richTextBox1.SelectionColor=Color.CadetBlue;
    } 
    
    
    
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 9:37 PM
  • hi,

    after selecting some text on richTextBox run following block code:

        private void Button1_Click(object sender, EventArgs e)
        {
    
            if (richTextBox1.SelectionFont != null)
            {
              Font currentFont = richTextBox1.SelectionFont;
              FontStyle newFontStyle = richTextBox1.SelectionFont.Style ^ FontStyle.Bold;
    
              richTextBox1.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);
            }
        }


    .Net adventurer

    If (a post answers your question) {please click " Mark As Answer" on that post}  else if (that is helpfull) {"Mark as Helpful"}

    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 9:52 PM
  • I have a blog post that describes this:

    http://msmvps.com/blogs/deborahk/archive/2009/10/31/richtextbox-styles.aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Sunday, November 21, 2010 10:06 PM
  • Thanks for all the help, but now I have another problem...

    Every time I change a part of the richTextBox, it changes back when I add more text to it through code. 

    So it happens like:

    string text = Convert.ToString(1stNumber);

     

    int txtLength = richTextBox1.Text.Length;

                            richTextBox1.Text += text + "  ";

                            richTextBox1.SelectionStart = txtLength;

                            richTextBox1.SelectionLength = text.Length;

                            richTextBox1.SelectionFont = new Font("Arial", 15, FontStyle.Bold);

                            MessageBox.Show("Check"); //Changes the font.

                            richTextBox1.SelectionColor = Color.DarkSlateGray;

    MessageBox.Show("Check");  //changes the color.

    //but then here it resets to it's usual font and just adds "hello" :

    richtextBox1.Text += "hello";

    Is there a way to make it not reset the font each time I add more text through code? 

     

     


     

     

    Monday, November 22, 2010 1:41 AM
  • Use AppendText instead of the Text property.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:45 AM
    Monday, November 22, 2010 2:06 AM
  • Here is an example:

    richTextBox1.AppendText(addedText) 
    
    

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Marked as answer by NoobieMe Monday, November 22, 2010 3:44 AM
    Monday, November 22, 2010 2:07 AM
  • That worked great! Thank you!
    Monday, November 22, 2010 3:44 AM