Text box not showing total - vb.net

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?

Related

VBA ShapeRange.Rotation Property randomly stops working

I am trying to rotate a shape. Below is the relevant snippet.
Sheets("Sheet1").Shapes.Range(Array("Down Arrow 8")).Select
Selection.ShapeRange.Rotation = 90 + Sheets("Sheet2").Range("H8")
My problem is that the last line randomly works and randomly doesn't! I used to have it as a number (instead of a to string) but this refuses to work! It worked fine, then I made some changes to the macro and then it helpfully returned Error 438 - Object doesn't support this property or method. I fiddled around, debugged, etc (without ever changing the code) and it started working! Then I made some more changes and it stopped working and it seems to have stopped working for good. I literally copied this code from a recorded macro.
What I have tried:
Using CStr() to convert the value to a string
Declaring a variable and using the variable
What I really don't understand is why it would work without me changing the code and them stop working without me changing the code (i.e. the relevant lines)
A few things. First, avoid select. It isn't necessary and will save you a lot of headache and time in the future. Second, you can reference the shape by its name right from the Shapes object, assuming you are only intending to rotate one shape with the code. Finally, are you certain the value in Sheets("Sheet2").Range("H8") is numeric? If not, this could cause an error. The code below handles the first two issues. Beyond that, we'll need to see more code to determine the error.
Sheets("Sheet1").Shapes("Down Arrow 8").Rotation = 90 + Sheets("Sheet2").Range("H8")

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.

VB.net Loop adding to combobox

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

VB 2010: ScrollToCaret won't work on plain textbox but will with rich text box

I have written an application in VB 2010 that generates log files, and I wanted to add a feature to view the log files without leaving the app. This was done, naturally, by adding a form with a text box and loading in the file.
Sometimes these files get plenty big, so I was looking for a way to just show the most recent entries -- i.e., the last few lines. I got that figured out (using a pretty cool method that works really fast without iterating through all the lines of text).
Just to make it convenient, I wanted to make it display the very bottom of the text box, so ScrollToCaret() was employed. However, in this particular text box, the ScrollToCaret() function didn't work, no matter how I tried to implement it. If I use a RichTextBox, it works fine, and if that's what I need to do, that's how I'll do it. But I have to know why it wouldn't work in a regular text box.
Here's my code:
Dim intLineCount As Int64 = IO.File.ReadAllLines(strFileName).Length
Dim intStartPosition As Int64 = intLineCount - 1000
Dim intPositionLoop As Int64
Try
Dim strAllLines() As String = File.ReadAllLines(strFileName)
For intPositionLoop = intStartPosition To intLineCount
txtViewLogs.AppendText(strAllLines(intPositionLoop - 1) & vbNewLine)
Next
Catch ex As Exception
End Try
txtViewLogs.Focus()
txtViewLogs.ScrollToCaret()
As this shows, this will display the last 1000 lines of the log file. When the file is completely loaded, the caret is already at the end of the file, so there was no need to set the SelectionStart point to the end of the file, but I even tried that, and no luck.
In order to eliminate the possibility that some property of the text box could have been accidentally changed, I deleted the text box and added another one, taking care not to make any accidental changes to the properties.
Anyone have any idea why it's like this? As I said, if I have to stick with a rich text box, that's what I'll do, but it kills me not knowing why this is doing that. I know I've used this in other applications, but I can't see why it won't work with this one.

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.