for an assignment in my class I had to make a super simple point of sale system and one of the features required was tracking how many of each item is sold. I know I can do it with If statements such as
If DropDownList.SelectedIndex = 0 Then
ddl1Tracker += 1
ElseIf DropDownList.SelectedIndex = 1 Then
ddl2Tracker +=1
Etc...
End IF
but I was wondering if there was a better way to do it so I don't need to make a variable to track each individual item?
Thanks
You could replace a bunch of ddlNTracker variables with an array:
Dim ddlTrackers(ddlCount-1) as Integer
And then replace the whole If section with a single function call:
ddlTrackers(DropDownList.SelectedIndex) += 1
Related
I have a little problem: I want to make some app in VB and I make two CheckedListBoxs and I have some idea: if I chose something in CheckedListBox1 I want to show some date inside CheckedListBox2.
I have a problem with declaration - I make something like this:
Dim model3 = {"A", "B", "C"}
But I have only one information inside CheckedListBox2: 'String[]'
If CheckedListBox1.CheckedItems.Count <> 0 Then
If CheckedListBox1.SelectedItem.ToString = "GWW" Then
Marka.Items.Add(model1)
ElseIf CheckedListBox1.SelectedItem.ToString = "AWW" Then
Marka.Items.Add(model2)
ElseIf CheckedListBox1.SelectedItem.ToString = "ZWW" Then
Marka.Items.Add(model3)
End If
Else
Marka.Items.Clear()
End If
Could you give me some prompt? I do not have too much experience so if I could asked as simple as possible :)
That's because you're only adding one item - the array itself - and the CheckedListBox will call its ToString method to get text that it can display, which is what you see. If what you actually want to do is add all the elements in the array into the CheckedListBox then you need to call AddRange rather than Add.
For the next part of this project that I've been working on to speed up follow up emails to clients for the office, I'm looking to grab a specific attachment from a specified filepath based on the items that the user selected on the userform. These emails will always be sending the exact same files so the less time the user has to spend manually attaching files, the better it will be. My first assumption right off the bat was that I'd need a loop to do this, so I began to do my groundwork, but now I'm generally stuck.
The first loop grabs what the user selected from the userform:
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
Counter = Counter + 1
msg = msg & "<font style = 'background: yellow'>" & List1.List(i) & "<br />"
Else: If Counter = 0 Then End
End If
Next
And the second loop attaches the files based on the selections above:
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
Counter = Counter + 1
.Attachments.Add List1.List(i)
Else: If Counter = 0 Then End
End If
Next
The attachments process just fine.However, the program ends up displaying the highlighted body of the message as the filepath I tried to associate with the list item:
' test files
List1.List(0) = "C:\Users\jmarkman\Dropbox\Python Practice\ex1.py"
List1.List(1) = "C:\Users\jmarkman\Dropbox\Python Practice\ex2.py"
List1.List(2) = "C:\Users\jmarkman\Dropbox\Python Practice\ex3.py"
List1.List(3) = "C:\Users\jmarkman\Dropbox\Python Practice\ex4.py"
List1.List(4) = "C:\Users\jmarkman\Dropbox\Python Practice\ex5.py"
List1.List(5) = "C:\Users\jmarkman\Dropbox\Python Practice\ex6.py"
So my question is, how do I associate these file paths with corresponding items in the listbox within the userform? I'm pretty sure that although it worked, the process changed the list items since I assigned them a different value.
I'm not sure how complex or simple this might end up being, so your time and patience are well appreciated.
People have said that one can learn best just by doing and making mistakes, but let me tell you that not having someone you can at least talk to about where to start making mistakes is incredibly frustrating. I hope anyone else who, like me, was on the cusp of solving this problem is able to look here and have their own "a-ha!" moment.
When I first asked this question, I was on the right path with choosing to put all of my filepath locations within an array and I was further along the right path by using a loop to go through it. I declared the myAttach array with a range as I did at first, but unlike whatever example I saw during my google research, I kept my second attachment loop the same but simply changed out List1.List(i) for myAttach(i). That made the script's wheels turn and performed all the functions I needed it to.
The long and short of it was that I made an array that matched the quantity and order of the items in the userform and re-used the loop I made for picking the subjectivities, I was able to associate the choices in the userform to those in the array by having the second loop go through the array. Each listing in the array had a filepath associated with it.
' example
myAttach(0) = "[filepath]"
myAttach(1) = "[filepath]"
myAttach(2) = "[filepath]"
myAttach(3) = "[filepath]"
' ... and so on ...
It helped to visualize the array and the list items as two side-by-side columns; the first item in the list is parallel to the first item in the array, and can therefore be recognized by the second loop. If a mental image isn't forthcoming, try inputting some data into two or more Excel columns to witness the relationship or review Matricies.
A caveat of this line-up approach, I found out shortly after while continuing research to figure out exactly what I did right this time would be if I had to have the items in the list in a specific order (i.e., alphabetical), but fortunately the list items are phrases and complete sentences and don't require sorting.
Happy learning and programming!
This code doesn't seem to be working. I don't really know what loop to use to get it too add the information the user puts into the machine to print again.
The aim of this is for the user to either pick:
to print a menu that they have typed in an earlier database. If they haven't typed anything into the database, then it should be blank
Should let the user enter information into the database (What I am mostly struggling with) and do error checking so that it tells them if they have entered a number when they should have entered a letter and
To end the program.
When I do (2) It lets me type but it doesn't recall the information back in the database. Also it needs a number (4) which should return to the main menu. I think this is where the loops come in but I don't know which one to use.
Here is the code:
Structure Cars
Public carmake As String
Public carmodel As String
Public caryear As Integer
Public carlicence As String
End Structure
Module Module1
Sub Main()
Dim userchoice
Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")
userchoice = Console.ReadLine()
If userchoice = 1 Then
Dim cardatabase(4) As Cars
Console.WriteLine("This will allow you to view the database.")
Console.WriteLine("Please enter the car make,licence,model and year")
cardatabase(1).carmake = Console.ReadLine()
cardatabase(1).carlicence = Console.ReadLine()
cardatabase(1).carmodel = Console.ReadLine()
cardatabase(1).caryear = Console.ReadLine()
ElseIf userchoice = 2 Then
Console.WriteLine("The database is,")
ElseIf userchoice = 3 Then
Console.WriteLine("Thanks for using this program.")
End If
Console.ReadLine()
End Sub
End Module
You have several issue with this code. Here's what I would recommend:
You need some sort of looping structure such as a While loop. Your test condition could be the userchoice variable.
In your If statements, you need to check if userchoice is equal to a string value instead of an integer value. So the line If userchoice = 1 Then should actually be If userchoice = "1" Then.
The cardatabase array should be declared outside of the loop. When you declare it in the loop, it will keep re-creating the array instead of adding more items to it.
Your Cars structure needs to be inside the Module Module1 block.
Since you don't know how many times the user may want to add a new car before they quit, I'd recommend using a List instead of an Array. Lists allow for easy dynamic re-sizing.
You need an integer variable to track the number of cars entered and use this as an index for your cardatabase collection.
Array/List indexes start with 0.
Cars should probably be a class instead of a structure. Also, it should be named Car. It's a single structure (or class, if you change it). The array itself should be called cars as it is a collection of a multiple Car structures (or objects if you change it to a class).
I was going to write example code here to demonstrate my points but it would be nearly an entire re-write and this would not help you understand why I made the changes I did.
My best advice to you is to go back through your book or tutorials that you read previously to get to this point and really try to understand what they are saying. If you don't get the concept, look it up elsewhere until you do.
Disclaimer: My recommendations are not comprehensive. I stopped examining your code when I identified all of the above issues right off the bat.
Is there any way to create class instances on the fly and refer to them later? I have a class with various methods and properties designed to hold & calculate product data. I'd like the application to be able to handle as many individual products as the user needs. This code clearly won't work but it should give you an idea of what I'm asking:
For x = 1 To howEverMany
Dim product_ & x.ToString() As New myProductClass
Next x
I appreciate this may not be the best approach (I should probably use lists or arrays to hold the product data) but I'm curious as to whether this is possible from a technical standpoint. I'm using VB.Net but answers in any .Net language will be welcome. Thanks.
Store them in a List(Of T). Then you can LINQ one or more objects in the list.
'class level
Private Products As New List(Of myProductClass)
'place where you load them
For x As Integer = 1 To howEverMany
Dim myc As New myProductClass
mpc.Id = x
'set other properties as needed
Products.Add(mpc)
Next
Get one by it's Id:
Dim mpc4 = (From p In Products Where p.Id = 4).FirstOrDefault
If Not mpc4 Is Nothing Then
'object exists
End If
You can make other similar queries on your List as well.
I have given each checkbox item on my application a value. I have group of checkboxes and I need to add up their total value base on user selection. I am using this code but it's not working for sum. I do not want to use if a.checked then add this elseif this checked add that kind of way.
Dim abe As CheckBox = groupAlumni.Controls.OfType(Of CheckBox)().Where(Function(r) r.Checked = True).Sum()
MsgBox(abe.Tag)
.sum part is giving an error. How can I achieve this?
Option 2
Also I am thinking I could change checkbox names to cb0 to cb15 and create a while loop and check them one by one to see which ones are selected and add up their values. How can I change cb number during while loop?
I wrote this but I really need cb0 to be cb1,cb2 and continue until cb15 during the loop.
If anybody answers either option it's fine for me. Thank you!
Dim counter As Integer = 0
Dim cbScore As Integer = 0
While counter < 15
If cb0.Checked Then
cbScore += cb0.Tag
End If
counter += 1
End While
If you only need the sum of the values, try this:
cbScore = groupAlumni.Controls.OfType(Of CheckBox)().Where(Function(r) r.Checked).Sum(Function(r) CInt(r.Tag))
Removed due to not compiling and incompatible methods.