Mike Caldwell
asked on
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
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
do you see a good result if you add "msgbox product"?
ASKER
Where to add?
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)
...
ASKER
I added
msgbox (product)
before and after Label3.text = product with no change.
msgbox (product)
before and after Label3.text = product with no change.
ASKER
Swapped in the "convert" suggestion; still nothing.
What are you entering for the values of TextBox1 and TextBox2?
ASKER
1 & 2.
ASKER
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).
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).
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
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
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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?
what kind of projet is your calculator? Is it a web application?
ASKER
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\Loca l\Temporar y 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!
Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. C:\Users\mike\AppData\Loca
Then all three textbox statements get an error message "requires an object reference."
Geez; I thought the baby step programs would be easy!
ASKER
emoreau: check the link in my previous response. This is a 'forms' example.
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
untitled.PNG
ASKER
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
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
ASKER
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.
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
you don't have any controls named Label3. There is only Label1
ASKER
emoreau: do you mean the issue is copy / paste rather than typing?
is this new sample working?
ASKER
emoreau: just to be safe, I manually typed in the same code. No difference.
ASKER
I skipped the "+" and "=" labels, so only have one label, which is button1.
can you add "option strict on" at the very top and fix all errors
ASKER
????? 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.
ASKER
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?
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?
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
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
ASKER
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?
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
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
ASKER
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.
IDE will not accept "Debuggerbreak." Maybe that is only available for the Pro version? I am using the free Express version.
ASKER
OK, didn't notice the dot. Entered Debugger.break, no errors, and no break.
ASKER
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.
can you copy the URL you used to download VS.Net Express?
>>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?
what have you done meanwhile? any modification?
ASKER
Nothing that I know of.
ASKER
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
can you please anwer with the URL where you downloaded the installation?
ASKER
Direct from Microsoft.
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Even worse: clean install on another machine.
ASKER
To get way down to basics I just ran a VB Script file with jut a message box and it popped up fine.
I don't understand what you are trying to do. VB.net and VBScript are 2 different beast
ASKER
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.
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.
ASKER
I stumbled across a "cockpit error" myself, but appreciate the patience.
Can you open the solution in this link, build it, then run it. Do you experience the same behavior?
ASKER
Cannot open a .PHP file. But my issue has been resolved; what is this about?
ASKER
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.