Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so i am making a program that requires me to check if a form is open and if that is true then create a picturebox. right now i have this code
VB
Private Sub MyBase_load(Sender As Object, E As EventArgs) Handles MyBase.Load

    For Each form In My.Application.OpenForms
        If (form.name = Me.Name) Then
            'form is loaded so can do work
            Dim Logo As New PictureBox
            Logo.BackgroundImage = My.Resources.ChangeWallpaperlogojpg
            Logo.Visible = True
            Logo.Width = 32
            Logo.Height = 32
            Logo.Location = New Point(72, 136)
            Controls.Add(Logo)

        End If
    Next
End Sub

But this doesnt work

Anyone know the answer?
Posted
Updated 27-Jul-15 7:24am
v3
Comments
Aravindba 28-Jul-15 3:18am    
Hi First check if break point reach the Dim Logo As New PictureBox line ?,increase width and height of picturebox.
or try like this

Private Sub MyBase_load(Sender As Object, E As EventArgs) Handles MyBase.Load


'form is loaded so can do work
Dim Logo As New PictureBox
Logo.BackgroundImage = My.Resources.ChangeWallpaperlogojpg 'This file name is correct ? without extension
Logo.Visible = True
Logo.Width = 32
Logo.Height = 32
Logo.Location = New Point(72, 136)
Controls.Add(Logo)


End Sub

without checking if condition,try to run

I tested it by myself and this code is working :
VB
Private Sub MyBase_load(Sender As Object, E As EventArgs) Handles MyBase.load

    For Each form In Application.OpenForms
        If (form.name = Me.Name) Then
            'form is loaded so can do work
            Dim Logo As New PictureBox
             Logo.BackgroundImage = My.Resources.Resources.Desert
            Logo.Visible = True
            Logo.Width = 32
            Logo.Height = 32
            Logo.Location = New Point(72, 136)
            Controls.Add(Logo)
            'Logo.Parent = Me
        Else
        End If
    Next
End Sub


The difference I see is the name of the BackgroundImage. In my sample I don't use the Filename-Extension - perhaps you try it like this with your code (substrct the letters "jpg").

But this Logo is displayed ever. I think, you wish to see the Logo only if the Form is active. Therefore the code should have "some" modifications :

At first create the PictureBox called "Logo" as control on your Form.
Then insert this code to your Form (instead of the code seen above) :
VB
Protected Overrides Sub OnActivated(e As System.EventArgs)
    Logo.Visible = True
    MyBase.OnActivated(e)
  End Sub

Protected Overrides Sub OnDeactivate(e As System.EventArgs)
    Logo.Visible = False
    MyBase.OnDeactivate(e)
End Sub
 
Share this answer
 
v3
Comments
Aravindba 28-Jul-15 3:15am    
5+,may be increase width and height of picturebox,then can see image in picturebox
Ralf Meier 28-Jul-15 4:10am    
Thanks for your vote ... :)
Member 11852402 28-Jul-15 10:26am    
I also found that it was working but it was the location. there is another picturebox on top and even bringtofront wont work. any suggestions?
Member 11852402 28-Jul-15 11:15am    
Found it
One obvious problem is that you create a picture box put you don't put it anywhere so it won't be visible. Try adding the picture box to the controls collection.

See Form.ControlCollection Class[^]

Another thing is that you're investigating the forms when the new form is already loading. BAsed on your requirements would it make more sense to investigate the existence before loading a new form?
 
Share this answer
 
v2
Comments
Member 11852402 27-Jul-15 13:20pm    
it still doesnt work
Member 11852402 27-Jul-15 13:24pm    
i added this:Controls.Add(Logo) and yet it doesnt show
Wendelius 27-Jul-15 13:32pm    
Using debugger, have you checked that the code is executed correctly and that it is added on the correct forms collection?
Member 11852402 27-Jul-15 13:41pm    
sorry, im quite new to vs. how would i do that?
Wendelius 27-Jul-15 13:53pm    
In that case I suggest going through some tutorial in debugging, like http://www.visual-basic-tutorials.com/beginner/Debugging.html[^]

Knowing how to debug efficiently is going yo save you a lot of time in the future :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900