Formatting Text: How To?

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
I have a textbox and a button.

no matter what is entered in the textbox - I want a space to appear after the first two characters when the button is clicked. So it will happen like this:

textbox = AA1111B

- button clicked -

textbox = AA 1111B

I have tried, format(textbox.text, "## #####") but that doesn't work. Can somebody help me on this one :)
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
button event:

VB.NET:
[/color]
[color=#0000ff][size=2][color=#0000ff]Private[/color][/size][size=2][color=#000000] [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#000000] Button1_Click([/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] sender [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.Object, [/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] e [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.EventArgs) [/color][/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#000000] Button1.Click[/color]

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] str1 [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = TextBox1.Text

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strAdd [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = str1.Insert(2, " ")

MessageBox.Show(strAdd)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][/color]

[color=#0000ff]

Cheers ;)
 
Last edited:

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
kulrom said:
button event:

VB.NET:
[/color]
[color=#0000ff][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#000000] Button1_Click([/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] sender [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.Object, [/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] e [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.EventArgs) [/color][/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#000000] Button1.Click[/color]
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] str1 [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = TextBox1.Text
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strAdd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = str1.Insert(2, " ")
 
MessageBox.Show(strAdd)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size][/color]
 
[color=#0000ff]

Cheers ;)

Man...that works perfect - and it look so simple. Thank you for that. The only thing I run into with it is that if I already have a space there - it puts another one. I thought about setting the max length of the textbox, but that wouldn't work - as there are cases when the text will be longer then other times. Is there an If/Then statement I can run with this? What are your thoughts -?

I noticed a textbox1.text.replace method, but that will just replace the 2nd character - which would work I guess if there was already a space there, but what if there wasnt' - AA1111A would end up like A 1111A.
 

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Well, it is like this:


Lets say the database lists column "locations"

the data is like so:

AA America
BB Africa
CC Canada
DD France

if a user types in the search box, aaamerica - it will not find what I need. That is the purpose to add a "white space" in the third position. But if the user enters aa america - it will not find what I need either because the string will be aa america instead of aa america.

I need a way for the program to determine if there is or is not a space in the 3rd position and respond accordingly.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
VB.NET:
Dim[/color][/size][size=2] str1 [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strAdd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size]


[size=2][color=#0000ff][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#000000] Button1_Click([/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] sender [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.Object, [/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] e [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.EventArgs) [/color][/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#000000] Button1.Click[/color]
 
str1 = TextBox1.Text
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] str1.Substring(2, 1) = " " [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]MessageBox.Show(str1) [/size][size=2][color=#008000]'it's ok
 
[/color][/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strAdd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = str1.Insert(2, " ")
 
MessageBox.Show(strAdd)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If 
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]

[size=2][color=#0000ff]



Cheers ;)
[/color][/size]
 

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Kulrom,

You have been a great help. Thank you so much for this. I was trying to use indexOf and that wasn't going anywhere...thank you again!
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
welcome ... btw you could use this to be sure that it doesn't preform else if statement all the time:

VB.NET:
If[/color][/size][size=2] str1.Substring(2, 1) = " " [/size][size=2][color=#0000ff]Then[/color][/size]
[color=#0000ff][/color] 
 
[size=2][color=#0000ff]
[/color][/size][size=2]MessageBox.Show(str1) [/size][size=2][color=#008000]'it's ok
 
[/color][/size][size=2][color=#0000ff]ElseIf[/color][/size][size=2] str1.Substring(2, 1) <> " " [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strAdd [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = str1.Insert(2, " ")
 
MessageBox.Show(strAdd)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If[/color][/size]
[size=2][color=#0000ff]

Cheers ;)
 
Top Bottom