VB 2-Dimensional Array Item comparison to image - vb.net

Why does the messagebox show "False"?
Dim images(4, 4) As Image
For rows = 0 To 4
For columns = 0 To 4
images(rows, columns) = My.Resources.kaboom
Next
Next
MessageBox.Show(images(3, 3).Equals(My.Resources.kaboom))

If you look at the code behind the kaboom property, you will see it creates a new object every time.
'''<summary>
''' Looks up a localized resource of type System.Drawing.Bitmap.
'''</summary>
Friend ReadOnly Property kaboom() As System.Drawing.Bitmap
Get
Dim obj As Object = ResourceManager.GetObject("kaboom", resourceCulture)
Return CType(obj,System.Drawing.Bitmap)
End Get
End Property
If you keep a reference to one object, it will be equal to true. It might also be faster since it doesn't need to create a new object.
Dim kaboom As Image = My.Resources.kaboom
Dim images(4, 4) As Image
For rows = 0 To 4
For columns = 0 To 4
images(rows, columns) = kaboom
Next
Next
MessageBox.Show(images(3, 3).Equals(kaboom))
Maybe you are already planning on doing this but here is a suggestion. If you are creating some sort of game, separate the display from the game logic. This mean, save the type of tile instead of the image and compare that. Later, you can add a bunch of different properties to a tile.
Const TYPE_KABOOM As Integer = 1
Dim tileType(4, 4) As Integer
For rows = 0 To 4
For columns = 0 To 4
tileType(rows, columns) = TYPE_KABOOM
Next
Next
MessageBox.Show(tileType(3, 3).Equals(TYPE_KABOOM))

Related

Random images on buttons

Good evening made the following code, create an array of buttons and panels, now how do I insert random images to those buttons, can you help me please.
Public Sub crearBotonesPaneles(ByVal creaBoton(,) As Button, ByVal creaPanel As Panel)
Dim puntoLocacion As Point
puntoLocacion.X = 20
puntoLocacion.Y = 40
For filas As Integer = 0 To 1
For columnas As Integer = 0 To 3
If IsNothing(creaBoton(filas, columnas)) Then
creaBoton(filas, columnas) = New Button
creaBoton(filas, columnas).Location = puntoLocacion
creaBoton(filas, columnas).Width = 50
creaBoton(filas, columnas).Height = 50
creaPanel.Controls.Add(creaBoton(filas, columnas))
puntoLocacion.X = puntoLocacion.X + 50
End If
Next
puntoLocacion.X = 20
puntoLocacion.Y = puntoLocacion.Y + 50
Next
End Sub
Doing something random ALWAYS means generating one or more random numbers in an appropriate range and then using them in an appropriate manner. The way you determine the range is application-specific and the manner in which you use the number(s) is application-specific. In your case, you might get the paths of all the image files in a folder, use random numbers to order those paths randomly and then use the paths one by one to get the images from the files. E.g.
Private rng As New Random 'Random number generator
Private imagePaths As Queue(Of String)
Private Sub LoadImagePaths()
'Create a new queue of file paths sorted based on a random number mapped to each one.
imagePaths = New Queue(Of String)(Directory.EnumerateFiles(My.Computer.FileSystem.SpecialDirectories.MyPictures,
"*.jpg").
OrderBy(Function(s) rng.NextDouble()))
End Sub
Private Function GetNextImage() As Image
'Load the image paths is there is no queue or the current queue is empty.
If imagePaths?.Any() = False Then
LoadImagePaths()
End If
'Create an image from the next file in the queue.
Return Image.FromFile(imagePaths.Dequeue())
End Function
Based on that code, you just call GetNextImage each time you need a random image. There will be no repeats until the entire list is exhausted.

CorelDraw selecting specific shapes in a collection

I'm trying to build a small nesting Function inside of my current working system. What I am having trouble with is that when I add the third shape to the collection and try to position it based on the previous shape added to the collection, it still positions it based on the very first shape. What I end up with is the original in the original position and the copies stacked on top of one another.
Function ArrangeImages(ByRef scol1 As Collection, ByRef sA, sB As Shape)
Dim i, ii As Long
i = scol1.Count
ii = i - 1
If i = 1 Then
Set sB = scol1.Item(i)
End If
If scol1.Count > 1 Then
Set sA = scol1.Item(ii)
Set sB = scol1.Item(i)
sB.SetPosition sA.PositionX, sA.PositionY + (sA.SizeHeight / 2) +
(sB.SizeHeight / 2) + 0.15
End If
End Function
You are setting the objects equal to each other and they reference each other in memory. In fact, you are doubling the object and using up twice as much memory.
Set sets an object equal to the reference of the object you want it to be. Here is an example.
Public Sub test()
Dim s As Worksheet
Dim s2 As Worksheet
Set s = Application.Workbooks.Add().Worksheets.Add
s.Name = "one"
Set s2 = s 's and s2 Name = "one"
s.Name = "two" 's and s2 Name = "two"
End Sub
Let sets an object equal to the value of the object you want it to be.
Try
Set sA = new Shape
Let sA = sB
Instead of
Set sA = sB
If that doesn't work, you may have to create a property or variable for each of the shapes you are wanting to work with.
Similar Question

How do I add to this array?

I have an object called pushOrderIncQTY. I can insert one item into the array using the I have an overload which accepts multiple items. My question is: how do I resize the array and add a record to it?
Dim pushOrderIncQTY() As infoPushOrderIncQTY
With pushOrderIncQTY
.costPrice = thisentry.Item("costPrice")
.externalTimeStamp = DateTime.Now()
.RootPLU = thisentry.Item("tagbarcode") 'set this to the barcode from the file
.sizeBit = -666
.supplierID = cfb.SupplierID
.orderReference = thisentry.Item("OrderNumber")
.orderLineReference = ""
.externalTransaction = ""
.sourceShop = cfb.SiteId 'set to the GEMINI location ID for this store (you will have to get this from your configuration file
.destinationShop = cfb.SiteId 'set this to the same as the sourceshop
.QTY = thisentry.Item("ActQty")
.whichQty = LiveSales.infoPushOrderIncQTY.Which_OrderQty.delivered 'only available option at present
End With
Edits here
Ok So i Can go with a list I tested this code and its putting int 16 records fine but how do I get it to go in batches
Dim pushOrderInQty As New List(Of infoPushOrderIncQTY)()
For Each thisentry2 In orderLineData.Rows
With pushOrderIncQTY
.costPrice = thisentry2.Item("costPrice")
.externalTimeStamp = DateTime.Now()
.RootPLU = thisentry2.Item("tagbarcode") 'set this to the barcode from the file
.sizeBit = -666
.supplierID = cfb.SupplierID
.orderReference = thisentry2.Item("OrderNumber")
.orderLineReference = ""
.externalTransaction = ""
.sourceShop = cfb.SiteId 'set to the GEMINI location ID for this store (you will have to get this from your configuration file
.destinationShop = cfb.SiteId 'set this to the same as the sourceshop
.QTY = thisentry2.Item("ActQty")
.whichQty = LiveSales.infoPushOrderIncQTY.Which_OrderQty.delivered 'only available option at present
End With
recordCount = recordCount + 1
pushOrderInQty.Add(pushOrderIncQTY)
Next
CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())
What I want is the ability to set batch size in cfb.batchsize which is a wrapper for my config file, what I want is say if their is 20 records and batch size is 5 it means the web service should be only called 4 times and not 20 individual times? and the records that get added to list is only 5 till the records set is completed?
You have asked two questions. So here is a separate answer to your second question. You simply need to count the reps within your loop. When you hit your batch size, call your web service and empty your list (to start over with a fresh batch). Like this:
Dim pushOrderInQty As New List(Of infoPushOrderIncQTY)()
For Each thisentry2 In orderLineData.Rows
With pushOrderIncQTY
.costPrice = thisentry2.Item("costPrice")
.externalTimeStamp = DateTime.Now()
'...etc
End With
recordCount = recordCount + 1
pushOrderInQty.Add(pushOrderIncQTY)
If recordCount >= cfb.BatchSize Then
CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())
pushOrderInQty.Clear()
recordCount = 0
End If
Next
'get the last, partial batch
If pushOrderInQty.Count > 0 Then
CallWebSerivce(wrpPush, request, pushOrderInQty.ToArray())
End If
You can use ReDim to resize any arry, but it wipes the contents. ReDim Preserve will retain the contents.
ReDim Preserve pushOrderIncQTY(5)
'or just make it one element larger, which is something you DO NOT want to do in a loop
Dim oldSize as integer = UBound(pushOrderIncQTY)
ReDim Preserve pushOrderIncQTY(oldSize + 1)
Btw, you could also define it as type List(of pushOrderIncQTY) and then after the list is loaded, you could use the .ToArray() method to convert it to an array.

returning a button from a function VB.net

In my program I have to reference different dynamically created buttons and they are stored in an array of records. Depending on what FieldShow is equal to, one of three FlowLayoutPanels are visible, which contain the buttons.
Private Function GetFields(i As Integer) As LoadingTemplate
Dim TempFields As LoadingTemplate
If FieldShow = 0 Then
TempFields.B = Study(i)
ElseIf FieldShow = 1 Then
TempFields.B = Task(i)
ElseIf FieldShow = 2 Then
TempFields.B = Note(i)
End If
Return TempFields
End Function
TempFields.B.RefBtn, if used here, then shows the button that is being returned, But if elsewhere I put
GetFields(x).B.RefBtn
with an appropriate value for X, it sometimes doesn't return things. Is there an easier way of doing this?
Thanks,
Henry

How to Implement Rowspan in a GridView (for .NET 3.5)

I created a custom GridView (actually a RadGrid) which does roughly the following.
Initialize GridView formatting
Setup Columns (depending on type of data to be displayed)
Populate the rows
When Instantiating this GridView, and adding it to my page, I want the first column to contain a "rowspan" attribute on the first row of repeated, but similar data. The "rowspan" value should be equal to the number of similar rows following it. In this way I hope to make the final view cleaner.
The logic for this, I figure, should take place while populating the rows. Initially I add rows to a DataTable and then bind it to the GridView as the final step.
Here's the general logic I was attempting, but it didn't work for me. What am I doing wrong?
Dim dt As New DataTable() 'Has three default columns
For Each d In Documents 'MAIN ITEM (First Column Data)
Dim rowspan As Integer
rowspan = 0
For Each f In Files
If rowspan = 0 Then
Me.dt.Rows.Add(New Object() {d.Title, f.Language, f.FileSize})
'THIS DOESN'T WORK!
Me.dt.Columns(0).ExtendedProperties.Item("rowspan") = rowspan.ToString()
Else
Me.dt.Rows.Add(New Object() {Nothing, f.Language, f.FileSize})
End If
rowspan += 1
Next
Next
Also keep in mind that the this is dumped into a DataView which is sorted by the first column, so I would imagine it must actually be sorted first, then somehow count the rows for each "like" first column, then set the "rowspan" value for the first row of that column equal to the number of rows belonging to it.
Does this make sense? Here is an ideal example of what I would like to accomplish in the layout:
Try this.
Protected Sub DemoGrid_PreRender(sender As Object, e As System.EventArgs) Handles DemoGrid.PreRender
MergeRowsWithSameContent(sender)
End Sub
Public Sub MergeRowsWithSameContent(gvw As GridView)
For rowIndex As Integer = gvw.Rows.Count - 2 To 0 Step -1
Dim row As GridViewRow = gvw.Rows(rowIndex)
Dim previousRow As GridViewRow = gvw.Rows(rowIndex + 1)
For i As Integer = 0 To row.Cells.Count - 1
If row.Cells(i).Text = previousRow.Cells(i).Text Then
row.Cells(i).RowSpan = If(previousRow.Cells(i).RowSpan < 2, 2, previousRow.Cells(i).RowSpan + 1)
previousRow.Cells(i).Visible = False
End If
Next
Next
End Sub
P.S: Shameless port of this wonderful code I have been using for years.