Results 1 to 10 of 10

Thread: Sorting Items In A TxtBx by Alphabetical Order ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    19

    Sorting Items In A TxtBx by Alphabetical Order ?

    Hi All,

    I have created a program that when a user selects a button, an input box is displayed to type in, for instance a name, when clicked ''OK'' the name goes into a textbox, how can i program it so that the contents of that textbox is automatically sorted into Alphabetical order according to the first to the first letter ?

    Can this be achieved ?

    Kind regards.

    THink this should have been posted in VB.NET forums, sorry.
    Last edited by SWDT; Oct 28th, 2009 at 11:17 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Put them in a ListBox instead of a textbox.

    Sorting a listbox can be done simply by setting a property.

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    One more way to do it is export the text from the textbox to array and then sort it. Once sorted, export it back to textbox....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    19

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Thanks chap - the issue I'm now having is that the text isn't going into the Listbox where as it was with the Textbox. What happens is a user click on the ''Create contact'' button where an inputbox displays, they enter a name and click ok, the name then appeared in the textbox, however changing it to a listbox and also moding the code to direct the text to the listbox, its no longer working and the text isn't displaying.

    Is that a result of it being a listbox and not a textbox ?

    Kind regards.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Not sure what you are asking but you just need to forget the textbox, use a listbox as suggested with a simple bit of code similar to this.
    Code:
     Dim Response As String = InputBox("Enter Contact Name", "Create Contact")
    
            If Response <> String.Empty Then
                ListBox1.Items.Add(Response)
            End If

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    19

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Thanks for that works a treat, I know I'm talking about the Textbox, however another feature that it was able to do was to save the text inside the textbox into a .TXT or whatever, I've noticed that I cannot save the items that are in the listbox the same way, is that possible. Here is the save code that I'm using:

    Code:
      Dim Save As New SaveFileDialog()
            Dim myStreamWriter As System.IO.StreamWriter
            Save.Filter = "Text[*.DOOS*]|*.DOOS|All File[*.*]|*.*"
            Save.CheckFileExists = False
            Save.Title = "Save File"
            Save.ShowDialog(Me)
            Try
    
    
                myStreamWriter = System.IO.File.AppendText(Save.FileName)
                myStreamWriter.Write(ListBox1.Text)
                myStreamWriter.Flush()
            Catch ex As Exception
    
            End Try
    Many thanks to all help so far.

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Did you see post no 3?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2009
    Posts
    19

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Ah apologies missed that - I can give that a go, is that easy enough to accomplish ?

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    It is ...

    Give it try...

    If you get stuck then simply post the code that you have tried and we will definitely help
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Sorting Items In A TxtBx by Alphabetical Order ?

    Here is one simple way to do it using LINQ.

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim sortedText = From t In Split(TextBox1.Text, vbCrLf) Order By t
    3.     TextBox1.Text = Join(sortedText.ToArray, vbCrLf)
    4. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width