variables to Textbox

tamik0

New member
Joined
Sep 20, 2007
Messages
1
Programming Experience
Beginner
I need help figuring out how to store the values of the text boxes into the variables I have declared (in bold). I can't seem to get this to work, here is my code, I would appreciate any suggestions:
VB.NET:
Option Explicit On 
Option Strict On

Imports System.Convert

Public Class frmMain
    Inherits System.Windows.Forms.Form


    [B]Private mlblInitialValue As Double
    Private mlblSalvageValue As Double
    Private mlblAssetLife As Single[/B]    
    
    Private mdblTotalDepreciation As Double
    Private mdblAnnualDepreciation As Double


    mlblInitialValue = (txtInitialValue.Text)
    mlblSalvageLife = (txtSalvageLife.Text)
    mlblAssetLife = (txtSalvageLife.Text)

    mdblTotalDepreciation = mlblInitialValue - mlblSalvageLife
    mdblAnnualDepreciation = mdblTotalDepreciation / mdblAssetLife
 
Last edited by a moderator:
You should use something like this :

VB.NET:
try
    if txtInitialValue.Text.Trim().Length > 0 then
        mlblInitialValue = Double.Parse(txtInitialValue.Text)
    else
        mlblInitialValue = 0 ' put the default value for an empty textbox
    end if
catch ex as FormatException ' there is a narrower exception thrown, you should use it instead of the general Exception
    mlblInitialValue = 0 ' put any error handling and message here
End If

Basically, if your textbox is used only for numbers, you should validate the user's input as soon as possible, that is in the txtInitialValue.Validating event handler. There, you should make sure that the value can be parsed and message the user or take any necessary action for error handling.
 
Back
Top