Solved

TextBox as Percentage

Posted on 2009-04-07
3
897 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

8 Experts available now in Live!

Get 1:1 Help Now