Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Formatting Credit Number in Textbox Control
#5
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:
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 dictionary
and 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


Messages In This Thread
Re: Help Formatting Credit Number in Textbox Control - by kismetgerald - 08-26-2012, 05:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Custom tab control help and making controls Derek275 4 21,172 11-16-2012, 09:51 AM
Last Post: Snake_eyes
  Winsock Control Covert2String 7 25,266 11-27-2011, 08:57 PM
Last Post: brandonio21
  Custom Tab Control xolara 5 20,382 04-24-2011, 07:12 PM
Last Post: xolara

Forum Jump:


Users browsing this thread: 1 Guest(s)