|
-
Sep 24th, 2008, 06:43 AM
#1
Thread Starter
New Member
Titlecasing
How do I enter the code to titlecase a textbox?
-
Sep 24th, 2008, 07:25 AM
#2
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)
-
Sep 24th, 2008, 09:35 AM
#3
Re: Titlecasing
vb.net Code:
Me.TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
-
Sep 24th, 2008, 12:45 PM
#4
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.
-
Sep 24th, 2008, 05:55 PM
#5
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.
-
Sep 25th, 2008, 01:35 AM
#6
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)
-
Sep 25th, 2008, 01:57 AM
#7
Re: Titlecasing
Here's an example of declaring an extension method on the String class:
vb.net Code:
Imports System.Runtime.CompilerServices Public Module StringExtensions <Extension()> _ Public Function ToTitleCase(ByVal instance As String) As String Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(instance) End Function 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:
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.
-
Sep 25th, 2008, 02:20 AM
#8
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 )
-
Sep 25th, 2008, 02:36 AM
#9
Re: Titlecasing
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|