none
How to get Different Font Colors in TextBox and Arrange in Ascending/descending order?

    Question

  • Dear Friends,

    I am giving an example to explain what I am trying to do.

    A TextBox has the Following Words; " ABC : 12" , "EGF : 15" and "JUT : 02".

    I want to have "ABC : 12" to have Red color Font, "EGF : 15" - Blue Font and "JUT : 02" to have Green Font.

    Is it possible to do this?
    Changing the "Fore Color" in the "Property" of the TextBox changes the color of all the Texts to one color.

    The other problem I am having is (how) to get these sorted according to the Number shown with the ABC, EGF etc,. They should be arranged either in ascending order or descending order.
    Each one is displayed as a String ("ABC : 12" is a String but "ABC" is a String and "12" is a Double converted to String).

    Please help.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 8:23 AM

Answers

  • OK.  The functions themsleves probably aren't that important, but understanding the data that you are actually manipulating is.  The answer is probably going to involve creating a structure that contains the associated data elements, then identifying which ones are involved in defining the sequence for sorting purposes, and constructing a routine that sorts an array of those structures according using the structure data elements that control the sequence.
    • Marked as answer by V.K.S.B.K Saturday, March 13, 2010 4:21 AM
    Thursday, March 11, 2010 10:51 PM
  • Hi V.K.S.B.K_ ,


    With two buttons and one RichTextBox on a FORM try this code please.

    I entered the following into the RichTextBox.>>

    12 23'34"
    12 23'30"
    12 23'32"


    Which represent;

    12 degrees, 23 minutes and 34 seconds then
    12 degrees, 23 minutes and 30 seconds then
    12 degrees, 23 minutes and 32 seconds.

    Do not hit ENTER on the last line otherwise you get an empty line when you click on the "Sort ascending " button.

    See my RecolorText SUB for an example of recoloring the text.


    Regards,

    John

    Option Strict On
    Imports System.Environment
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.Size = New Size(75, 40)
            Button2.Size = New Size(75, 40)
            Button1.Text = "Sort" & NewLine & "ascending."
            Button2.Text = "Sort" & NewLine & "descending."
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim textLines() As String = RichTextBox1.Lines
            Array.Sort(textLines)
            RichTextBox1.Lines = textLines
            ReColorText(RichTextBox1)
    
        End Sub
    
        Private Sub ReColorText(ByVal aRichTextBox As System.Windows.Forms.RichTextBox)
    
            Dim index As Integer = 0
            Dim currentColor As Color = Color.Red
            aRichTextBox.SelectionColor = currentColor
    
            Do
                aRichTextBox.SelectionStart = index
                aRichTextBox.SelectionLength = 1
                If aRichTextBox.SelectedText = " " Then currentColor = Color.Green
                If aRichTextBox.SelectedText = "'" Then currentColor = Color.Blue
                If aRichTextBox.SelectedText = vbLf Then currentColor = Color.Red
                aRichTextBox.SelectionColor = currentColor
                index += 1
            Loop Until index = aRichTextBox.Text.Length
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim textLines() As String = RichTextBox1.Lines
            Array.Sort(textLines)
            Array.Reverse(textLines)
            RichTextBox1.Lines = textLines
            ReColorText(RichTextBox1)
    
        End Sub
    
    End Class


    • Marked as answer by V.K.S.B.K Saturday, March 13, 2010 4:20 AM
    Friday, March 12, 2010 12:44 AM

All replies

  • Dear Friends,

    I am giving an example to explain what I am trying to do.

    A TextBox has the Following Words; " ABC : 12" , "EGF : 15" and "JUT : 02".

    I want to have "ABC : 12" to have Red color Font, "EGF : 15" - Blue Font and "JUT : 02" to have Green Font.

    Is it possible to do this?
    Changing the "Fore Color" in the "Property" of the TextBox changes the color of all the Texts to one color.

    The other problem I am having is (how) to get these sorted according to the Number shown with the ABC, EGF etc,. They should be arranged either in ascending order or descending order.
    Each one is displayed as a String ("ABC : 12" is a String but "ABC" is a String and "12" is a Double converted to String).

    Please help.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !
    • Merged by Liliane Teng Thursday, March 11, 2010 8:51 AM the duplicate thread
    Wednesday, March 10, 2010 8:17 AM
  • use richtextbox. and use its properties such as selectionstart for starting the selection for marking text for font color and set the selectionlength and mark the color for text as you like

    hope it helps

    Please mark the answer as Correct if applicable
    • Proposed as answer by Cor LigthertMVP Wednesday, March 10, 2010 10:15 AM
    • Marked as answer by V.K.S.B.K Wednesday, March 10, 2010 12:20 PM
    • Unmarked as answer by V.K.S.B.K Wednesday, March 10, 2010 1:24 PM
    • Unproposed as answer by V.K.S.B.K Wednesday, March 10, 2010 2:56 PM
    Wednesday, March 10, 2010 8:48 AM
  • one time is enough, and this question is endless answered.

    Use a RichTextbox.

    the Textbox has no colours fonts or whatever.


    Success
    Cor
    Wednesday, March 10, 2010 10:16 AM
  • You can't put different colored text in a text box.

    You can draw the text into a picture box using any color you want.  

            Dim BMP As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
            
    Dim g As Graphics = Graphics.FromImage(BMP)
            g.Clear(Color.White)
            g.DrawString(
    "ABC : 12"Me.Font, Brushes.Red, 0, 0)
            g.DrawString(
    "EGF : 15"Me.Font, Brushes.Blue, 0, 15)
            g.DrawString(
    "JUT : 02"Me.Font, Brushes.Green, 0, 30)
            PictureBox1.Image = BMP

    Or you can print the text into a rich text box, nominating the font color for each line. But to do this you need code that selects the text you want to color before selectng the color, and then you have to make sure that the text insertion point is restored properly.  The easiest way to sort your lines is to put them into an array and sort the array.  That also simplifies printing them as different colors, as you can print the lines to the rich text box in a loop.  Note that I have intitialised the array in an odd order so you can see the sort actually doing something, and I have hard-coded which lines should be which color.

            Dim Lines(2) As String
            Lines(0) = "EGF : 15"
            Lines(1) = "JUT : 02"
            Lines(2) = "ABC : 12"
            Array.Sort(Lines)
            RichTextBox1.Text = 
    ""
            For I As Integer = 0 To 2
                
    Dim P As Integer = RichTextBox1.SelectionStart
                
    Dim Q As Integer = Lines(I).Length
                RichTextBox1.AppendText(Lines(I) & vbCrLf)
                RichTextBox1.Select(P, Q)
                
    If I = 0 Then RichTextBox1.SelectionColor = Color.Red
                
    If I = 1 Then RichTextBox1.SelectionColor = Color.Blue
                
    If I = 2 Then RichTextBox1.SelectionColor = Color.Green
                RichTextBox1.SelectionStart = RichTextBox1.TextLength
            
    Next

    • Marked as answer by V.K.S.B.K Wednesday, March 10, 2010 12:19 PM
    • Unmarked as answer by V.K.S.B.K Wednesday, March 10, 2010 1:24 PM
    Wednesday, March 10, 2010 10:36 AM
  • Dear Cor Ligthert,

    Sorry, I didn't get it.
    The properties of TextBox has "Font" and "ForeColor", same as RichTextBox has. The "Font" dialog box has only "Underline" & "Strikeout" effect  other than "Font Style", "Font" & "Size" in both.

    Can you please explain a bit more.

    What about the 2nd problem, can you please help on that also.

    I check this forum for similar question but didn't get anything similar. May be I used incorrect word/words for searching.
    MSDN Library with MS VS 2008 also didn't give any useful answers.

    Thanks
    Regards
    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 10:41 AM
  • Sorry, I made the text to short.

    The textbox has no fonts or whatever for individual parts of the text, you would use the richtextbox for that.

    For instance Trujade has showed many good samples about that in this forum.


    Success
    Cor
    • Marked as answer by V.K.S.B.K Wednesday, March 10, 2010 12:11 PM
    • Unmarked as answer by V.K.S.B.K Wednesday, March 10, 2010 2:56 PM
    Wednesday, March 10, 2010 11:05 AM
  • Dear Friends,

    I am giving an example to explain what I am trying to do.

    A TextBox has the Following Words; " ABC : 12" , "EGF : 15" and "JUT : 02".

    I want to have "ABC : 12" to have Red color Font, "EGF : 15" - Blue Font and "JUT : 02" to have Green Font.

    Is it possible to do this?
    Changing the "Fore Color" in the "Property" of the TextBox changes the color of all the Texts to one color.

    The other problem I am having is (how) to get these sorted according to the Number shown with the ABC, EGF etc,. They should be arranged either in ascending order or descending order.
    Each one is displayed as a String ("ABC : 12" is a String but "ABC" is a String and "12" is a Double converted to String).

    Please help.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !

    Try not to double post questions:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/8403fab4-eb87-45cf-a35a-2647bc07afaa

    :)


    Doug

    SEARCH ... then ask
    Wednesday, March 10, 2010 11:25 AM
  • Dear Friends,

    I am giving an example to explain what I am trying to do.

    A TextBox has the Following Words; " ABC : 12" , "EGF : 15" and "JUT : 02".

    I want to have "ABC : 12" to have Red color Font, "EGF : 15" - Blue Font and "JUT : 02" to have Green Font.

    Is it possible to do this?
    Changing the "Fore Color" in the "Property" of the TextBox changes the color of all the Texts to one color.

    The other problem I am having is (how) to get these sorted according to the Number shown with the ABC, EGF etc,. They should be arranged either in ascending order or descending order.
    Each one is displayed as a String ("ABC : 12" is a String but "ABC" is a String and "12" is a Double converted to String).

    Please help.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !

    Try not to double post questions:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/800409a4-65ab-4ad6-87db-93e85303e0cc

    :)


    Doug

    SEARCH ... then ask
    Wednesday, March 10, 2010 11:26 AM
  • Dear Doug,

    I'm sorry for this question posted twice.

    The 1st Time when I clicked "Submit" button, nothing happened for a long time then disappeared, fortunately I was able to copy the question while waiting for something to happen as I thought there was something wrong so to prevent re-typying I copied my question and then re-posted it.

    Honestly I didn't see the 1st question posted (appearing in my Thread).

    Now I realise What Cor Ligthert wrote "one time is enough , and this question is endless answered." he must have pointed my 2 posts.

    Sorry about that.

    Thanks for pointing out the answers in the other question which I didn't read.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 12:09 PM
  • Dear Doug,

    I'm sorry for this question posted twice.

    The 1st Time when I clicked "Submit" button, nothing happened for a long time then disappeared, fortunately I was able to copy the question while waiting for something to happen as I thought there was something wrong so to prevent re-typying I copied my question and then re-posted it.

    Honestly I didn't see the 1st question posted (appearing in my Thread).

    Now I realise What Cor Ligthert wrote "one time is enough , and this question is endless answered." he must have pointed my 2 posts.

    Sorry about that.

    Thanks for pointing out the answers in the other question which I didn't read.

    Thanks
    Regards

    VKSBK
    A Real Novice Programmer !
    Wednesday, March 10, 2010 12:13 PM
  • Now I realise What Cor Ligthert wrote "one time is enough , and this question is endless answered." he must have pointed my 2 posts.


    Actually, what I think he refers to in his post is that this question has been asked and answered many times in the past.  There are a few threads that will help you in the following forum search.  It's just a matter of using the correct search terms to find things here.  In this case, I used "selected text color" as the search term:

    http://social.msdn.microsoft.com/Search/en-US?query=selected%20text%20color&rn=Visual%20Basic%20General%20Forum&rq=meta:Search.MSForums.ForumID(0f60fa48-1ceb-41ee-a10a-0dfcee7e19bd)%20site:microsoft.com&ac=3

    You'll just need to look through the threads that are returned in that search to find those that pertain to your question (there are a few...I did look at what was returned).  Between those threads and the answers you have in these 2 threads of yours, you should be able to come up with something.

    :)


    Doug

    SEARCH ... then ask
    Wednesday, March 10, 2010 12:22 PM
  • Dear Veeraraghavan & Acamar,

    Thanks for the reply.

    I'll try these and come back if I get into any problems.

    For the time being I marked these are answers to my question.

    Sorry for the late reply, you'll find out why if you read the reply to Doug.

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 12:23 PM
  • Dear Acamar & Veeraraghavan,

    Yes, this works.
    But I want to know one more thing so just to get this question attracted I marked it as Unanswered.

    Now, the the Texts are as follows;

    "ABC : 12 deg 25 min" , "EGF : 15 deg 12 min" and "JUT : 02 deg 15 min".
    and I want to  sort them
    (1) according to the Deg (ie; 12, 15, 02) and
    (2) according to the Min ( ie; 25, 12, 15) and
    (3) if the Deg becomes same then sort them according to the Min (eg: 28 deg 21 min & 
         28 deg 12min).

    I couldn't find out how to do this.
    Can you help please?

    Thanks
    Regards

    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 1:37 PM
  • Dear Doug,

    Thanks for the information.

    Thanks
    Regards
    VKSBK

    A Real Novice Programmer !
    Wednesday, March 10, 2010 2:58 PM
  • Sorting it after it is converted to text makes the process harder, and also restricts you if you might want to make changes to the text in the future, for instance to reformat it for different users. I would recommend that you provide some information about how the data is initially available. Are there numeric variables for these items?  Are these in degrees and minutes, or some sort of combined form (degrees with decimals, for instance?   Also, a question about sorting is very different than displaying text in colors, so I would recommend a new thread with an accurate title - that will attract more attention from people who know about sorting. 

    If you post twice just cancel one of them, although you have to get in quick before people post a complaint and make it harder for you to delete.
    Wednesday, March 10, 2010 8:15 PM
  • Hello V.K.S.B.K.,
    Welcome to MSDN Forum! 
    I have merged your two similar questions. In order to faciliate the Forum management, please mark the replies as answers if they help and unmark them if they provide no help. Thanks.


    Best regards!
    Liliane Teng
    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Thursday, March 11, 2010 9:05 AM


  • Now I realise What Cor Ligthert wrote "one time is enough , and this question is endless answered." he must have pointed my 2 posts.


    The first part is true, the second part was more meant on that what is often (where I remember me from Trujade maybe also by you) has showed in this forum.
    Success
    Cor
    Thursday, March 11, 2010 9:22 AM
  • Dear Acamar, Cor Ligthert & Liliane Teng,

    Thanks for the reply and all the help.

    I'm sorry for very late reply as I had a Internet connection problem & electricity problem today.

    Cor Ligthert, Thanks for the clarifying my post.

    Liliane Teng, Thanks for merging the 2 Questions, I was feeling a bit guilty about the mistake I made and couldn't delete one of them since that was answered by few.

    Acamar, Thanks for the reply. Sorry I'm replying you after nearly 20 hours.

    You wrote " I would recommend that you provide some information about how the data is initially available. Are there numeric variables for these items?  Are these in degrees and minutes, or some sort of combined form (degrees with decimals, for instance?   Also, a question about sorting is very different than displaying text in colors, so I would recommend a new thread with an accurate title - that will attract more attention from people who know about sorting. "

    Getting the Data involves 2-3 Functions and some need a DLL file (which comes with the "Swiss Ephemeris") to work.
    I need some time to present my questions / problems in a better way so that all can understand it.

    You may remember that I mentioned in one of my earlier posts that I was writing an Astrology Program using the" Swiss Ephemeris" ( free for Non-commercial purpose and need to get paid Licence for commercial purpose). This problem I am having is in that program I am creating - Not for commercial purpose But for learning programming and it has been an ambition of mine - writing my own Astrology Program myself.. (Started learning VB.Net  to write an Astrology Program)

    The planetary positions and Cusps positions are obtained by Functions which come in the "Swiss Ephemeris" and I have to just Call them.

    I want to sort these positions in an order when there are more than one in a Zodiac Sign, and want to show the Planets and Cusp in different Color fonts.

    I'll present these thing in a better way with the relevant Functions - those functions will not work if you just copy them as they need the DLL File.

    Hope this gives some introduction for my next post on this problem.

    Thanks
    Regards

    VKSBK





    A Real Novice Programmer !
    Thursday, March 11, 2010 4:39 PM
  • OK.  The functions themsleves probably aren't that important, but understanding the data that you are actually manipulating is.  The answer is probably going to involve creating a structure that contains the associated data elements, then identifying which ones are involved in defining the sequence for sorting purposes, and constructing a routine that sorts an array of those structures according using the structure data elements that control the sequence.
    • Marked as answer by V.K.S.B.K Saturday, March 13, 2010 4:21 AM
    Thursday, March 11, 2010 10:51 PM
  • Hi V.K.S.B.K_ ,


    With two buttons and one RichTextBox on a FORM try this code please.

    I entered the following into the RichTextBox.>>

    12 23'34"
    12 23'30"
    12 23'32"


    Which represent;

    12 degrees, 23 minutes and 34 seconds then
    12 degrees, 23 minutes and 30 seconds then
    12 degrees, 23 minutes and 32 seconds.

    Do not hit ENTER on the last line otherwise you get an empty line when you click on the "Sort ascending " button.

    See my RecolorText SUB for an example of recoloring the text.


    Regards,

    John

    Option Strict On
    Imports System.Environment
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.Size = New Size(75, 40)
            Button2.Size = New Size(75, 40)
            Button1.Text = "Sort" & NewLine & "ascending."
            Button2.Text = "Sort" & NewLine & "descending."
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim textLines() As String = RichTextBox1.Lines
            Array.Sort(textLines)
            RichTextBox1.Lines = textLines
            ReColorText(RichTextBox1)
    
        End Sub
    
        Private Sub ReColorText(ByVal aRichTextBox As System.Windows.Forms.RichTextBox)
    
            Dim index As Integer = 0
            Dim currentColor As Color = Color.Red
            aRichTextBox.SelectionColor = currentColor
    
            Do
                aRichTextBox.SelectionStart = index
                aRichTextBox.SelectionLength = 1
                If aRichTextBox.SelectedText = " " Then currentColor = Color.Green
                If aRichTextBox.SelectedText = "'" Then currentColor = Color.Blue
                If aRichTextBox.SelectedText = vbLf Then currentColor = Color.Red
                aRichTextBox.SelectionColor = currentColor
                index += 1
            Loop Until index = aRichTextBox.Text.Length
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim textLines() As String = RichTextBox1.Lines
            Array.Sort(textLines)
            Array.Reverse(textLines)
            RichTextBox1.Lines = textLines
            ReColorText(RichTextBox1)
    
        End Sub
    
    End Class


    • Marked as answer by V.K.S.B.K Saturday, March 13, 2010 4:20 AM
    Friday, March 12, 2010 12:44 AM
  • Dear Acamar & John Anthony Oliver,

    Thanks for the help and the reply.

    Sorry, I couldn't reply to you both as there was some problem with the Internet connection.

    I'll try your code and let you know if I need any more help.

    Thanks
    Regards
    VKSBK

    A Real Novice Programmer !
    Saturday, March 13, 2010 4:26 AM