How do I enter the code to titlecase a textbox?:cry:
Printable View
How do I enter the code to titlecase a textbox?:cry:
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)
vb.net Code:
Me.TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
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.
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.
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)
Here's an example of declaring an extension method on the String class: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:
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 Moduleand "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.vb.net Code:
MessageBox.Show("hello world".ToTitleCase())
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.
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?
Thus:Code:Imports System.Runtime.CompilerServices
Public Module StringExtensions
<Extension()> _
Public Function IndexOfMulti(ByVal instance As String, ByVal search As String) As Integer()
...
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 :( )
:thumb: 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.Quote:
Originally Posted by Jenner
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.