VB.net Loop adding to combobox - vb.net

Spent about an hour now, with multiple rages at my monitor. I have a variable which stores the amount of elements within an array.
I want to add the numbers '1' up to this variable number to a combobox. So that when i use the combobox, it gives the options of 1,2,3 etc up to the variable number. If anyone could help, that would a fantastic!!
Also, I tried a few different loops but caused visual studio to give an unable to access debug error when i tried to run the program. I am new to this so apologies if this seems basic.
The code I have below stores the count.
accno = custdetails.Count

You don't need to write the loop. This is a one-liner:
MyCombobox.Items.AddRange(Enumerable.Range(1, accno).ToArray())
I prefer that because it starts you thinking in terms of matching your presentation to a data source, which will help you a lot as you learn further.
But if you really want to:
For i As Integer = 1 to accno
MyCombobox.Items.Add(i)
Next

Related

In VB.net is there an option to force generation of the counter variable in the "Next" statement?

Visual Basic .net automatically inserts a "Next" statement when you enter a "For" statement; e.g., if I type:
For i as integer = 1 to 10
a Next will automatically be inserted, so that the code looks like:
For i as integer = 1 to 10
Next
It is optional to put the counter variable ("i" in the above) in the Next statement, so that it would read:
Next i
I'd really like to make this the default, as it really helps when one has nested For statements. I can't find anything in the Visual Studio settings to do this; maybe it's partly buried somewhere in Intellisense. I thought perhaps someone out there has already figured this out.
You will want to modify the .snippet file associated with the pattern. For the one you are looking for it is, by default:
VisualStudioInstallDirectory\VB\Snippets\1033\common code patterns\conditionals and loops\ForEachNext.snippet
You can also track down the exact location by looking in
Tools > Code Snippets Manager..., select Visual Basic for the language and browse to Code Patterns - If, For Each, Try Catch, Property, etc - the file location will be listed there.

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

Editing contents of a labview array

I'm trying to increment specific elements by 1, in order to log results as they come in. I'm trying to read an element, add 1 to it, and then write it back to the same memory address. Why isn't this simple?
In code it would be something as simple as;
array1[element1] = (array1[element1]+1)
or
array1[element1]++
Arrays seem to be either read (indicators) or write (controls)? This is really frustrating, and there's very little help online.
You can use an "Array index/replace" element inside a "In place element structure":
You should use ReplaceArraySubset in the Array palette. For simple replacements, it's much faster than the In Place Element Structure
As an infrequent, novice Labview user I have the same problem ... until I found the code that I used 10 years ago. Surely the answer to sgccarey is:-
Right click on the array control or indicator and 'create local variable'
This variable will appear on the block diagram and can be set as 'Change to Write' or 'Change to Read' as necessary to use as the input and / or output array to a simple 'replace array subset'.
This way the array data only appears once on the Front Panel and is updated as required.
I have no idea if using Local Variables affects runtime efficiency but it works for me. Hope this helps.

VB.Net Events Practice Help

I was given an assignment to "write a program to print only even numbers between 6 and 16 using events", but I don't even know where to begin. The main thing I am having difficulty with understanding in this assignment is how I am supposed to specify that it only print even numbers in the given range of numbers.
Am I going to have to do a Mod2 code for each individual number and have it exclude any with the result of 1? Or is there another piece of code specifically designed for such an occassion? Perhaps there is some type of equation I can have the program read in terms of a variable, which holds the values of 6, 8, 10, 12, 14, and 16? I am just genuinely confused on how this is supposed to be programmed. Any assistance would be greatly appreciated.
No offense, but I'm not at all convinced that you are accurately relaying you assignment, but...
Firstly, using MOD is a good starting point, particularly as you are supposedly tying this in with events...
To use events as part of your solution, I would suggest creating a textbox withevents and a handler for textchanged, then in a loop set the txtbox.Text property to the string representation of the loop index (say going from 1 to 20), then inside the textchanded event turn the propery back into an integer, check to see if it's within the proper range and even(using, as you suggested, MOD).
The following will help you solving a part of the problem:
Dim number as Integer = 6
While number <= 16
// PRINT Goes here ..
number = number + 2
End While
These Microsoft links are good learning resources:
Learning Visual Basic from the Ground Up
Getting Started with Visual Basic
Video How to: Creating Your First Visual Basic Program
Events in Visual Basic
Closer Look: Understanding Properties, Methods, and Events
Events and Event Handlers

Text box not showing total

In my program I want to add some values together with a running total, and then show that total in a text box. When I try to run it, though, it just shows zero.
Here's the code I'm using
TotalPrice = TotalPrice + Price
Next
TxtLuggage.Text = TotalPrice
this chunk was a part of a For next loop.
How do I fix this.
Please do not take this as an offense, but I think you should read some book or at least a decent article on .NET debugging. This one looks like an easy to spot with just a bit of basic debugging.
See this one for example, and pay special attention on stepping through code, setting breakpoints and watches
What you should do:
set a breakpoint in the code in the problematic code line
when debugger breaks code execution there, see which object are you changing the property for. Does this property change the text box text value? Are you using a correct form instance?
does the For..Next loop set it to zero on its final pass?