You must Sign In to post a response.
  • Category: ASP.NET

    Applying color and font to first line in a Multiline textbox Asp.net c#

    Guys,

    I have a textbox whose Text mode is set to MULTILINE. In code (c#) , i append some values using string builder and finally i am passing the values in the string builder to textbox. Now i want the first line in the textbox to be in Bigger font and color to differentiate from the line below.

    How can i apply the color and font style for the first line in the multiline textbox ? Ple help me to work it out.

    Thanks.
    Rajabharathi R .
  • #767934
    Hi,

    There are many ways we can do this.

    You can create a following function which we manage both color and font.


    public static class RichTextBoxExt
    {
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
    box.SelectionStart = box.TextLength;
    box.SelectionLength = 0;

    box.SelectionColor = color;
    box.AppendText(text);
    box.SelectionColor = box.ForeColor;
    }
    }


    We can fill the color and font based on our requirements.



    var userid = "USER0001";
    var message = "Access denied";
    var box = new RichTextBox
    {
    Dock = DockStyle.Fill,
    Font = new Font("Courier New", 10)
    };

    box.AppendText("[" + DateTime.Now.ToShortTimeString() + "]", Color.Red);
    box.AppendText(" ");
    box.AppendText(userid, Color.Green);
    box.AppendText(": ");
    box.AppendText(message, Color.Blue);
    box.AppendText(Environment.NewLine);

    new Form {Controls = {box}}.ShowDialog();



    The AppendText is not a predefined function. We need to write this function too.


    public static void AppendText(this RichTextBox box, string text, Color color, Font font)
    {
    box.SelectionStart = box.TextLength;
    box.SelectionLength = 0;

    box.SelectionColor = color;
    box.SelectionFont = font;
    box.AppendText(text);
    box.SelectionColor = box.ForeColor;
    }


    So in the above function we can pass Font,Color and any style you can specify.

    To make all the things to be application in First Line. Give id to that particular TH. Then while loading call this TH id with this function.

    Thanks,
    Mani

  • #767948
    if you want to color a particular text or line in textbox then you might need to use RichTextbox control, but unfortunately there is no inbuilt control exist in asp.net
    instead you need to use some third party control like FreeTextBox, Tiny MCE etc
    you can use FreeTextbox control it is most-used free ASP.NET WYSIWYG HTML editor featured in open source and commerical projects. Just drop FreeTextbox.dll in your /bin/ folder, change <asp:Textbox /> to <FTB:FreeTextbox />, and you're done.
    see below link
    http://www.freetextbox.com/
    http://www.aspdotnet-suresh.com/2011/05/richtextbox-sample-in-aspnet-or-how-to.html

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments