Looping Through a Bitmap Array - arrays

Dim bmpNew As Bitmap = Nothing
Dim Bitmaps(15) As Bitmap
For b = 0 To Bitmaps.Count - 1
If Bitmaps.Contains(bmpNew) Then
Else
If Bitmaps(b) Is Nothing Then
If Bitmaps(b) Is bmpNew Then
Else
Bitmaps(b) = bmpNew
End If
Else
End If
End If
ListBox1.Items.Add(b)
Next
I am developing an application that generates random images every time the button is pressed. After generating the image, it loops through an array of bitmaps which are initially empty. What it should do is fill the array with every new image that it creates while making sure to not add it if it already exists in the array. I have tried many attempts and this method is the only one that actually makes sense and is very close to what I am trying to achieve, but it still produces duplicates in my array.
What am I missing?

Related

Is there a way to make an empty array and growing it as it gets data in vb.net?

I have been working on a project, and am attempting to make a new array for data. I have tried making an empty array with Dim Name() As String = {}. I am using a ListView, and the way I have done it there are blank spots where I have gotten rid of data. This is my current code:
Sub English(ByVal Country() As String, ByVal Language() As String)
rbDisplayallData.Checked = False
lstResults.Visible = True
lstResults.Items.Clear()
lstResults.Columns.Clear()
With lstResults
.View = View.Details
.Columns.Add("English Speaking Countries", 200, HorizontalAlignment.Left)
End With
For i = 0 To 181
Dim EnglishSpeakingCountries(i) As String
If Language(i) = "English" Then
EnglishSpeakingCountries(i) = Country(i)
End If
lstResults.Items.Add(New ListViewItem({EnglishSpeakingCountries(i)}))
Next
End Sub
I am trying to get rid of these spaces.
I Was thinking if I were to compact the array or make a new one with the same data going into a new array it would fix the issue.
If you have a solution please let me know.
There are two things that could be considered an empty array
An array with no elements, i.e. a Length of zero.
An array where every element is Nothing.
All arrays are fixed-length. Once you create an array with a particular number of elements, it always has that number of elements. You can use ReDim Preserve or Array.Resize but, in both those cases, what actually happens is that a new array is created and the elements copied from the old array. The new array is assigned to the same variable but anywhere the old array is referenced, it will still have that same number of elements. Try running this code to see that in action:
Dim a1 As String() = {}
Dim a2 As String() = {"First", "Second", "Third"}
Dim b1 = a1
Dim b2 = a2
Console.WriteLine(a1.Length)
Console.WriteLine(a2.Length)
Console.WriteLine(b1.Length)
Console.WriteLine(b2.Length)
Console.WriteLine()
ReDim Preserve a1(2)
Array.Resize(a2, 6)
Console.WriteLine(a1.Length)
Console.WriteLine(a2.Length)
Console.WriteLine(b1.Length)
Console.WriteLine(b2.Length)
Console.ReadLine()
Output:
0
3
0
3
3
6
0
3
As you'll be able to see, a1 and a2 end up referring to new arrays with the specified lengths but the original arrays with the original lengths still exist and are still accessible via b1 and b2.
If you start with an array with no elements then you can use ReDim Preserve or Array.Resize to give the appearance of resizing the array but that's not really what's happening and that should generally be avoided. If you know how many elements you'll end up with then you could create an array of that size and then set each element in turn. You'd need to keep track of the next element index though, so that's still a bit tedious.
Generally speaking, if you want an array-like data structure but you want it to be able to grow and shrink as required, you should use a collection. The most common collection is the List(Of T), where T is any type you care to specify in your code. If you want to store String objects then use a List(Of String). You can call Add to append a new item to the end of the list, as well as Insert, Remove and RemoveAt methods. You can also get or set an item by index, just as you can do for array elements.
Note that a List(Of T) actually uses an array internally and uses the aforementioned method of "resizing" that array. It optimises the process somewhat though, which makes the code easier for you to write and large collections more efficient to use.
It's worth noting that, in your own code, the Columns and Items properties of your ListView are both collections, although they are slightly different to the List(Of T) class.
Looking at your original code, this:
For i = 0 To 181
Dim EnglishSpeakingCountries(i) As String
If Language(i) = "English" Then
EnglishSpeakingCountries(i) = Country(i)
End If
lstResults.Items.Add(New ListViewItem({EnglishSpeakingCountries(i)}))
Next
could be changed to this:
Dim englishSpeakingCountries As New List(Of String)
For i = 0 To 181
If Language(i) = "English" Then
englishSpeakingCountries.Add(Country(i))
lstResults.Items.Add(Countries(i))
End If
Next
Note that you're just adding items to two collections. I guess the question is whether you actually need this extra collection at all. If you do want to use it later then you need to assign it to a member variable rather than a local variable. If you don't need it later then don't create it at all. As I said, you're already adding items to a collection in the ListView. Maybe that's all you need, but you haven't provided enough info for us to know.

I want to create a 2D array of points that links to my 2D array of components

I am making a circuit diagram drawer for a school project and have a 2D array that stores whether 2 components are connected or not. I want to find a way to have an array that then stores the co-ordinates of these points so that if they are deleted, it can find the points and get rid of the connection
ElseIf P(1).X = Nothing And P(1).Y = Nothing Then
P(1).X = pointx
P(1).Y = pointy
Form1.Drawlines() 'run the DrawLines sub in Form1.vb
NoOfNodes = NoOfNodes + 1 'increment the NoOfNodes Variable by 1
Node2 = COMPONENTID
Connections(Node1, Node2) = true
This is the code that stores the points being drawn, at the end the value of Connections is set to true to show that there is a connection between the components.
I need a way to have another array that can say something along the lines of:
Connections(Node1,Node2) = (P(1),P(0))
any contribution is much appreciated,
Charlie
Sounds like each element in your array should point to an instance of a class. On the class you can pack as many attributes and as much information about the connection as you like.

How to pass the value of picturebox image to another picturebox using array vb.net

I'm beginner at vb.net please help me
How can i pass my picturebox1.image to picturebox2.image and the value of picturebox2.image to picturebox3.image. My point is, Everytime the new image arrive it's like the images moving forward.the incoming image will go to picturebox1. And the old value of picturebox1 will move to picturebox2 and the value of picturebox2 will go to picturebox3. I don't know how to construct the logic in coding. Help me please
Ok, there are a number of ways the accomplish this. I use my database to store images all the time, so I'll show you a method for displaying the images in the order they are retrieved from the database. You are correct in that if you assign pic2.image to pic1.image and so on, the picture boxes will all have the same image because that's the value of pic1, reversing the order does the same thing. You need to assign the image you want in each picture box to that picture box.
First I create a list of images, they're all jpg's but it shouldn't matter:
Imports System.Drawing ' <---- I assume you have this at the top of the code behind
Dim MyImages as List(of Image)
I retrieve the images from the database in a datatable in order of the primary key ascending, key is 1 to n and fill the list:
Dim x as Integer = 1
pictureboxname = "pic" & x
For Each MyDataRow as DataRow in MyTable.Rows
' Load the list, the image is in column zero
MyImages.Add(MyDataRow(0))
' because this is the initial list, you can also populate the pictureboxes too
' you need to use the picturebox name in an expression to populate the pictureboxes in a loop.
' Something like this .... this is untested
(pictureboxname).Image = MyImages(MyImages.Count -1)
' and change picturebox name to the next picture box
x += 1
' I assume picturebox name is pic1, pic2, etc ....
pictureboxname = "pic" + x
' you'll have to limit this loop to the number of pictureboxes you have if they are less than the number of images, but you get the idea
Next
When a new images is added, you can then add it to the list and database at the same time and then repopulate the picture boxes.
StoreImage(NewImage)
MyImages.Add(NewImage)
Dim x as integer = 1
pictureboxname = "pic" & x
For each MyImage as Image in MyImages
' assign each image in turn to the picture boxes ... again, untested
(pictureboxname).Image = MyImage
x += 1
pictureboxname = "pic" & x
' again, you'll have to limit this loop to the number of pictureboxes you have if it is less than the number of images ....
Next
This is not be exactly what you're looking for, but should get you started.

VB.Net Inserting information from an Access Database into an array

I'm doing my first project attempting to bind data from an .MDB file to a Windows Form project in Visual Basic. Most of the functions work off an arrays "Hubs", so I wanted to fill this array with information drawn from my database. After using the wizard to create DataSet, I created the following code.
Public Sub LoadData()
For i As Integer = 0 To 15
With Hubs(i)
.ID = HubsDataSet.Tables("Hubs").Rows(i).Item(1).ToString 'This line is flagged as bad by the unit tests.
.City = HubsDataSet.Tables("Hubs").Rows(i).Item(2).ToString
.Lat = HubsDataSet.Tables("Hubs").Rows(i).Item(3).ToString
.Lon = HubsDataSet.Tables("Hubs").Rows(i).Item(4).ToString
End With
Next
'Load ID1ComboBox List
For i As Integer = 0 To 15
ID1ComboBox.Items.Add(Hubs(i).ID)
Next
'Load ID2ComboBox List
For i As Integer = 0 To 15
ID2ComboBox.Items.Add(Hubs(i).ID)
Next
'Set default values for Combo boxes
ID1ComboBox.SelectedIndex = 0
ID2ComboBox.SelectedIndex = 1
End Sub
While the project appears to run and function properly, I now find that all my unit tests fail, throwing an error that no Row 0 exists. In addition, if I attempt to use a for each statement instead of a for loop, the program no longer functions (it skips the lines entirely, since it does not hit a break point I inserted). I'm new to this and very confused what I'm doing wrong.

Silverlight Multipage Printing

I am implementing silverlight printing using PrintDocument. My UI is generated runtime using XamlReader which parse xaml which I have stored in db.
Here is code:
string str = sb.ToString();
newUI = XamlReader.Load(sb.ToString()) as FrameworkElement;
newUI.DataContext = ReportData;
grdPreviewArea.Children.Add(newUI);
grdPreviewArea.Height = pageHeight;
grdPreviewArea.Width = pageWidth;
Grid.SetColumn(newUI, 1);
Grid.SetRow(newUI, 1);
Now to print I am setting newUI as e.PageVisual in my print event handle.
This works fine if rendered UI is fits single page , but I am not able to print second page if it does not fits single page.
First you need to work out how many pages are needed
Dim pagesNeeded As Integer = Math.Ceiling(gridHeight / pageHeight) //gets number of pages needed
Then once the first page has been sent to the printer, you need to move that data out of view and bring the new data into view ready to print. I do this by converting the whole dataset into an image/UI element, i can then adjust Y value accordingly to bring the next set of required data on screen.
transformGroup.Children.Add(New TranslateTransform() With {.Y = -(pageIndex * pageHeight)})
Then once the number of needed pages is reached, tell the printer to stop
If pagesLeft <= 0 Then
e.HasMorePages = False
Exit Sub
Else
e.HasMorePages = True
End If
Or if this is too much work, you can simply just scale all the notes to fit onto screen. Again probably by converting to UI element.
Check out this link for converting to a UI element.
http://www.codeproject.com/Tips/248553/Silverlight-converting-to-image-and-printing-an-UI
Hope this helps

Resources