How to create a loop for: adding x number to a textbox, after every y number.

weldos

Member
Joined
May 19, 2013
Messages
5
Programming Experience
1-3
so pretty much what im looking to do is by using a loop of some sort ( my vb skills isnt that great, so im not entirely familiar with loops) to add 1 to a Textbox (in this case happens to be txtbCat1Items.text) for every 4 in the same text box.

question for reference/better understanding of what im trying to do: "For every 4 Category 1 items ordered, the 5th one is free" - so for every 4 items in category 1, add 1 to the items to make it 5, and i need to do this in some sort of loop form.
|

thankyou
 
You haven't really provided an adequate description. What exactly do you think you need to loop through? What exactly is in the TextBox? Is it one line or many? Just numbers or text? Where does it come from: does the user enter it themselves or does the app display it? How exactly is a Category 1 item to be identified? Explain it to us like we have no prior knowledge of your project... because we have no prior knowledge of your project.
 
ill post a picture of my program and a snippet of the code affecting the problem, pretty much what i want for the loop to do is that every time txtbCat1.text = 4, add 1 to the txtbCat1.text, then reset the loop, so if another 4 items come up again it adds another 1 to the textbox, so eg if it = 8 it adds 2 to the text box, if it = 12 then add 3, and so on (or in easier terms, for every multiple of 4 then txtbcat1items.text = txtbcat1items.text + 1. so pretty much for every 4th value, add 1 to the text box. (i was told something about a mod function would be good to use, but i havent really looked much into that function)


program interface.png

code snippet.jpg

it is kinda hard for me to explain it, so sorry if my explanation isn't helping much


Edit1: the little items textbox for category 1 is called txtbCat1.text, and that is where the value will be input in both manually and through the buttons on the side
edit 2: the Total Costs boxes are labelled Cat(whatever it is)cost.text in the code
 
Last edited:
Firstly, there's no place for a loop there. What exactly are you going to loop through? The user enters a number and you add 1 to it if it's 1 less than a multiple of 5. That's all there is to it.

You've got some things to consider though. For instance, if the user clicks the + or - buttons then you know for a fact that the current value is being incremented or decremented, so you can make the adjustment right there and then. If the user types a value in though, how do you know when they have finished typing? If they type in 4 then you might think to change that to 5 but what if they were intending to type in 41? Also, what happens if the current value is 5 and the user clicks the - button? You can't decrement the value to 4 because then you'd need to add 1 and make it 5 again, so you're going to have to decrement to 3.

This exactly how software development is. Rarely are things as simple as they seem and, in real-world apps, if you want to release a quality product then you end up spending 80% of your time writing code to deal with 20% of the possible states. So, I suggest that you spend a bit of time considering what states your app could be in and exactly what behaviour you want to see in those states. We haven't even touched on the fact that the user could potentially enter non-numeric values into a TextBox. I would suggest that, at least, you need to handle the Leave event of each TextBox and the Click event of each Button to make the appropriate adjustments to the values. If you are not allowed to assume valid input every time then it becomes more complex still.
 
Suggestions

How the Mod works: It gets the remainder of an integer division. Example:

mynum = 9
intdiv = mynum \ 4 'result is 2
modrem = mynum Mod 4 'result is 1


However, in your scenario, you have no need for the Mod operator. You would simply need to add the result of the integer division to the original number:
total = mynum + intdiv 'result is 11.

In other words, you would start with a final total value and then compute. Simply divide the total number by 4 (using integer division) to get the number of additional items, which should be kept separately in case the original total number is changed.

I would suggest using a NumericUpDown control instead of a textbox for user input, and a separate control(such as a label) for outputting the adjusted total with the additions. So if the nud control shows a value of 4, 5, 6, or 7, the label would show a total of 5, 6, 7, or 8, which includes the extra. If the nud control shows a value of 8, 9, 10, or 11, the label would show a total of 10, 11, 12, or 13. And so on.

Furthermore, your code is very poorly constructed. First and foremost, put Option Strict On at the top of your code. This will enforce correct type conversion. You need to convert all textbox string values into numbers before attempting to compute the results. Also, if a value is fixed and should not be changed by the user, you should be using labels, not textboxes, or else change the ReadOnly property of the textbox to True.
 
Last edited:
Back
Top