How can I get rid of this error? Visual Basic 2010 - vb.net

I have an array of pictureboxes, like this (they all have an image displayed on them, from the designer, from my.resources):
Dim imgSourcePic() As PictureBox = {imgAcronym, imgAcrostic, imgAdjective, imgAdverb, imgAlliteration, imgApostrophe, imgClause, imgComma, imgDialogue}
and another array of pictureboxes (which don't have an image displayed, yet):
Dim imgDefinitiontoMatch() As PictureBox = {imgDefinition1, imgDefinition2, imgDefinition3, imgDefinition4, imgDefinition5}
In the sub NewGame() I have a line of code which is:
imgDefinitiontoMatch(intX).Image = imgSourcePic(intRandomNumber).Image
But, whenever it executes this line of code I get this error:
I debugged it and saw that the pictureboxes, in the array, are displaying the Image property as 'Nothing'.
How else can I assign an image to the pictureboxes in imgSourcePic() that'll work?
I have tried creating variables for all of the images, such as:
Dim picAcronym As Image = My.Resources.Acronymn_definition
...
But still doesn't achieve anything.
I hope this isn't a duplicate, but I have been trying to figure it out all day and can't seem to make
it work :(
Many thanks and, as always, hope this makes sense.

You're populating those arrays before InitializeComponent() runs, so all of the variables are still Nothing.
You need to assign the array in Sub New(), after InitializeComponent() (which creates the actual PictureBoxes)

You can also use the Load() Event of the Form, which I see you already have in your code:
Public Class Form1
Private PBs() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PBs = {PictureBox1, PictureBox2, PictureBox3}
' ... more code ...
End Sub
End Class

Related

Where to declare a public object which refer to a VB object?

First time that I use Visual Studio and VB.net.
Could someone explain to me where to declare a public object which refers to a VB object ?
This code works fine :
Public Class Form1
Private ThePen As New System.Drawing.Pen(Color.Red)
Private Sub Line(A As Point, y As Point)
Dim NewGraphic As Graphics = PictureBox1.CreateGraphics()
NewGraphic.DrawLine(ThePen, A, B)
NewGraphic.Dispose()
End Sub
End Class
But I would like to declare only one time in public
Dim NewGraphic As Graphics = PictureBox1.CreateGraphics()
I tried to declare it at the beginning, but it seem that my object PictureBox1 is not yet loaded (so, can't access PictureBox1.CreateGraphics())
So I tried in
Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
But I can't declare public variable inside :(
You should pretty much NEVER call CreateGraphics. Draw on a control in its Paint event handler or, if appropriate, create a custom control and override the OnPaint method. Store the data that represents the drawing in one or more fields and, whenever you want to change the drawing, set those fields and call Invalidate on the control.
Private lineStart As Point
Private lineEnd As Point
Private Sub DrawLine(start As Point, [end] as Point)
lineStart = start
lineEnd = [end]
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As EventArgs) Handles PictureBox1.Paint
e.Graphics.DrawLine(Pens.Red, lineStart, lineEnd)
End Sub
Generally speaking, it is preferable to specify the area to invalidate rather than not specifying an argument and invalidating the entire control. It is actually painting the pixels to the screen that is the slow part so it is preferable to keep that to a minimum. I'll leave that part to you but you may like to check this out for more info. Note that, if you're moving a line, you'd need to invalidate the area that contained the old line and the area that will contain the new line. You can call Invalidate multiple times with different areas in such cases, or you can combine the areas into one Region and call it once.
So different view but interesting !
I'm unable to test it :(
There's a problem with e. object in
e.Graphics.DrawLine(Pens.Red, lineStart, lineEnd)
BC30456 Visual Basic 'Graphics' is not a member of 'EventArgs'.

How to call a sub within anohter sub that is on a different form. VB.NET

I'm self taught so pardon me if my terminoligy isn't correct.
I want to call a sub within another sub that is in a completely different form. but for some reason i can't get it to work. Maybe it's not possible. I'm not sure.
for a bit of background on the program. There's two pictureboxes. One has a binding source and the other does not. I want the one that doesnt have a binding source to display the same image as the other picture box once it has loaded.
so the first sub is in form2. code is as follows:
Public Sub GetImage()
Me.Pbox_Image.Image = Mainfrm.Pbox_Image.Image
Me.Pbox_Image.Refresh()
Me.Pbox_Image.Update()
End Sub
This procedure works fine when called within form2. But i want the code to run when mainfrm p_Imageload is completed.
The code I used in mainfrm is as follows:
Public Sub Patient_Image_LoadCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles Patient_Image.LoadCompleted
GetImage()
End Sub
Visual studio displays an error and the code will not run. The error says that GetImage is not declared. I thought setting it to public would solve the issue but it didn't.
Any help is greatly appreciated.
You call it with adding a containing class infront of method, like this:
On FormX:
Sub
FormY.MethodZ()
End Sub

Cant use my object before dispose

Just like the title said, i cannot use my object if i dipose it, i mean before i dispose it, i cannot use it. here is my code
Public Class Form1
Dim hasil(3) As Bitmap
Dim OneClickInsertGbr As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
hasil(0) = New Bitmap("C:\Users\Robokidz\Google Drive\Tugas Akhir\Data Training\Foto Uang\1K\scan0001.jpg") 'This is my image
hasil(1) = New Bitmap("C:\Users\Robokidz\Google Drive\Tugas Akhir\Data Training\Foto Uang\1K\scan0001.jpg") 'This is my image
hasil(2) = New Bitmap("C:\Users\Robokidz\Google Drive\Tugas Akhir\Data Training\Foto Uang\1K\scan0002.jpg") 'This is my image
PictureBox1.Image = hasil(0)
hasil(0).Dispose()
hasil(1).Dispose()
hasil(2).Dispose()
End Sub
End Class
after i run, it generate error
Parameter is not valid.
after that check and see what is the reason behind the error, i know the problem is dispose. Because after i delete all of that dispose, it work fine, but the other problem rise.
Out of memory
i know this error, because i use too many memory.
my question is.
How to use an object and dispose it without getting that error?
The idea is that you dispose an object when you are finished using it. If you want the user to see an Image displayed in a PictureBox then you obviously haven't finished with that Image, so you shouldn't be disposing it. If you are later going to replace that Image in the PictureBox with another one, THEN you should dispose the Image that you are no longer going to display because you are now finished with it.
Here's an example of the sort of thing you need to do:
Private Sub SetPictureBox1Image(filePath As String)
If PictureBox1.Image IsNot Nothing Then
'Dispose an existing Image first.
PictureBox1.Image.Dispose()
End If
'Display the new Image.
PictureBox1.Image = Image.FromFile(filePath)
End Sub

Why can I not set the selection of a drop down list in VB.net before the form has been shown?

I cannot seem to get Visual Studio 2010 to let me set the SelectedItem or SelectedIndex property of a drop down list before the form has been shown on the screen. I can set these without issue if I wait until the form can be seen, so I'm not sure what the problem is. Here is my code.
Private Sub Test_Program_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For each item in System.IO.Directory.GetFiles(Application.StartupPath + "\Pictures\")
dd_pics.items.add(item)
Next
dd_pics.SelectedIndex = 0
background = dd_pics.SelectedIndex
End Sub
background is an integer variable. I've tried to change SelectedIndex to SelectedItem and change the background variable type to string, but that hasn't helped. Later in the code, I use the background variable to set a picture based on the file it references from the above code. I do not get an error, but it seems as if Visual Studio is skipping the line dd_pics.SelectedIndex = 0 and all sequential lines in the Sub. I found this out because I'm actually getting an error when I try to set the picture referenced here to a PictureBox and the debugger is telling me there is Nothing in the variable I am trying to set the PictureBox to. I can use the exact same code in a button and it works fine. The only difference I can find is that the form is fully loaded and I have to click the button where it is automated here. Can anybody at least tell me when Visual Studio is having this problem or possibly how to fix it? Any help is greatly appreciated. Thanks in advance.
I usually do these in the constructor:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
For each item in System.IO.Directory.GetFiles(Application.StartupPath + "\Pictures\")
dd_pics.items.add(item)
Next
dd_pics.SelectedIndex = 0
background = dd_pics.SelectedIndex
End Sub
Could this work in your situation?

Display a modeless Form but only one

VB2010. I must be missing something because I couldn't find a solution after searching for an hour. What I want to do is simple. In my app I want to display a modeless form so that it is floating while the user can still interact with the main form.
dim f as New frmColors
f.Show(Me)
But I only want one instance of the form at any time. So how can I prevent more than once instance being displayed, and if there is one instance then just give it focus?
Does something like this work for you, if the form is already visible you can not do a Show, you can just do a BringToFront, also you can check to see if the Form has been disposed so you can New up another one.
Public Class Form1
Dim f As New frmColors
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If f.IsDisposed Then f = New frmColors 'To handle user closing form
CheckForm(f)
End Sub
Private Sub CheckForm(frm As Form)
If frm.Visible Then
frm.BringToFront()
Else
frm.Show(Me)
End If
End Sub
End Class
Make your form follow the singleton pattern. I can't vouch for this sample, but from the text it appears to do what you want.