DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005

    VB .NET Multiple Forms PictureBox change problem

    Hello everyone,
    This is my first post here, so try to go easy on me. I tried searching these forums and the internet for days to no avail. Let me just first say that I'm a newbie to VB .NET.

    Ok, here's what I'm trying to do. I have a VB .NET program with two forms. I want to have three buttons on the first form. Button1 loads and displays the second form. Button 2 makes a PictureBox on Form2 'visible'. Button3 on the first form makes the same PictureBox on Form2 'not visible'.

    I am close to figuring out how to do this. I make a class called myFormLibrary that declares the two forms. I can modify and change elements in each form - things like properties for each specific form (ie, Show, hide, etc...)

    I made a Public Sub in Form2 that has the code in it to make the PictureBox viewable. I can successfully call the sub from within Form1, but nothing happens when I tell it to make the PictureBox viewable.

    Code within button click on Form1:
    Dim Form2 As New Form2
    Code within lockDoors Sub in Form2:
    PictureBoxLocked1.Visible = True
    MsgBox("Test to see if we made it in here")
    The MessageBox pops up, but the image doesn't appear. Oh, I forgot to mention that when I loaded Form2 I had it make the PictureBox.Visible = False

    This seems like something very simple, but I'm lost. I tried 10 ways to change the state of the PictureBox. What am I doing wrong? If there's any further info anyone needs to know to helpme further, please let me know.

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    If you declare the Form2 instance inside Form1's Button1_Click event, it's not visible from the Button2- and Button3_Click events. You should declare the Form2 reference at the class level of Form1 (that is, outside any Sub or Function). Your code should look like this:
    Option Explicit On 
    Public Class Form1
        Inherits System.Windows.Forms.Form
        ' Class-level reference to Form2
        Dim theForm2 As Form2
        Private Sub Button1_Click(ByVal sender As System.Object, _ 
            ByVal e As System.EventArgs) Handles Button1.Click
            ' Create and show new instance of Form2
            theForm2 = New Form2
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, _ 
            ByVal e As System.EventArgs) Handles Button2.Click
            ' Show PictureBox on theForm2
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, _ 
            ByVal e As System.EventArgs) Handles Button3.Click
            ' Hide PictureBox on theForm2
        End Sub
    End Class
    Option Explicit On 
    Public Class Form2
        Inherits System.Windows.Forms.Form
        Public Sub LockDoors(ByVal Show As Boolean)
            PictureBoxLocked1.Visible = Show
        End Sub
    End Class
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. MMFAN Retires
    By MMFAN in forum .NET
    Replies: 20
    Last Post: 09-22-2002, 11:54 PM
  2. Activating 1 of multiple VB app Forms
    By Sam in forum VB Classic
    Replies: 0
    Last Post: 08-29-2001, 02:23 PM
  3. Activating 1 of multiple VB app Forms
    By Sam in forum VB Classic
    Replies: 0
    Last Post: 08-29-2001, 02:23 PM
  4. Replies: 84
    Last Post: 01-29-2001, 01:12 PM
  5. Multiple Forms in VB app calling a COM object
    By Patricia in forum VB Classic
    Replies: 1
    Last Post: 12-06-2000, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center