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

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.

Related

Visio VBA trying to get a list of containers and member shapes

Background: I have some code that runs through a Visio page and returns all the shapes. Many of these shapes are in containers, so I would like to know what container a shape belong to.
Original approach: I was hoping to retrieve the "parent" container of each shape (I only need one level of container, there are no containers within containers) using the Shape.ContainingShape property but that was only returning '0' for every shape.
If anyone has a solution for how I was originally trying to get the container, that would be the most elegant. But since I can't get that to work, I am trying the following alternative, which is also presenting a roadblock.
Current approach: I was able to get a list of all the containers on the page, and now I would like to pull the member shapes for each container. It's not as clean, but it would allow me to cross-reference the shapes and get the containers they belong to.
Issue: I am getting "Error 13 Type Mismatch" when trying to create an array with column 0 being the container name, and column 1 being the member shapes.
' Create array of containers and member shapes
Dim arr() As Long
Dim vsoMemberShape As Shape
Dim vsoContainerShape As Shape
Dim containerArr() As Long
Dim rows As Integer
Dim i As Integer
For Each ContainerID In vsoPage.GetContainers(visContainerIncludeNested)
Set vsoContainerShape = vsoPage.Shapes.ItemFromID(ContainerID)
arr = vsoContainerShape.ContainerProperties.GetMemberShapes(1)
rows = UBound(arr)
ReDim containerArr(0 To rows, 0 To 1)
For i = 0 To UBound(arr)
Set memberShape = vsoPage.Shapes.ItemFromID(arr(i))
containerArr(i, 0) = vsoContainerShape.NameU
containerArr(i, 1) = vsoMemberShape.NameU
Next
Next
' The following code is in a For loop, not shown
' shapeToName is what I want to compare to the member shapes in the container
' array defined above, and then retrieve the corresponding container
' This is where the error is popping up
shapeToName = CStr(vsoShapeTo.Name)
Dim x As Integer
x = Application.Match(shapeToName, Application.Index(containerArr, 0, 1), 0)
shapeContainer = containerArr(x, 1)
I think what you're looking for is Shape.MemberOfContainers, which returns an array of containers that a shape is a member of.
You can also have a look at this post which covers the same issue:
Visio: How to get the shapes that are contained in one shape?
I'll also throw in a link to a post by David Parker that covers containers in the context of cross-functional flowchart, which makes good use of Containers and Lists:
https://bvisual.net/2009/09/07/visio-2010-containment-and-cross-functional-flowcharts/

Looping Through a Bitmap Array

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?

Visio VBA - How can I distribute shapes with a known, fixed distance

I'd like to place all currently selected shapes into an array. I'd then like to sort that array so I can find either the top most or left most shape in the array. I'd then like to use that shape as my starting point, and then from there align the other shapes a fixed, known distance apart. I've tried to place the shapes into an array like so:
Dim numShapes As Integer, i As Integer
Dim arrShapes As Visio.Selection
numShapes = Visio.ActiveWindow.Selection.Count
For i = 1 To numShapes
arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
I have tried to create the array with no type specification, specifying as variant, and as in this example as selection. I don't know if I can put them into a list of some kind either? Obviously I can't get to the point of sorting the array and then distributing my shapes until I can get the array to populate. I'm placing a break point in the code and I have the "Locals" window open and I can see that the array is not being populated.
Update:
Why does this work,
Dim Sel As Visio.Selection
Dim Shp As Visio.Shape
Set Sel = Visio.ActiveWindow.Selection
For Each Shp in Sel
Debug.Print Shp.Name
Next
And this does not?
Dim i As Integer
Dim Shp As Visio.Shape
For i = 1 To Visio.ActiveWindow.Selection.Count
Set Shp = Visio.ActiveWindow.Selection(i)
Debug.Print Shp.Name
Next i
Regards,
Scott
There was a couple of problems in your code - fixing only one would not have got you any further in understanding if you had actually fixed anything.
Your arrShapes is declared as a general object - the Selection
Object is one of those objects that is the Jack of all trades, and
master of none.
You didn't "Set" when assigning to the array.
I don't have Visio on this machine, so cannot directly test the code below. I am also assuming that all items selected are shapes (usually a safe assumption in Visio).
Dim numShapes As Integer, i As Integer
Dim arrShapes() As Shape ' Set this up as an array of shape
If Visio.ActiveWindow.Selection.Count > 0 then ' don't want to cause a problem by setting the array to 0!
ReDim arrShapes(Visio.ActiveWindow.Selection.Count)
numShapes = Visio.ActiveWindow.Selection.Count ' while not really necessary it does help explain the code.
For i = 1 To numShapes
' must Set as we want the reference to the shape, not the default value of the shape.
Set arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
Else
MsgBox "No shapes selected. Nothing done." ' soft fail
End If

Array of Picture boxes - now showing pictures (Visual Basic)

So, I have an array of picture boxes
Dim LocationArray(8,6) As PictureBox
I have set the array to pictures boxes as so
ArrayLocation(0,0)=pic00
ArrayLocation(0,1)=pic01
ect.
So, when the form loads, I want the picture box (3,0) to load with a picture in it
So far I have
LocationArray(3,0).Image = My.Recources.pic
And nothing happens when I load the form.
When I try to display a picture after changing which the coordinates of the array, I get a NullreferenceExepecation error.
How will I be able to fix this, the code I have so far is,
Static xAxis As Integer = 3
Static yAxis As Integer = 0
LocationArray(xAxis, yAxis).Visible = False
xAxis = +1
LocationArray(xAxis, yAxis).Image = My.Resources.man
LocationArray(yAxis, xAxis).Visible = True

Loop through Textboxes in access database

I have an Access database that I want to loop through certain textboxes in order to do a calculation and display the answer in a seperate textbox. When I attempt to loop it loops through every control on my form (with the Form.Controls) method. I would like to only loop through 4 specific textboxes (txtbx1, txtbx2, txtbx3, txtbx4) when my button is clicked.
Explanation...
TextBox_A contains a Number
Upon button click take the number from TextBox_A, Multiply by 2800, then Divide by 12
Display the answer to the calculation in txtbx1.
I would do this for each of the 4 textboxes named above. Then have a "Total" textbox that adds up the total from each textbox (txtbx1, txtbx2, txtbx3, txtbx4). Please help, new to this and at a complete loss.
Every control has got a Tag property. Set the Tag property for textboxes whose values you want to include in your algorithm (say to "INClUDE"). It's free format text, so you can put what you like.
Then write code attached to some form event similar to this:
Dim c As Control
Dim txt As TextBox
For Each c In Me.Controls
'check it's a text box ...
If TypeOf c Is TextBox Then
'see if including ...
Set txt = c
If Len(txt.Tag) > 0 Then
'do something here (I've coloured, to show works)
txt.BackColor = 10
End If
End If
Next c
I've set an extra variable txt to refer to the textbox in question, so that I can get at the TAG property using autocompletion (I believe this is called a narrowing conversion!).
It would be easier if you rename TextBox_A (_B / _C etc) to TextBox_1 / _2 / _3 etc.
And then try this:
Dim i As Integer
For i = 1 To 4
Controls("txtbx" & i) = Val(Controls("TextBox_" & i)) * 2800 / 12
Next
Otherwise, this would work too, but it is not well readable:
= Val(Controls("TextBox_" & Chr(Asc("A") + i - 1))) * 2800 / 12

Resources