Can't Grasp Visual Basic 2005 Express

I bought "Visual Basic 2005 Express Edition for Dummies", and I have got to a part in the book where it tells me to input this, says nothing about a form. I pressed F5 to execute, I don't see what it is doing, this is supposed to be a minimal example. I would also love to know what "Dim" means. I am trying to create a super simple program, Textbox1 + Textbox 2 = Texbox 3, I can't do it b/c this stupid book provides no tell for how to do anything close to this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim TVShow As String
TVShow = "Barney"
TVShow = "Five-O"

End Sub


Thanks, I am frustrated, sorry
 

munkyeetr

New Member
First of all, I am working off some knowledge of Visual Basic 6, but as far as these things go, I believe it is all the same.

Dim stands for Dimension, and it is used to declare variables.

Syntax:
Dim variable-name as Type

so...
Code:
Dim strText as String
Dim iCount as Integer
declares a variable strText which holds a String, and iCount which holds an integer.

In the code you quoted in your post you won't see anything happen with the program because all it is doing is declaring a variable, assigning it a value, then reassigning it another value. It does nothing to display it. Add a textbox to the Form and add this code to the end of your example before the End Sub line:
Code:
Textbox1.Text = TVShow
(It may be Text1.Text in VB 2005, whatever the name of the control is)
and the value of TVShow should be displayed in the textbox.

As far as the "simple program" you want to create:

Textbox1 + Textbox2 = Textbox3

If what you want to do is concatenate Textbox1 and Textbox2 together and assign it to Textbox3, you are forming the statement backwards. It should read:
Code:
Textbox3 = (Textbox1 + Textbox2)
The value being assigned to should be on the left side of the assignment operator (=). Also adding strings like this results in a concatenation, not addition.
 
First of all, I am working off some knowledge of Visual Basic 6, but as far as these things go, I believe it is all the same.

Dim stands for Dimension, and it is used to declare variables.

Syntax:
Dim variable-name as Type

so...
Code:
Dim strText as String
Dim iCount as Integer
declares a variable strText which holds a String, and iCount which holds an integer.

In the code you quoted in your post you won't see anything happen with the program because all it is doing is declaring a variable, assigning it a value, then reassigning it another value. It does nothing to display it. Add a textbox to the Form and add this code to the end of your example before the End Sub line:
Code:
Textbox1.Text = TVShow
(It may be Text1.Text in VB 2005, whatever the name of the control is)
and the value of TVShow should be displayed in the textbox.

As far as the "simple program" you want to create:

Textbox1 + Textbox2 = Textbox3

If what you want to do is concatenate Textbox1 and Textbox2 together and assign it to Textbox3, you are forming the statement backwards. It should read:
Code:
Textbox3 = (Textbox1 + Textbox2)
The value being assigned to should be on the left side of the assignment operator (=). Also adding strings like this results in a concatenation, not addition.

Forgive my stupidity, how would I get that to come up in as msgbox and using X for Textbox1, Y for Textbox2, Z for Textbox 3, Using two buttons, Button1 to display the msgbox and Button2 to calculate(or vice-versa, they will be labeled)

Thanks
 
Textbox3 = (Textbox1 + Textbox2)

I tried that above and I got this

Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'System.Windows.Forms.TextBox'.

Here is my form

Public Class Form1
Dim Textbox3 As Integer


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


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Textbox3 = (TextBox1 + TextBox2)


End Sub

End Class
 

rbxslvr

New Member
Forgive my stupidity, how would I get that to come up in as msgbox and using X for Textbox1, Y for Textbox2, Z for Textbox 3, Using two buttons, Button1 to display the msgbox and Button2 to calculate(or vice-versa, they will be labeled)

Thanks
Do you mean that you have named the textboxes "X", "Y", and "Z"... and want it to show X's value, then Y's, then Z's in a message box?:confused:

If not, can you explain what you want?
 

rbxslvr

New Member
I tried that above and I got this



Here is my form

you want "textbox3.text = Textbox1.text & Textbox2.text"... that is, if you want to combine the two strings... like enter 123 in textbox 1, and 456 into textbox 2, textbox 3 will show 123456
 

Cromewell

Administrator
Staff member
I think CInt is still in VB2005, it tries to convert a string to an integer

textbox3.text = CInt(textbox1.text) + CInt(textbox2.text)
 

rbxslvr

New Member
try this... use 6 textboxes and a button with default names

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox3.Text = TextBox1.Text & TextBox2.Text
TextBox6.Text = CInt(TextBox4.Text) + CInt(TextBox5.Text)
End Sub
End Class



You can't change "textbox3"s value to a string... because it is an object... you must change the text property of the object... (notice my code) that should help
 
If you want them to add as math functions declare them as Integers or Doubles

Tried it,

Public Class Form1
Dim Textbox3 As Integer
Dim Textbox2 As Integer
Dim Textbox1 As Integer


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


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Textbox3 = (TextBox1 + TextBox2)


End Sub

End Class

I get this,
Error 1 'Textbox2' is already declared as 'Friend Dim WithEvents TextBox2 As System.Windows.Forms.TextBox' in this class.
Error 2 'Textbox1' is already declared as 'Friend Dim WithEvents TextBox1 As System.Windows.Forms.TextBox' in this class.

For Doubles it says Dbl is not defined :rolleyes:
 

Cromewell

Administrator
Staff member
It's just going to get more and more confusing.
I'm confused already :p :D

You can't use the same names for them, if you want to use integer/single/double/long/short/some other numeric type variables you have to give them a different name then take the value from the textbox and assign it to the variable.
 

munkyeetr

New Member
1) Start a new project.
2) Add a Form.
3) Add a Command Button.
4) Double click on the Command Button, then post what code it has created for you.

We'll start from scratch.
 

rbxslvr

New Member
PEOPLE... it is "textbox3.text" not "textbox3"!!!!:rolleyes:

That is the problem... you can't change an object to a string... you must change the text property of that object. that is where the errors come from.


(Sorry that sounds so harsh... but no one is listening)
 
Last edited:
CInt is the way to go,


Thanks for all input guys, here is a screene with my simple interface, including a big red button that everyone loves to push. You guys will probably hear more from me, I am just now learning the jargon and simple stuff.



Yes, I used TextBox2.Text :)
 

Cromewell

Administrator
Staff member
Is the .text part not used by default with textboxes anymore? It was in VB6 but I suppose it doesn't really matter if you just put .text on all the time anyway
 

munkyeetr

New Member
Apparently. In VB6 you didn't have to put the .Text part. It was the default, if you didn't specify the property being accessed, it used the Text property.

Ahhh, .NET
 
What would I use to put a beginning text in a text box? Text you can click on and it disappears, then you can type what you want in the Text box.

Also, how would I make a button to clear out all text boxes in the form?

Thanks
 
Top