Quicker method than looping through thousands of records - vb.net

Is there a quicker way than looping through thousands of records (around 24k)?
Code:
For n = 0 To oCHStockItems.Count - 1
Dim itemSellingPrice As New CH.CH_ItemSellingPrice
With itemSellingPrice
.StockItem = oCHStockItems(n).StockItem
.Code = oCHStockItems(n).StockItem.Code
.Name = oCHStockItems(n).StockItem.Name
.ProductGroupCode = oCHStockItems(n).StockItem.ProductGroup.Code
.CurrentSellingPrice = oCHStockItems(n).StockItem.StockItemPrices(0).Price
.NewSellingPrice = 0D
.LastSellingPriceDate = oCHStockItems(n).LastSellingPriceDate
.OriginalPrice = oCHStockItems(n).OriginalPrice
End With
_itemSellingPrices.Add(itemSellingPrice)
Next
Originally I was assigning oCHStockItems to a Grid (it's actually a Sage 200 Grid) however I can't seem to find a way to reference to the field oCHStockItems(n).StockItem.StockItemPrices(0).Price.
Normally the above kind of syntax works. For example if I want to reference to the Stock Code it would be StockItem.Code.
StockItem.StockItemPrices(0).Price does produce a value however it doesn't show on the Grid. I've logged a ticket with Sage to see if they can help.
However I'm thinking they will come back and say that it can't be done and on that assumption I'm kind of left with looping through oStockItems and assigning the properties to the properties of my predefined class. So with that in mind has anyone any suggestions of speeding this kind of process up?

Sage got back to me with a solution:
First:
AddHandler dgItems.Items.ItemAdded, AddressOf Items_ItemAdded
Then:
Private Sub Items_ItemAdded(ByVal sender As Object, ByVal args As Sage.Common.Controls.ListItemArgs)
args.Item.SubItems(4).Value = oCHStockItems(args.Item.Index).StockItem.StockItemPrices(0).Price
End Sub
This quickly populates the item on the Grid.
All I have done is assign the original collection oCHStockItems to the .DataSource of the grid and the method Items_ItemAdded sorts the rest out.

Related

devExpress searchLookupEdit not setting value

I have multiple searchLookupEdit controls on a form. All but one are working.
here is the code to populate one that is working
Private _list As sbList
_list = ag.ListByName("RMATroubleCode")
sluTroubleCode.Properties.DataSource = _list.Items
sluTroubleCode.EditValue = _item.TroubleCode.Value
here is the one that will not work
Private Sub populate_SKUOrdered(ByRef ctl As SearchLookUpEdit)
Dim ag As New cpAssemblyQuery()
Dim l As List(Of cpAssembly) = ag.TopLevelAssemblies()
ctl.Properties.DataSource = l
End Sub
populate_SKUOrdered(sluCreateSKUOrdered)
sluCreateSKUOrdered.EditValue = _item.SKU_OriginallyOrdered.Value
I have verified that sluCreateSKUOrdered has a DisplayMember and ValueMember property set correctly. They are both set to "SKU" so that is pretty simple.
The dropdown does contain the expected # of items.
The SKU i am trying to set is 'RM45'. So thats pretty simple to verify. It is not a case of trailing or leading spaces...
Are there some trouble shooting steps I can take? I am confident this is a case of simple oversight, but I can not find anything on DevExpress to help me resolve this issue.
I am using a ORM, it is necessary to use the following code
ctl.Properties.DisplayMember = "SKU.Value"
ctl.Properties.ValueMember = "SKU.Value"
The other controls were using interfaces, or simpler objects, so there was not a good analogy there.

How can I cause a button click to change the image in a picture box?

I'm using VS2010 for VisualBasic, and I'm working with several similar forms. What I need to have happen is for the buttonclick on each page to cycle through the My.Resource images in order: adj_01, adj_02, adj_03,... and each form will have a different three-letter prefix.
This is what I have so far:
It might not be clear, but I'm trying to have the images cycle trough one after the other with each button click. Apparently there is an issue with either my referencing, or that the images are .png format. Simultaneously, I'm trying to have 2 separate label update information with each image change. This is what I have so far with that:
EDIT I just noticed an error that might confuse everyone on the photos: The first lines starting the If statements are checking to see if the PictureBox is empty. Needless to say, I don't know how to do that.
Here you go...
Private Sub NextAdjButton_Click(sender As Object, e As EventArgs) Handles NextAdjButton.Click
If AdjectivesPictureBox.Tag Is Nothing Then
AdjectivesPictureBox.Tag = 0
End If
Dim number As Integer = CInt(AdjectivesPictureBox.Tag)
If number < 5 Then
number = number + 1
AdjectivesPictureBox.Image = My.Resources.ResourceManager.GetObject("adj_" & number.ToString("00"))
AdjectivesPictureBox.Tag = number
End If
End Sub

Creating VB.Net forms via code instead of in design mode

I'm using netzero hardware to manage the contents of a number of monitors. My present solution creates a form in VB.Net that has a pixel offset corresponding to where I've placed the Monitors in display management in the control panel. Each monitor has a dedicated form, and in each form are various objects.
The annoyance is that each form must be individually created (so far as I know) at design time. I can't make an array of forms, coupled with an array of offsets and assign all the properties through code.
There ought to be a way to do this...it would simplify my coding and project management.
What I see on MSDN is either over my head or not helpful.
I haven't tested this in hardware yet, but it does compile w/o error:
Public Sub makeform()
Dim MonitorForm(21) As Form
Dim MPictureBoxes(21) As PictureBox
Dim a As Integer
For i As Integer = 0 To n 'up to 21
MonitorForm(i) = New Form
MonitorForm(i).Name = "Form" & (i + 1)
MonitorForm(i).Text = "Form" & (i + 1)
MonitorForm(i).Controls.Add(MPictureBoxes(i))
MonitorForm(i).Location= new Point (x(i), y(i))
With MPictureBoxes(i)
.Name = "Picture Box " & Convert.ToString(i)
.Image = Image.FromFile(CurrentPic(i))
.Location = New System.Drawing.Point(0, 0)
.Size = New Size(1920, 1080)
' Note you can set more of the PicBox's Properties here
End With
Next
End Sub
Where I had gone wrong in my attempts at this was trying to do it this way
Dim Monitor(21) as New Form
That doesn't work, and the difference between Dim Monitor(21) as Form followed by monitor(i)= new Form
was simply too subtle for my present understand of classes, namespaces etc.
.
Well, I've had to give up on this approach and go back to creating n forms at design time (which means that they have names of form2...form22, putting each of them at manual start positions in design mode. There just doesn't seem to be a way to do this with an array of forms. So the code I have built around the messiness of forms2...forms22 works just fine, it's just going to be messy to maintain and elaborate on.
The solution to this may lie in system.screen classes, but the documentation on this is too advanced for me and I'm not finding good code examples for anything other than extracting data about how many screens there are - nothing about writing to them.
This is very easy in code. You want to make many instances of the same form. In this case, I have created a form in the designer called frmTest and I create many instances in code called frmNew:
Public Sub Main()
For x = 100 To 400 Step 100
For y = 100 To 700 Step 200
Dim frmNew As New frmTest
frmNew.Show()
frmNew.Top = x
frmNew.Left = y
frmNew.Height = 100
frmNew.Width = 200
Next
Next
End Sub
I have just used two loops to increment x and y values, but you could do this from a database or config file easily enough.

Not all controls in collection are being copied

I'm a bit confused here. I'm copying all the controls from one form to a panel on the main form and for some reason only about half of them copy.
Private Sub switchComponent()
Dim selection As String = TreeView1.SelectedNode.Text
Panel1.Controls.Clear()
Dim query = From cont In serverDic(selection).Controls
Select cont
For Each copier As Control In query
Panel1.Controls.Add(copier)
Next
End Sub
serverDic is defined as:
Dim serverDic As New Dictionary(Of String, frmServer)
When stepping through the code, serverDic(selection).Controls has 12 elements, but only 6 of them get copied. Next time this gets called, only 3 get copied. Does Panel1.Controls.clear() somehow kill the references?
EDIT: Just to show that there are infact 12 elements in the collection:
The problem here is that you are iterating over a collection that you are changing. When you add a Control to an container it is implicitly removed from it's previous parent and hence query. This is why you see exactly half of the items get removed.
With most collections this would be more apparent because they would throw if modified during an enumeration. The primary source of query here though is ControlCollection which does allow for modifications while enumerating.
To fix this problem just add the following line before the For Each loop.
query = query.ToList()

Vb.net Gridview "pointer"?

I have code that interacts with a gridview, and the code is exactly the same for multiple gridviews. So can I do something like this:
Dim gridViewPointer As GridView
If (gridViewNumber = 1) Then
gridViewPointer = GridView1
ElseIf (gridViewNumber = 8) Then
gridViewPointer = GridView8
...
and then
If (gridViewPointer.DataSourceID = SQLDatasourcetemp.ID) Then
...
Will this work or is there another way to do this?
Edit:
I'm checking to make sure that the data the user is inputting into the gridview is correct. It could be one of 4 gridviews, and the checks are exactly the same, the only parameter that changes in the code is gridview1/gridview2/etc. So if I can use a "pointer" to the correct gridview then I can elimninate all the duplicate code.
Yes that is not a problem at all.
Whenever you assign an object to a variable you are actually assigning a memory reference to the variable. Using that reference you can read, write, and call all properties and methods of the object as if it were there original.
You might want to read up on the differences between value and reference types. This is primarily a concern when passing data through function calls.
http://msdn.microsoft.com/en-us/library/t63sy5hs%28VS.80%29.aspx
In fact I would probably create a new function to call on the gridview...
Private Sub GridOperations(ByVal grid as GridView)
//Do work here.
End Sub
If (gridViewNumber = 1) Then
GridOperations(GridView1)
ElseIf (gridViewNumber =8) Then
GridOperations(GridView8)
...
What you are asking is correct. When you set gridViewPointer = GridView1, you are actually only storing the pointer to the GridView1 object, not copying the object, so any action you perform on gridViewPointer after the set will directly control GridView1.