Solved

TextBox as Percentage

Posted on 2009-04-07
3
898 Views
Last Modified: 2012-06-27
Hi.  I basically want a text box entry to be a percentage. So, if the user enters 100 its 100 percent of a value. 50 would be 50 percent (.50) and so on.  In the database its simply a decimal.  How would I go about doing this?

Thanks!!!
0
Comment
Question by:suicehockey44
  • 2
3 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24087377
You can do something like this
 

        Dim value As Integer = 50

        Debug.WriteLine(FormatPercent(value / 100))

Open in new window

0
 

Expert Comment

by:spacerat60
ID: 24087448
I Think you are asking a Type casting question

also you might want to sanitize the input to make sure it is within range of percentages (less than 1 and greater than 0)
Function getValue(TextBox tb)

    Dim percent As Double

    

    'Cast String to double

    percent = CDBL(tb.value)

    IF( (percent/100) <= 1 and (percent/100) >= 0 )

        getValue = percent

    ELSE

       'invalid percentage value

    ENDIF

ENDF

Open in new window

0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24087496
Here's another validation if you want to format the textbox on leave event
    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

        If Integer.TryParse(Me.TextBox1.Text, New Integer) AndAlso Me.TextBox1.Text >= 0 AndAlso Me.TextBox1.Text <= 100 Then

            Me.TextBox1.Text = FormatPercent(Me.TextBox1.Text / 100)

        Else

            Me.TextBox1.Text = String.Empty

        End If

    End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - Oracle BulkCopy from CSV Date Format 7 44
MsgBox 4 45
Put window Form inside tab page 10 26
VB.Net - Windows UserName index and length error 4 20
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now