[quote=493648:@Tim Hare]If I had a nickel for every time I’ve seen this coding error (or done it myself). Markus alluded to it, but let me explain it a little more fully. This code will only work if i is equal to awayMax (or if the selected item happens to be 5). Say awayMin is 4 and awayMax is 7 and the desired index is 6. The code will do the following:
i=4: doesn’t match, set SelectedRowIndex = 5
i=5: doesn’t match, set SelectedRowIndex = 5
i=6: matches, set SelectedRowIndex = 6
i=7: doesn’t match, set SelectedRowIndex to 5
So when it finishes, SelectedRowIndex is wrong. You need to either save the rowindex in a temp variable, or exit the loop when you find the match. I would go with a temp variable.
[code]
dim rowindex as integer
rowindex = 5 // initialize to default value
For i As Integer = awayMin To awayMax
AwayTempPopupMenu.AddRow Str(i)
If i =Clone_of_Heating_Prefs_Original_Array(2, Economy_or_Normal, 6, Zone_Selected, 5) Then
rowindex = AwayTempPopupMenu.LastAddedRowIndex
end
Next
AwayTempPopupMenu.SelectedRowIndex = rowindex
[/code][/quote]
Markus did point it out last night and although I did not write it out, which illiterates it’s not a bad thing to do if questionable your exclamation makes it obvious. I dithered with it this morning and although I was maybe close to your thinking I added more code than necessary. Yours is simple. I concluded I did not want a default value or the operator would thing an entry was already set. So I need it set to a blank or -1.
The workaround which I was focusing on last night was how to set a value from the popup into the clone array when it is the correct time to do so. When a new heating zone selected with a button is made the first time, an associated away temp needs to be set. Unfortunately, this zone selection would trigger the change event in the popup which would be premature and fire a change event in the popup menu then store an incorrect value in the clone array.
My workaround was that both the change event and mouse in the popup menu needed to be seen at the same time. Seem to work fine but I bet someone has a better idea of how this should be done.
Incedetently, yes I do a one to one copy to make an authentic clone. I store all newly entered values in the clone and before saving cross-check with the original ref clone to see if changes have been made then allow an option to cancel or save. Seems to work slick.
Thanks, Tim & Markus
This was the code I came up with last night after hear for Markus which actually does look like yous. Rowindex was a parameter I see as was my FoundAt. I first thought it was a built in parameter of the popup. Not so.
[code]Var FoundAt As Integer =-1 //Set to blank if no entry found
AwayTempPopupMenu.RemoveAllRows // Remove all rows first
//Populate Popup Menu
For i As Integer = awayMin To awayMax
AwayTempPopupMenu.AddRow Str(i)
//Find Away Temp in stored clone array
If i = Clone_of_Heating_Prefs_Original_Array(2, Economy_or_Normal, 6, Zone_Selected, 5) Then
FoundAt = AwayTempPopupMenu.LastAddedRowIndex
end
next
//Set Popup menu to value
AwayTempPopupMenu.SelectedRowIndex = FoundAt[/code]