08-26-2012, 05:16 AM
Okay, so it didn't work as I thought it would. Here's what I did per your instructions:
I created a class with the following code:
I added the following line to the top of my form:
I added the following line to the LostFocus event of my textbox (TextBoxCard):
PROBLEMS:
I noticed that TextBoxCard (which contains the credit card number) doesn't format as intended once the user tabs out of the field.
Instead VB throws a KeyNotFound exception that says:
I found that when I select the Card Type first and then go back to enter the credit card number, everything works fine - except for the formatting. This doesn't work because what I'm intending is to prevent the user from selecting the Card Type. I want the application to choose the Card Type based on the format of the Credit Card Number entered.
Please pardon me, if it seems like I'm asking too much. But I'm learning in the process and I hope you can understand?
I created a class with the following code:
Code:
Public Class CardFormatter
Private cardDictionary As Dictionary(Of String, String)
Public Sub New()
'Now, we need to create the dictionary
cardDictionary = New Dictionary(Of String, String)
'Add the contents
cardDictionary.Add("Amex", "{0:0000 000000 00000}")
cardDictionary.Add("Visa", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Mastercard", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Discover", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Isracard", "{0:0000 0000}")
cardDictionary.Add("Diners", "{0:0000 000000 0000}")
End Sub
Public Function GetCardNames() As List(Of String)
Return New List(Of String)(cardDictionary.Keys)
End Function
Public Function GetFormattedString(ByVal cardName As String, ByVal cardNumber As String) As String
Dim formatString As String = cardDictionary.Item(cardName)
Return String.Format(formatString, Long.Parse(cardNumber))
End Function
End Class
I added the following line to the top of my form:
Code:
Private CF As New CardFormatter
I added the following line to the LostFocus event of my textbox (TextBoxCard):
Code:
CF.GetFormattedString(ComboBoxCardType.Text, TextBoxCard.Text)
PROBLEMS:
I noticed that TextBoxCard (which contains the credit card number) doesn't format as intended once the user tabs out of the field.
Instead VB throws a KeyNotFound exception that says:
Quote:The given key was not found in the dictionaryand then points to the following line in the CardFormatter class:
Code:
Dim formatString As String = cardDictionary.Item(cardName)
I found that when I select the Card Type first and then go back to enter the credit card number, everything works fine - except for the formatting. This doesn't work because what I'm intending is to prevent the user from selecting the Card Type. I want the application to choose the Card Type based on the format of the Credit Card Number entered.
Please pardon me, if it seems like I'm asking too much. But I'm learning in the process and I hope you can understand?
//Kismet