Solved

# TextBox as Percentage

Posted on 2009-04-07
901 Views
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
Question by:suicehockey44
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 48

Expert Comment

ID: 24087377
You can do something like this

Dim value As Integer = 50
Debug.WriteLine(FormatPercent(value / 100))
0

Expert Comment

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
0

LVL 48

Accepted Solution

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
0

## Featured Post

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
###### Suggested Courses
Course of the Month5 days, 5 hours left to enroll