Solved

VB 2008  - Convert contents of TextBox to an Integer

Posted on 2009-07-12
3
304 Views
Last Modified: 2012-05-07
ActiveAns is a textbox.  It may contain Nothing or an integer.  If it contains a number I need to convert that to (n) as below.  I can't seem to do this without runtime errors that say it cannot convert it.  

How do I test ActiveAns and then convert the contents of ActiveAns to n ?


If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
End If

Open in new window

0
Comment
Question by:ljcor
3 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 24833637
Use the Integer.TryParse method....

        Dim n As Integer
        If Integer.TryParse(ActiveAns.Text, n) Then
            MsgBox(n.ToString & " is an integer")
        Else
            MsgBox("is not an integer")
        End If

Wayne
0
 
LVL 2

Expert Comment

by:Moed
ID: 24834070
If IsNumeric(ActiveAns.Text) And ActiveAns.Text > 0 Then
try
    Dim n As Integer = Int(ActiveAns.Text)
    blah blah blah
catch ex as exception
Msgbox("Entry must be a number")
end try
End If

0
 

Author Closing Comment

by:ljcor
ID: 31602542
Thanks to each of you for your advice.  I didn't use the second solution because I thought I had used it earlier without a result.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Footer for each row on Gridview 2 39
VB.Net Tranfer data between to VB.Net apps 8 44
How to duplicate form in Visual Studio 2015 2 89
ModalPopup  question 22 36
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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