Need help with first very simple VB program

I have written many VB Script programs, now want to move on to compiable VB programs.  I am using Visual Basic 2010 Express.  Found a tutorial for a simple calculator.  There are two text boxes to enter two numbers and a result box written as a subroutine under a button1 labeled "Calculate."  No error messages, but nothing shows up in the result box.  In fact the result box disappears on the form; should I see something now, or only when clicking "calculate."   Here is the subroutine:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim num1, num2, product As Single
        num1 = TextBox1.Text
        num2 = TextBox2.Text
        product = num1 + num2
        Label3.Text = product
    End Sub

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

    End Sub
End Class
LVL 1
Mike CaldwellConsultant to IP industryAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Just be sure, can you copy in your code and I'll run it here?
I copied the code from your OP.
0
 
Éric MoreauSenior .Net ConsultantCommented:
do you see a good result if you add "msgbox product"?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Where to add?  
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
käµfm³d 👽Commented:
You are trying to rely on implicit conversion in lines 5 and 6. It would be much safer if you converted the values from strings to singles:

...
num1 = Convert.ToSingle(TextBox1.Text)
num2 = Convert.ToSingle(TextBox2.Text)
...

Open in new window

0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
I added

msgbox (product)

before and after Label3.text = product with no change.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Swapped in the "convert" suggestion; still nothing.
0
 
käµfm³d 👽Commented:
What are you entering for the values of TextBox1 and TextBox2?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
1 & 2.  
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
This is the tutorial I am working from:

http://www.vbtutor.net/vb2010/vb2010_Lesson2.htm
0
 
käµfm³d 👽Commented:
Shame on vbtutor.net for demonstrating implicit conversion with no error handling. What if the user puts in "Hello" and "World!".  Tsk tsk tsk   =P

Did the MsgBox code from the tutorial work for you? I pasted your code into a new project, and it worked fine (withstanding the implicit conversion).
0
 
Éric MoreauSenior .Net ConsultantCommented:
what about this:

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
msgbox "we start"
        Dim num1, num2, product As Single
        num1 = csng( TextBox1.Text)
msgbox "the value of num1 is " + num1.tostring
        num2 = csng(TextBox2.Text)
msgbox "the value of num2 is " + num2.tostring
        product = num1 + num2
msgbox "the value of product is " + product.tostring

        Label3.Text = product
    End Sub
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Yes, the msgbox step was fine.  Then I moved on to the calculator, and just can't get an answer to pop up.   I could not figure out how to add "msgbox" into this code as suggested by emoreau.  Seems very odd that my code works fo you and not me.  Just be sure, can you copy in your code and I'll run it here?
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>Then I moved on to the calculator, and just can't get an answer to pop up.

what kind of projet is your calculator? Is it a web application?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
First line gets the following error message:
Error      1      Handles clause requires a WithEvents variable defined in the containing type or one of its base types.      C:\Users\mike\AppData\Local\Temporary Projects\junk1\Form1.vb      3      103      junk1


Then all three textbox statements get an error message "requires an object reference."

Geez; I thought the baby step programs would be easy!

0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
emoreau: check the link in my previous response.  This is a 'forms' example.
0
 
käµfm³d 👽Commented:
Can you post the code for the Designer.vb file of your form? You may need to "Show All Files" (see screenshot) in order to view it.
untitled.PNG
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
I think this is it.
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(33, 47)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(210, 46)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(100, 20)
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "2"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(128, 128)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(468, 46)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(39, 13)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Label1"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(626, 262)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label

End Class

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
you cannot just copy code from one project to another. you also need to create the controls and these controls need to have the same name to have the eact same code working
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Just to be clear, I started over, so here is the code for the above Designer.vb file:

Also, I did not make the "+" and "+" lables of the example.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim num1, num2, product As Single

        num1 = CSng(TextBox1.Text)

        num2 = CSng(TextBox2.Text)

        product = num1 + num2

        Label1.Text = product

    End Sub
End Class

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
you don't have any controls named Label3. There is only Label1
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
emoreau: do you mean the issue is copy / paste rather than typing?  
0
 
Éric MoreauSenior .Net ConsultantCommented:
is this new sample working?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
emoreau: just to be safe, I manually typed in the same code.  No difference.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
I skipped the "+" and "=" labels, so only have one label, which is button1.
0
 
Éric MoreauSenior .Net ConsultantCommented:
can you add "option strict on" at the very top and fix all errors
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
?????  I don't have any errors.  I started over, then listed the code and the Designer.vb file.  This runs without errors, just that no result is shown in Lable1.text when I click Button1.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
BTW: I just added

Msgbox ("Message")

after the Dim statement, and when I click the message does NOT pop up.  Somehow this is just not running at all.  Maybe I'm just not using the IDE correctly?
0
 
Éric MoreauSenior .Net ConsultantCommented:
comment out everything in your code
go back to your form
double-click the control
this will re-open the code editor with the click event handler already created (but empty)
add just a msgbox statement
test
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
No response.  So I started a new project with nothing but the "Hello World" example of the tutorial, and it does nothing too!  There must be a simple control or preference I messed up.  Maybe reinstall the Express software?
0
 
Éric MoreauSenior .Net ConsultantCommented:
it doesn't make any sense. yes you can try reinstalling it. it is not worth trying to debug your calculator as long as the simple message box is not working
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you tried to put a break point? enter the statement "debugger.break" in the click event to see if your debugger is going through your event
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Reinstalled, restarted PC; still will not display a simple Msgbox.  This is weird.

IDE will not accept "Debuggerbreak."  Maybe that is only available for the Pro version?  I am using the free Express version.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
OK, didn't notice the dot.  Entered Debugger.break, no errors, and no break.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
BTW: this did work initially.  When I first started the tutorial the Msgbox sampel worked fine, I even built it and it ran independently.  Now the same thing does nothing.
0
 
Éric MoreauSenior .Net ConsultantCommented:
can you copy the URL you used to download VS.Net Express?
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>When I first started the tutorial the Msgbox sampel worked fine, I even built it and it ran independently.  Now the same thing does nothing.

what have you done meanwhile? any modification?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Nothing that I know of.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Now it is really weird.  I installed on another PC, so totally clean, no history, etc.  Ran the windowed program below, and no response.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Test")
    End Sub
End Class

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
can you please anwer with the URL where you downloaded the installation?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
0
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
but it makes no sense that it works, then without changing anything (as you said) the same project doesn't work a second time
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Even worse: clean install on another machine.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
To get way down to basics I just ran a VB Script file with jut a message box and it popped up fine.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I don't understand what you are trying to do. VB.net and VBScript are 2 different beast
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
I just wanted to verify that my PC would pop up a message.

So I uninstalled VB, then did a System Restore back to this morning, then installed VB Express clean.  No change in result.  So let me make sure I understand the IDE environment.  I started a new Forms project.  I drug a button box onto the blank form, clicked it, and renamed the button "OK".  I then double clicked on the button and put the simple message-only code between "Private sub" and "End sub" statements. .  I then went to the [name.design] tab and single clicked the OK box.  A new window with "Test" should pop out, but it does not.  There has to be something very simple but very fundamental that I am missing.
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
I stumbled across a "cockpit error" myself, but appreciate the patience.
0
 
käµfm³d 👽Commented:
Can you open the solution in this link, build it, then run it. Do you experience the same behavior?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
Cannot open a .PHP file.  But my issue has been resolved; what is this about?
0
 
Mike CaldwellConsultant to IP industryAuthor Commented:
According to the dialog this shoudl be a ZIP file, but when I click to download it downloads a PHP file; I assume that is the code that uploaded the ZIP file, but I cannot see the PHP code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.