Results 1 to 9 of 9

Thread: Titlecasing

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    Titlecasing

    How do I enter the code to titlecase a textbox?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Titlecasing

    Do You Mean Something Like This?

    There is a VB function in StrConv that'll do that for you:
    Code:
    Dim s As String = StrConv("hello world", VbStrConv.ProperCase)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Titlecasing

    vb.net Code:
    1. Me.TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Titlecasing

    Nice, I didn't know about that hidden gem either jmcilhinney.

    Using that method is more "proper" in VB.NET booface98, so ignore my first post and use the one above.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Titlecasing

    It's be nice if they just included it as a member of the String class. I guess now (.NET 3.5) we can create our own extension method for the String class so its use would be just like String.ToUpper or String.ToLower.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Titlecasing

    Yea, I never found it because I figured it would be part of the String class, and thought it was one of those oddball things that got lost in the transition. Do you got any good examples of adding an extension method to a base class jmcilhinney? I'm going to head over to MSDN and see what I can find tomorrow morning but I was wondering if you knew of a something off the top of your head.

    It's something I haven't played around yet extensively in 3.5 that I can see might come in handy... like adding a custom .IndexOfMulti() to the String class that would return an array of Integers with all the starting indexes of a string within another string:

    Dim myString As String = "A cat is a cat and that is that."
    Dim catIndex() As Integer = myString.IndexOfMulti("cat")
    'catIndex is (2, 11)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Titlecasing

    Here's an example of declaring an extension method on the String class:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module StringExtensions
    4.  
    5.     <Extension()> _
    6.     Public Function ToTitleCase(ByVal instance As String) As String
    7.         Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(instance)
    8.     End Function
    9.  
    10. End Module
    Extension methods must be declared in a module and be decorated with the Extension attribute. They must always have at least one parameter and its the type of that first parameter that the method extends. With the method above declared you can then do this:
    vb.net Code:
    1. MessageBox.Show("hello world".ToTitleCase())
    and "Hello World" will be displayed. Note that you don't actually pass the first parameter. The object you call the extension method on becomes the first parameter.

    The obvious limitation of extension methods is that they only have access to public members of the objects they're called on, but they can make things convenient at times.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Titlecasing

    Huh, that's pretty slick. I would have assumed they would have used different syntax, but that's pretty straight forward. So I assume that any additional parameters you give the method are parameters you're passing on?

    Code:
          Imports System.Runtime.CompilerServices
            
          Public Module StringExtensions
           
              <Extension()> _
              Public Function IndexOfMulti(ByVal instance As String, ByVal search As String) As Integer()
              ...
    Thus:

    Dim indexes() As Integer = myString.IndexOfMulti("blah")

    (I'll have to finish that thought tomorrow when I'm not up at 3am and suffering from insomnia )
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Titlecasing

    Quote Originally Posted by Jenner
    Huh, that's pretty slick. I would have assumed they would have used different syntax, but that's pretty straight forward. So I assume that any additional parameters you give the method are parameters you're passing on?

    Code:
          Imports System.Runtime.CompilerServices
            
          Public Module StringExtensions
           
              <Extension()> _
              Public Function IndexOfMulti(ByVal instance As String, ByVal search As String) As Integer()
              ...
    Thus:

    Dim indexes() As Integer = myString.IndexOfMulti("blah")

    (I'll have to finish that thought tomorrow when I'm not up at 3am and suffering from insomnia )
    Just a couple of things. I'd be inclined to name that method "IndexesOf" or "IndicesOf" rather than "IndexOfMulti". I'd also name the second parameter "value" to be consistent with the existing String.IndexOf method. Finally, you might like to overload the method to, again, be consistent with String.IndexOf.

    We've got a bit off-topic here so, if you have any more questions regarding extension methods, I'd suggest starting a new thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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