Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' declare constants and variables
        Dim cm As Integer
        Dim m As Double
        Dim g As Integer
        Dim kg As Double
        mtextbox.Text = ""
        kgtextbox.Text = ""

        ' converting from cm to m
        Integer.TryParse(cmTextBox.Text, cm)
        m = cm / 100

        ' converting from g to kg
        Integer.TryParse(gtextbox.Text, g)
        kg = g / 1000


        ' display the converted result (cm to m)
        If user enter integer then 
            display = "m"

        Else
            MessageBox.Show("Please enter a number.")

        End If

        ' display the converted result (g to kg)
        If user enter integer then
            display = "kg"

        Else
            MessageBox.Show("Please enter a number.")

            'set focus
            mtextbox.Focus()
            kgtextbox.Focus()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cmtextbox.Text = ""
        gtextbox.Text = ""
    End Sub
End Class

My project topic is on data conversion for length and weight. There is something wrong with the code. Can anybody help me. Thanks in advance. I need to hand in on 22/1/10. Please reply asap.

Dani AI

Generated

This needs just a few concrete fixes to be reliable and user-friendly.

Do the conversions when the user clicks a Convert button (not in Form_Load — the textboxes are empty there). Validate each input, check the Boolean result returned by TryParse, and only use the parsed number when that call succeeds. Use a floating type (Double or Decimal) if you want to accept fractional values. As pointed out, show the numeric result (not the literal "m"); and as noted, you can use try/catch with Parse, but TryParse is preferred for simple input validation because it avoids exceptions.

A concise pattern to follow (run on a button click): try-parse the cm input; if valid, compute meters and write the formatted result to the meters textbox; otherwise show a short error and set focus back to the bad field. Repeat for grams→kilograms. Keep the Clear button responsible for resetting both inputs and outputs.

Example implementation (place this in the Convert button click handler):

Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
    Dim lengthCm As Double
    If Double.TryParse(txtCm.Text, Globalization.NumberStyles.Float, Globalization.CultureInfo.CurrentCulture, lengthCm) Then
        txtMeters.Text = (lengthCm / 100.0).ToString("0.###")
    Else
        MessageBox.Show("Please enter a valid length in centimeters.")
        txtCm.Focus()
        Return
    End If

    Dim massG As Double
    If Double.TryParse(txtG.Text, Globalization.NumberStyles.Float, Globalization.CultureInfo.CurrentCulture, massG) Then
        txtKg.Text = (massG / 1000.0).ToString("0.###")
    Else
        MessageBox.Show("Please enter a valid mass in grams.")
        txtG.Focus()
    End If
End Sub

Troubleshooting notes: set the form's AcceptButton so Enter converts, enable Option Strict On to catch implicit casts, format output with ToString to control decimals, and test with both dot and comma decimal separators (culture settings). For reference on parsing safely see Double.TryParse documentation.

Recommended Answers

All 5 Replies

If user enter integer then
display = "m"


.

Your first problem is here. Don't put your variable in quotes, or you'll just display the "m". Try this to get started:

Dim cm As Integer
        Dim m As Double

        Integer.TryParse(TextBox1.Text, cm)
        m = cm / 100
        MsgBox(m)'This will display the value in your variable, not the "m."

Also, Integer.TryParse is only going to return a zero if the user does not enter an integer. Look at using integer.parse in a Try / Catch block to trap an invalid entry.

Dim cm As Integer       
 Dim m As Double         
Integer.TryParse(TextBox1.Text, cm)        
m = cm / 100        
MsgBox(m)'This will display the value in your variable, not the "m."

You mean I have to follow this?

Dim cm As Integer
Dim m As Double
Integer.TryParse(TextBox1.Text, cm)
m = cm / 100
MsgBox(m)'This will display the value in your variable, not the "m."

You mean I have to follow this?

I'm not sure what the question is now.

If you are brand new at this, my recommendation is to start very simple. Create a form with just a textbox and a button. Put the code above into the click event of the button. Run the program, enter a value, and see what happens.

In your original code, it looks like you are not even sure how to start, so you need to start simple. This code:

If user enter integer then display = "m"

Is not real code, it is more like "here is what I want to do", so please start simple, and people here will help you build on that.

Good Luck!

im not 100% on what you are trying. here is what you should do if you want an application to convert cm to m and g to kg.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
      
        '

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim number, convertednumber as integer
try
number=textbox1.text
catch
msgbox("please enter a valid number")
end try
      
convertednumber=number/100
textbox1.text=convertednumber.tostring
    End Sub
End Class

To:wiss.dev
You mean I have to follow your code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.