split a String AND insert into it
-
Tuesday, July 14, 2009 9:19 AMHi, im trying to do a bit of basic string manipulation - but for some reason i cant get my head around this.
Basically i have some text boxes with strings in them, they are pulled from SQL money columns so show to 4 decimal places.
what i want to do is strip the period and 4 decimal places off, easy enough i can do that:
Dim SplitStrng() As String SplitStrng = Split(TextBox1.Text, ".") TextBox1.Text = SplitStrng(0)
but i also want to add a uk pound sign to the begining of the string, so ....
Dim StrOrigional As String = TextBox1.Text TextBox1.Text = StrOrigional.Insert(0, "£")
Thing is i want to do both these things on the string. If i try to combine the two then the insert refuses to work, or even causes the whole app to crash.
What would be nice is to bash this into a function so it can be re-used over and over since its something id use a lot, but i cant sus out how to do that since how im doing it is on a particular textbox, how to make it so i could run it as a function on any text box escapes me.
very newbie question i know, sorry guys, but its an easy answer if someone could!
All Replies
-
Tuesday, July 14, 2009 9:27 AMDim SplitStrng() As String
SplitStrng = Split(TextBox1.Text, ".")
TextBox1.Text = SplitStrng(0).Insert(0, "£")
:: Learning .Net :: -
Tuesday, July 14, 2009 10:00 AM
Or even:
TextBox1.Text = TextBox1.Text.Insert(0, "£") SplitStrng = Split(TextBox1.Text, ".")
I would guess, however, that after you're inserting the £ symbol, you're trying to perform numeric operations on the pounds and pence of the transaction, which would have you trying to do something to the 'number' "£1", which is, of course, not a number...
I make this guess because I can't think how you might be getting crash-exceptions otherwise... -
Tuesday, July 14, 2009 10:03 AM
See the answer in this thread for your currency problem, but change en-US for en-UK
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5f45da97-35e9-4767-ba6e-63378079042d/
Success
Cor- Proposed As Answer by YiChun ChenModerator Thursday, July 16, 2009 9:36 AM
- Marked As Answer by Martin Xie - MSFT Monday, July 20, 2009 3:34 AM
-
Tuesday, July 14, 2009 11:25 AM
Thanks for the replies folks.
As Kfrost pointed out, it would indeed cause an issue, and on saving to SQL i wouldnt want to save the £ sign, just the value. however im not trying to perform any operations as yet, just display the values from the database, and it would still crash.
Still, textbox1.text wouldnt work for a function either since its textbox specific...
Ill give your answer a whirl Cor and see if i can get it to work.
Dim Field As Decimal = 14 Dim Show = Field.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("en-Uk")) -
Tuesday, July 14, 2009 11:34 AM
i found this to work just fine, from the thread you linked Cor:
Dim decimalCost As Decimal decimalCost = CDec(FormatCurrency((LoanLimit1TB.Text), 2)) LoanLimit1TB.Text = ("£" & CStr(decimalCost))
but couldnt for the life of me figure out the line of code you provided in that thread (as quoted above) - not sure where im going wrong with it:
Dim Field As Decimal = 14 Dim Show = Field.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("en-Uk")) textbox1.text = (Show)
will either of these cause an issue when saving a record to the Db as Kfrost pointed out? i wouldnt want the £ symbol to write, or attempt to write to the Db. -
Tuesday, July 14, 2009 11:39 AM
Hi Cor,FthrJack,
using the link as you (Cor) has suggested and also changing the en-US to en-UK just returns a $ as currency character.
Better use it like this:
Dim Field As Decimal = 14 Dim PoundNumberInfo As System.Globalization.NumberFormatInfo PoundNumberInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-UK").NumberFormat PoundNumberInfo.CurrencySymbol = "£" Dim Show = Field.ToString("C", PoundNumberInfo)
Mark the thread as answered if the answer helps you. This helps others who have the same problem !- Marked As Answer by Martin Xie - MSFT Monday, July 20, 2009 3:35 AM
-
Tuesday, July 14, 2009 1:13 PM
Hi Heslacher,
I got that working as below:
Dim Field As Decimal = Textbox1.Text Dim PoundNumberInfo As System.Globalization.NumberFormatInfo PoundNumberInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-UK").NumberFormat PoundNumberInfo.CurrencySymbol = "£" Dim Show = Field.ToString("C", PoundNumberInfo) Textbox1.Text = (Show)
However i have it as a TextChanged event at the moment - because the text in the textboxes changes when different rows in a gridview are selected, so understandably that causes issues when typing in the box. ("1£1.00" etc, and errors if you delete the whole text)
im guessing the best place for it is in a Lostfocus event, but then ill need to also have it change somehow when you select a new record in the gridview... which really only leaves textchanged event as far as i can tell, which doesent work properly.
putting a label "£" infront of the text box might be much less problematic, though it doesent look very nice.... All im trying to emulate is the "format = Currency" property from Access forms.- Edited by FthrJACK Tuesday, July 14, 2009 1:18 PM
-
Tuesday, July 14, 2009 1:34 PM
This, here, is a method I wrote about three years ago when, I'll admit, I had no goddamn idea what I was doing.
It takes a textbox, a keyPressEventArgs raised by that textbox, and the maximum, 4-figure, numeric, value you want to allow into the textbox.
It handles (to a degree) inserting text at random points, keypresses while text is selected and I think at one point, it used to insert a "C" character at the beginning of the string, if you asked it nicely, but I think I got rid of that feature on the grounds that maintaining that "C" across several situations, all of which expected the textbox string to be entirely numeric, was making me go completely postal.
Writing it was an unbelievable pain in the ____, and it's probably still buggy as all ____, but you're welcome to use it as a starting point.
Public Sub KeyPressHandlerNumeric(ByRef TBox As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal max As Integer) Dim check As String = "0" Dim checkchanged As String = "0" If TBox.Text = "0" Or TBox.Text = "00" Or TBox.Text = "000" Then ' TBox.Text = String.Empty End If 'If (e.KeyChar.ToString = "0") And (TBox.SelectionStart = 0) Then ' e.Handled = True ' Exit Sub 'End If If IsNumeric(TBox.Text) Then check = TBox.Text checkchanged = check End If Dim oldSelectionStart As Integer = TBox.SelectionStart If IsNumeric(e.KeyChar) Then ClearTextSelected(TBox, oldSelectionStart) If oldSelectionStart = 0 Then TBox.Text = e.KeyChar.ToString & TBox.Text ElseIf oldSelectionStart < TBox.TextLength Then TBox.Text = Mid(TBox.Text, 1, oldSelectionStart) & e.KeyChar.ToString & Mid(TBox.Text, oldSelectionStart + 1) Else TBox.Text = TBox.Text & e.KeyChar.ToString End If TBox.Invalidate() TBox.SelectionStart = oldSelectionStart + 1 ElseIf Equals(e.KeyChar, ChrW(System.ConsoleKey.Backspace)) Then If TBox.SelectionLength <> 0 Then ClearTextSelected(TBox, oldSelectionStart) TBox.SelectionStart = oldSelectionStart Else If oldSelectionStart = 0 Then ElseIf (oldSelectionStart = 1) And (TBox.Text.Length > 1) Then TBox.Text = Mid(TBox.Text, 2) ElseIf oldSelectionStart = 1 Then TBox.Text = String.Empty ElseIf (oldSelectionStart > 1) And (oldSelectionStart <= TBox.Text.Length) Then TBox.Text = Mid(TBox.Text, 1, oldSelectionStart - 1) & Mid(TBox.Text, oldSelectionStart + 1) TBox.SelectionStart = oldSelectionStart - 1 Else TBox.Text = Mid(TBox.Text, 1, TBox.Text.Length - 1) TBox.SelectionStart = TBox.Text.Length End If End If End If If IsNumeric(TBox.Text) Then If CInt(TBox.Text) > max Or CInt(TBox.Text) < 0 Then TBox.Text = check TBox.SelectionStart = oldSelectionStart End If Else TBox.Text = "0" TBox.SelectAll() End If e.Handled = True End Sub -
Tuesday, July 14, 2009 2:20 PM
This, here, is a method I wrote about three years ago when, I'll admit, I had no goddamn idea what I was doing.
Ah so you do know how i feel then? haha
Thanks for the code KFrost, ill have a play around with it, i cant believe this is so awkward when in Access its just a text box property! and i haaaaaaaaaaate access and its bugs and quirks, limitations and tribbles (hence this project).
im still convinced there has to be a simple way ... -
Tuesday, July 14, 2009 2:29 PM
Hi Cor,FthrJack,
using the link as you (Cor) has suggested and also changing the en-US to en-UK just returns a $ as currency character.
Better use it like this:
Dim Field As Decimal = 14 Dim PoundNumberInfo As System.Globalization.NumberFormatInfo PoundNumberInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-UK").NumberFormat PoundNumberInfo.CurrencySymbol = "£" Dim Show = Field.ToString("C", PoundNumberInfo)
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
Hi ALL,
Unless it has been recently added I thought en-UK did not exist?
I thought it was en-GB for ENglish-GreatBritain ??
Anyone care to verify please?
You can also ChangeCulture and ChangeUICulture like this.>>
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) My.Application.ChangeCulture("en-GB") My.Application.ChangeUICulture("en-GB") End Sub End Class
Regards,
John -
Tuesday, July 14, 2009 2:34 PMHi John,
you are right.
Mark the thread as answered if the answer helps you. This helps others who have the same problem ! -
Tuesday, July 14, 2009 2:57 PMHi John,
Sorry, I had probably just before looked at Google News UK
:-)
Cor
Success
Cor -
Tuesday, July 14, 2009 3:14 PMFthrJack
unrelated to this thread...I posted some revised code to your other thread. Until your last post I didn't undersatand what exactly you were trying to accomplish.
so I changed it around a little. It verifies the format for whichever combobox item is selected.
of course by now you've probably moved on and don't want to go back to that :) -
Wednesday, July 15, 2009 7:43 AM
Yes John you are right!
@jwavila - thanks, ill check it out. I had left it on the back burner a bit to be honest, since it is semi functional at the moment!
ok so using Cors origional suggestion, it works nicely. So long as we are in the right country lolPrivate Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim Field As Decimal = TextBox1.Text Dim Show = Field.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")) TextBox1.Text = (Show) End Sub
The problem is because i HAVE to use this as a textchanged event, if you type a new number into the textbox it causes an error. Because after the first digit is entered it adds two decimal places and the pound sign, as you would expect. So now you have: £1.00
But the cursor is at the begining of the text, and on the next keypress you get: 2£1.00
Which causes an error. . . any suggestions welcome! -
Wednesday, July 15, 2009 8:14 AM
I guess that if you have to do things on that event, you'll have to parse the £ sign out, and then re-insert it.
Dim thing As String = "£2.50" thing = System.Text.RegularExpressions.Regex.Replace(thing, "£", String.Empty)
-
Thursday, July 16, 2009 10:03 AMModerator
Thank you for KFrostILEM's good suggestion.
Hi FthrJACK,
You also can put the code into KeyDown Event and set the condition to handle the issue when we press Enter.
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then Dim Field As Decimal = TextBox1.Text Dim Show = Field.ToString("C", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")) TextBox1.Text = (Show) End If End SubHope this can help you a lot.
Thanks
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Martin Xie - MSFT Monday, July 20, 2009 3:57 AM

