http://forum.codecal...html#post294096
I'm not going to re-type it all, visit that thread if you want a more in depth description of what I'm trying to do. I'm brand new to visual basic and whilst I'd like to learn it along the way my main aim is just to complete my calorie tracking program.
I've created this thread as a place to post my problems as and when I come across them (there will be many)
21 replies to this topic
#1
Posted 24 February 2011 - 06:19 AM
|
|
|
#2
Posted 24 February 2011 - 06:13 PM
Feel free to pound away. :)
There aren't too many Visual Basic programmers here, but I am sure we can cover all the basics nicely.
There aren't too many Visual Basic programmers here, but I am sure we can cover all the basics nicely.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 25 February 2011 - 04:50 AM
Okay, first problem is a simple one i think by I'm not sure why its going wrong.
I have an input text bos (1), a button and an output text box (2). I'm trying to make it such that when you type a number into text box 1 and click the button, it adds that number to the number in text box 2. Currently what happens is that whatever is typed into text box 1 is just copied into text box 2 when the button is pressed.
Help would be much appreciated, thanks.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim input_number As Integer input_number = Convert.ToInt32(TextBox1.Text) Dim total_number As Integer total_number = total_number + input_number TextBox2.Text = Convert.ToString(total_number) End Sub
I have an input text bos (1), a button and an output text box (2). I'm trying to make it such that when you type a number into text box 1 and click the button, it adds that number to the number in text box 2. Currently what happens is that whatever is typed into text box 1 is just copied into text box 2 when the button is pressed.
Help would be much appreciated, thanks.
#4
Posted 25 February 2011 - 11:17 AM
Thats because your adding total_numer with input_number. And since total_number is never assigned its basically saying 0 + input_number.
This code should work, and i think you'll understand it.
Good luck with your project ~ Committed. :)
This code should work, and i think you'll understand it.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim input_number As Integer
input_number = Convert.ToInt32(TextBox1.Text)
[COLOR=red]Dim input_number2 As Integer
input_number2 = Convert.ToInt32(TextBox2.Text)[/COLOR]
Dim total_number As Integer
total_number = [COLOR=red]input_number2[/COLOR] + input_number
TextBox2.Text = Convert.ToString(total_number)
End Sub
Good luck with your project ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#5
Posted 25 February 2011 - 12:23 PM
I did think about that before but i tried to resolve by adding a total_number = 0 somewhere at the start of the program. I think it didn't run or something, can't remember.
Anyway, that works, thanks.
edit: I did have to type 0 into box2 initially though; is no text not considered a 0? I don't think this will be an issue but out of interest is there a way to remove that need?
Anyway, that works, thanks.
edit: I did have to type 0 into box2 initially though; is no text not considered a 0? I don't think this will be an issue but out of interest is there a way to remove that need?
#6
Posted 25 February 2011 - 01:27 PM
Quote
edit: I did have to type 0 into box2 initially though; is no text not considered a 0? I don't think this will be an issue but out of interest is there a way to remove that need?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim input_number As Integer [COLOR=black]Dim input_number2 As Integer[/COLOR] [COLOR=black]Dim total_number As Integer [/COLOR] [COLOR=black]input_number = Convert.ToInt32(TextBox1.Text)[/COLOR] [COLOR=red]If TextBox2.Text != "" Then [/COLOR][COLOR=red]input_number2 = Convert.ToInt32(TextBox2.Text) Else [/COLOR][COLOR=red]input_number2 = 0 EndIf [/COLOR] [COLOR=black]total_number = [/COLOR][COLOR=black]input_number2 + input_number TextBox2.Text = Convert.ToString(total_number) [/COLOR] End Sub
I think that should work, but if not, its because the "if loop" syntax is wrong, I really dont code in VB, I just know enough C# to help you lol.
Good luck ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#7
Posted 25 February 2011 - 01:59 PM
'if loop' syntax was fine, not equal to is: <> :)
It works though and gave me a bit of practice with boolean stuff, cheers.
It works though and gave me a bit of practice with boolean stuff, cheers.
#8
Posted 25 February 2011 - 02:30 PM
why not something like this?
yes.. yes this cat knows VB in fact.... :c-cool:
Sub Main()
Console.Write("Enter a number: ")
Dim i As Integer
If (Not Integer.TryParse(Console.ReadLine(), i)) Then
i = 0
End If
Console.WriteLine(i)
Console.WriteLine("Press <Enter> to terminate...")
Console.ReadLine()
End Sub
yes.. yes this cat knows VB in fact.... :c-cool:
#9
Posted 25 February 2011 - 03:23 PM
I've just run that, works fine but I prefer working in the form mode. Dunno if you read my other post about the project? But the form mode is perfect for what I want. I'm gonna try and do some more work on it now having it store a running number count somewhere so that i can close and re-open the program and the total count remains. I'll definitely need some more help soon :)
update: I've now got calories in and calories burnt with a submit button and it will add those to the total appropriately. I am quite chuffed with myself. I need to know now how to store values in some sort of database so that I can have a stored total calorie number and then eventually store separate data for days and each individual input.
edit: Is it possible to put code already within a sub routine into another one so that it can be minimized? Not really necessary I guess but would help with organisation.
update: I've now got calories in and calories burnt with a submit button and it will add those to the total appropriately. I am quite chuffed with myself. I need to know now how to store values in some sort of database so that I can have a stored total calorie number and then eventually store separate data for days and each individual input.
edit: Is it possible to put code already within a sub routine into another one so that it can be minimized? Not really necessary I guess but would help with organisation.
Edited by centurion, 25 February 2011 - 04:13 PM.
#10
Posted 25 February 2011 - 03:48 PM
Didnt know you knew VB Sam. :P
Anyways centrurion, you could still use Sam's code with alil editing:
Anyways centrurion, you could still use Sam's code with alil editing:
If (Not Integer.TryParse(TextBox2.Text, input_number2)) Then input_number2 = 0 End IfI think that will work. ~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#11
Posted 25 February 2011 - 04:17 PM
What does TryParse do?
The current method works fine although takes up space when there are several boxes that each need it applying to.
See my previous post for most recent questions, only just edited it.
The current method works fine although takes up space when there are several boxes that each need it applying to.
See my previous post for most recent questions, only just edited it.
#12
Posted 25 February 2011 - 04:20 PM
TryParse is a beaut!
makes code really easy to read, and normally faster, heres why
if the string is not a number, or is even null, it will return false, if the string is parsed properly, it will return true
this way, you don't need to use a try/catch block. Try/Catch blocks have a lot of overhead.
I'll read back to your older post.
makes code really easy to read, and normally faster, heres why
if the string is not a number, or is even null, it will return false, if the string is parsed properly, it will return true
this way, you don't need to use a try/catch block. Try/Catch blocks have a lot of overhead.
I'll read back to your older post.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









