0

I want to show Two different colors in the same textbox at the same time see the image for understanding. And the image is my sample UI design.

A and B is Two different chat messages

[The concept is when the page is loaded I'm fetching the data from Database and showing it in the textbox textmode = "multiline" and] I want to show the "A" is Green color and "B" is Red color and I tried the below code to achieve this but I'm getting the all text color as "Red" or "Green" only because it is overriding based on the last txtbox.ForeColor line execution.

ASPX :-

<asp:TextBox ID="txtbox" runat="server" TextMode="MultiLine" Width="950px" Height="510px"></asp:TextBox>

Note :- Based on my Live working code condition sometimes it will go to if condition and else condition also

C# :-

string A = "A";
string B = "B";
if(....)
{
txtbox.Text += A+" : "+"Hi";
txtbox.Text += A+" : "+"The color is Green";
txtbox.Text += A+" : "+"Ok";

txtbox.ForeColor = System.Drawing.Color.Green;
}
else
{
txtbox.Text += B+" : "+"Hello";
txtbox.Text += B+" : "+"The color is Red";
txtbox.Text += B+" : "+"Ok";

txtbox.ForeColor = System.Drawing.Color.Red;
}

Suggest me how can I achieve this?

ashok
  • 199
  • 1
  • 12
  • relatable https://stackoverflow.com/a/1178452/6271132 – mabiyan Aug 26 '22 at 10:06
  • thnaks for your comment and you given link answer is not working well for me. I used the `StringBuilder` and added required style nothing happens. – ashok Aug 26 '22 at 10:34
  • I find the relevant answer to my post [this](https://stackoverflow.com/questions/16710774/show-different-color-stringstext-in-textbox-asp-net) and in textbox showing is bit difficult using [this](https://stackoverflow.com/questions/1178249/how-to-highlight-or-change-the-color-of-some-words-in-a-label-dynamically-at-run/1178452#1178452) I used first [link](https://stackoverflow.com/questions/16710774/show-different-color-stringstext-in-textbox-asp-net) and it's working fine for me. – ashok Aug 26 '22 at 11:14
  • Only one REAL question required here. Do you need to edit the text in that box, with the colors, or is the text box JUST for display? Displaying each bit of text (even with their own font or bold or color or underline is easy). But, the REAL question do you require the text box to allow edits of that text with the color ? – Albert D. Kallal Aug 27 '22 at 00:40
  • Hi @AlbertD.Kallal thnx for your comment and I want to show only color of the text in textbox – ashok Aug 28 '22 at 12:26
  • Ah, very good then. See my example below. We can color text to our box - even on the same line if we want. – Albert D. Kallal Aug 28 '22 at 20:16

1 Answers1

0

Ok, so all we want is text box area, and some color text we can easy put into that box.

We could use a label, but BETTER is a "literal" control.

Lets drop this control onto the page - put a box around it.

we can EVEN have it scroll bar - like a multi-line text box;

so, this markup:

        <div style="width:50%;height:400px;border:solid 1px black;overflow:auto;">
            <asp:Literal ID="Literal1" runat="server">
            </asp:Literal>
        </div>

The "div" is our box - with the border. If you do NOT want a scroll bar, then remove the "overflow:auto" from above style.

So, we now run this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadLabel();
    }

    void LoadLabel()
    {
        string MyText = Literal1.Text;
        if (MyText != "")
            MyText += @"<br/>";

        // add some red text to box

        MyText += AddText("This is some red text", "red");

        MyText += AddText("This is some Blue text", "blue");

        MyText += AddText("The Matrix is watching you", "Green");
        MyText += AddText("Knock knock....", "Green");
        MyText += AddText("Follow the White Rabbit", "Green");
        MyText += AddText("", "Green");  // add blank line

        MyText += AddText("This is some red text", "Red",false);
        MyText += AddText(" and ", "Black",false);
        MyText += AddText("Some blue text on the same line", "Blue");

        Literal1.Text = MyText;

    }

    string AddText(string strText, string color,bool NewLine = true)
    {
        string FontSize = "3";
        string FontFace = "Courier New";
        string sResult = "";

        sResult =
            string.Format(@"<font size = '{0}' Color = '{1}' Face = '{2}' >{3}</font>",
            FontSize,color,FontFace,strText);

        // add new line???
        if (NewLine)
            sResult += @"<br/>";

        return sResult;
    }

So, we just build a "helper" routine in which we pass some text, the color, and "optional" if we want a new line (default).

So, run the above, and we get this:

enter image description here

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51