Visual Basic 2010 Questions

Associate
Joined
14 Oct 2009
Posts
502
Location
Newport, Shropshire
Hi all,
Recently started computing and am building my own Army Building software for Warhammer 40k via Visual basic.
however i have run into an issue.
Basically i have the total of the army at the bottom, to add a unit to the army you press a button then select the unit, each unit is given a number, then you enter the quantity of the unit, then it displays the unit you have chosen in one text box and the points cost of that one unit on the other text box. The issue i have is that if you put in a unit in say Elite slot 1 which is say 280 points. And then change it to a different unit which costs say 280 points again the army total will be 560 points, rather than when you press the button to change the selection it resetting the points cost.

Dim Elite1 As Integer
Dim Elite1Qty As Integer
Dim Elite1Total As Integer

Elite1 = InputBox("Please Input your elites choice, 1 = Pariah, 2 = Flayed one and 3 = Immortal ")

If Elite1 = 1 Then
Elite1Qty = InputBox("Input the number of pariahs you want")
Elite1Total = (Elite1Qty * 36)

Elite1_txt.Text = ("Pariahs x " & Elite1Qty)
End If

If Elite1 = 2 Then
Elite1Qty = InputBox("Input the number of Flayed ones you want")
Elite1Total = (18 * Elite1Qty)

Elite1_txt.Text = ("Flayed ones x " & Elite1Qty)
End If

If Elite1 = 3 Then
Elite1Qty = InputBox("Input the number of Immortals you want")
Elite1Total = (28 * Elite1Qty)

Elite1_txt.Text = ("Immortals x " & Elite1Qty)
End If

If Elite1 replace Then


End If

Elite1Totaltxt.Text = Elite1Total
Army_total = Army_total + Elite1Total
Armytotaltxt.Text = Army_total

This is the piece of code. I am running the Army total at the end of all the IF statements should i run it inside each of the IF statements?
Or i was wondering if i could use an if statement which triggers when the button is pressed a second time, which then resets the value of Elite1Total, therefore not totalling up the points cost.

Sorry for the wall of text but im new to this all, this is my 4th week of the course And i know its an extremely basic piece of software but this is where im starting from so any help would be great!

Cheers
 
This is happening because your integer variables(Elite1Total specially) are not re-initializing(to 0) when you change your selection.
There are couple of ways you can avoid this.
 
Thats exactly what i was trying to figure out,
Any ideas, So far ive only worked with integers and Singles (still no idea what that does) so is there something i can change the integer to which makes it change to 0 if the button is pressed again?
Also Another question, Is there a way i can use the integers from one private sub in another button without redeclaring them, i am thinking of putting a button to the side which when pressed clears all the values of the integers inside the private sub but primarily Elite1Total

Thanks
 
Put a combo box with your multiple choices, text box for your inputs numbers and a command button for the calculating process.
That way user can select the unit by using the drop down combo box and then enter the number of units in the adjacent text box.
Then user can press the command button to calculate the value of the unit

Get it? :)


If you declare the variables inside a sub routine(Private) in a form, the life of that variable is only to that sub routine.

If you declare it in the top section(just below the "Option Explicit") of the form variable can be accessed throughout the form only(Private form)

If you declare the variable in a module(public) a variable can be accessed any where in the project.
 
I looked into combo boxes but could not find out how to populate them without a data source, so i scrapped that and went with input boxes as im familiar with them.
And to change a private form to public form do i just change the word private at the front to Public?
 
hmm dont have VB installed right now :( but I am sure you can add items to combo boxes with out a data source!
And yes you can change it to public, but be careful when doing so, this makes the variable accessible anywhere with in that parameter.

I am trying not to give you the answer :)
 
Thanks for the help so far,
Ill be having mine shortly :P
I would rather not re-write the whole thing to use combo boxes as i have already written 80% of the code, just need to finish off a few more categories and eliminate small problems like this.
I have a feeling there is a command you type after declaring Elite1total as an integer, im thinking something like Dim Elite1total As integer = Reset to 0, Or something similar.

Thanks so far though
 
Cheers for the help so far,
But i have added in Elite1total = 0 after the If statements and it still wont reset the value, Also i tried inputting redim elite1total and it gave me a rather complicated error.
I dont suppose you have steam? if so my username is in my sig, Would be a great deal easier then constantly replying to this thread :P

Thanks
 
No steam, I am using a backup PC till I re-do my water loop and upgrade and case mod and hundreds of other stuff ;)
Give me a minute let me see
 
OK just a quick fix!

Put Elite1Total = 0 at the end like this(remove other Elite1Total = 0 syntaxes)
================
Elite1Totaltxt.Text = Elite1Total
Army_total = Army_total + Elite1Total
Armytotaltxt.Text = Army_total

Elite1Total = 0
================

Edit
oh and Army_total = 0 as well
so it should be

Elite1Totaltxt.Text = Elite1Total
Army_total = Army_total + Elite1Total
Armytotaltxt.Text = Army_total

Elite1Total = 0
Army_total = 0
 
Dim Elite1 As Integer
Dim Elite1Qty As Integer
Dim Elite1Total As Integer

Elite1 = InputBox("Please Input your elites choice, 1 = Pariah, 2 = Flayed one and 3 = Immortal ")

If Elite1 = 1 Then
Elite1Qty = InputBox("Input the number of pariahs you want")
Elite1Total = (Elite1Qty * 36)

Elite1_txt.Text = ("Pariahs x " & Elite1Qty)
End If

If Elite1 = 2 Then
Elite1Qty = InputBox("Input the number of Flayed ones you want")
Elite1Total = (18 * Elite1Qty)

Elite1_txt.Text = ("Flayed ones x " & Elite1Qty)
End If

If Elite1 = 3 Then
Elite1Qty = InputBox("Input the number of Immortals you want")
Elite1Total = (28 * Elite1Qty)

Elite1_txt.Text = ("Immortals x " & Elite1Qty)
End If

If Elite1 replace Then


End If

Elite1Totaltxt.Text = Elite1Total
Army_total = Army_total + Elite1Total
Armytotaltxt.Text = Army_total

Elite1Total = 0
Army_total = 0
 
Cheers for the help,
It does not want to recognise the Replace Then on the last IF statement.

EDIT: Got it working, I jsut removed the If statement starting with replace then and it all seems to be working perfectly, will implement this over all of the other options and see what i can do.

Cheers
 
Last edited:
I deleted that and it is working for Elite choice one, just tried it on elite choice 2 and im yet to get it working.
Does the statement actually do anything? It appears to just open an if statement and then End it?
 
The problem appears to be fixed,
Its just messing up the Army_total now, I cant seem to update it properly now, Looks like thigns are really goind downhill with this.
 
In Form_Load() even put this
========
Armytotaltxt.Text = 0
Elite1Totaltxt.Text = 0
Armytotaltxt.Enabled = False
Elite1Totaltxt.Enabled = False

========

then the rest like this


=========
Dim Elite1 As Integer
Dim Elite1Qty As Integer
Dim Elite1Total As Integer

Elite1 = InputBox("Please Input your elites choice, 1 = Pariah, 2 = Flayed one and 3 = Immortal ")

If Elite1 = 1 Then
Elite1Qty = InputBox("Input the number of pariahs you want")
Elite1Total = (Elite1Qty * 36)

Elite1_txt.Text = ("Pariahs x " & Elite1Qty)
End If

If Elite1 = 2 Then
Elite1Qty = InputBox("Input the number of Flayed ones you want")
Elite1Total = (18 * Elite1Qty)

Elite1_txt.Text = ("Flayed ones x " & Elite1Qty)
End If

If Elite1 = 3 Then
Elite1Qty = InputBox("Input the number of Immortals you want")
Elite1Total = (28 * Elite1Qty)

Elite1_txt.Text = ("Immortals x " & Elite1Qty)
End If


Elite1Totaltxt.Text = Elite1Total
Army_total = cint(Armytotaltxt.Text) + Elite1Total
Armytotaltxt.Text = Army_total

Elite1Total = 0
Army_total = 0
 
Back
Top Bottom