Results 1 to 15 of 15

Thread: Simple code that makes a picturebox visible and not visible

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Simple code that makes a picturebox visible and not visible

    I was using this code to make a picturebox not visible and then visible - picturebox is called MainWindow

    Code:
        Private Sub TSToggle_Click(sender As System.Object, e As System.EventArgs) Handles TSToggle.Click
            Debug.WriteLine(MainWindow.Name)
            Debug.WriteLine(MainWindow.Visible)
            MainWindow.Visible = Not MainWindow.Visible
        End Sub
    After I fill the picture box - via a drag and drop operation - I can no longer toggle it - I get a meaningless error

    "Parameter is not valid." - which I show in the screen shot below. I also show the form document outline. PB is inside a content panel. What am I making "invalid" about the PB that it does not want to show up??

    Even changed it to this - still got same error message!!

    Code:
        Private Sub TSToggle_Click(sender As System.Object, e As System.EventArgs) Handles TSToggle.Click
            If PBInFront Then
                PBInFront = False
                MainWindow.SendToBack()
            Else
                PBInFront = True
                MainWindow.BringToFront()
            End If
        End Sub
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Simple code that makes a picturebox visible and not visible

    This what happens when you make code up as you go along! .Visible has two values, True or False.

    You also can't send a control to the back if there's nothing for it to be behind.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Simple code that makes a picturebox visible and not visible

    Not sure what's going on in your code. Just ran this on a blank project and it worked just fine.

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TestPictureBox.Image = Image.FromFile("C:\Temp\round-error.png")
        End Sub
    
        Private Sub ToggleVisibilityButton_Click(sender As Object, e As EventArgs) Handles ToggleVisibilityButton.Click
            TestPictureBox.Visible = Not TestPictureBox.Visible
        End Sub
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    Quote Originally Posted by dunfiddlin View Post
    This what happens when you make code up as you go along! .Visible has two values, True or False.

    You also can't send a control to the back if there's nothing for it to be behind.
    I have something for it to be behind. You can see that from the document outline. UIList is what is toggling in and out...

    This code has been working for a year - it's in a proof-of-concept app we are working on.

    Argh...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Simple code that makes a picturebox visible and not visible

    Oops just read that it stops working once you do a drag and drop on it. This works for me after doing a drag and drop.

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TestPictureBox.AllowDrop = True
        End Sub
    
        Private Sub ToggleVisibilityButton_Click(sender As Object, e As EventArgs) Handles ToggleVisibilityButton.Click
            TestPictureBox.Visible = Not TestPictureBox.Visible
        End Sub
    
        Private Sub TestPictureBox_DragDrop(sender As Object, e As DragEventArgs) Handles TestPictureBox.DragDrop
            Dim pb = CType(sender, PictureBox)
            pb.Image = Image.FromFile(CType(e.Data.GetData(DataFormats.FileDrop), String()).First)
        End Sub
    
        Private Sub TestPictureBox_DragEnter(sender As Object, e As DragEventArgs) Handles TestPictureBox.DragEnter
            e.Effect = If(e.Data.GetDataPresent(DataFormats.FileDrop), DragDropEffects.Copy, DragDropEffects.None)
        End Sub
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    I am chasing my tail here - it's a multi-threaded app so I could be seeing error's from elsewhere too.

    Wow - I wish I could pinpoint exactly when this started happening.

    Any interesting debugging idea's or trick's someone might have???

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple code that makes a picturebox visible and not visible

    One question: Is the error always when the PB transitions in a specific direction? For instance, does it only occur when going from visible to not visible or the other way around?

    If it is always in one direction and not the other, then that would suggest that you might be asking, "What is it about a PB with Visible = False that is not valid?" After all, I would expect that the problem had to do with the not visible state.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    I thought it was both directions - but now it seems like it might be when going from VISIBLE to going to NOT VISIBLE.

    But that could be because I'm suppressing some TIMER code from drawing in the PB if it's not visible. That's new code - since I'm thinking my threads are misbehaving (although I'm synclocking and delgating everything the way I should)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Simple code that makes a picturebox visible and not visible

    It doesn't sound like a synchronization issue, exactly. I also think that the location of the exception may be a redd herring. The actual exception may be in something triggered by changing the visibility state, perhaps. I have no suggestion as to what that is, though, so it seems like it might be something specific to the code. Still, if you think that the behavior changed when you disabled a timer, that would be something worth looking into. The point is that if you can cause the behavior to change by taking some action, even if you don't know why that action would change the behavior, that would be a valuable clue as to where the problem lies.

    Does toggling the visibility cause any intentional side effect in the code? For instance, does something occur as a result of the visibility changing? You already suggested that the behavior of some timer tick event changes behavior due to the visibility changing, are there other things?

    Another thing that would be worth looking into, potentially, is how the visibility affects the control. The control may not be accessible in that state from some other code, though again, I can't say what. Some research into what ramifications Control.Visible = False has on the accessibility of the control might suggest something.
    My usual boring signature: Nothing

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Simple code that makes a picturebox visible and not visible

    Close Visual Studio and delete the bin and obj folders that reside in the application project's folder. Re-open Visual Studio and compile/run the program again. Usually when I get weird errors I can't explain, I do that. Try it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    @niya - I will try - maybe not tonight - too late...

    But I did find some info on a exception trace that might indicate it's on some paint event of the picturebox - and even that it's thread related... Thing is I don't have any paint events in the code - it's way simpler than that!

    Code:
       at System.Drawing.Image.get_Width()
       at System.Drawing.Image.get_Size()
       at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
       at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Form.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmSysCommand(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Form.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmNcButtonDown(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at UI.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Simple code that makes a picturebox visible and not visible

    I donno if this could help but that error is occurring because GDI+ is failing to get the width from the native image:-
    vbnet Code:
    1. '
    2. Public ReadOnly Property Width As Integer
    3.     Get
    4.         Dim num As Integer
    5.         Dim status As Integer = Gdip.GdipGetImageWidth(New HandleRef(Me, Me.nativeImage), num)
    6.         If (status <> 0) Then
    7.             Throw Gdip.StatusException(status)
    8.         End If
    9.         Return num
    10.     End Get
    11. End Property

    Changing the visibility triggers a redraw which ultimately leads to a call to the above. Its trying to get the width of the PictureBox's image and the above property is called. status is most likely not returning zero which causes an exception to be thrown:-

    vbnet Code:
    1. '
    2. Friend Shared Function StatusException(ByVal status As Integer) As Exception
    3.     Select Case status
    4.         Case 1
    5.             Return New ExternalException(SR.GetString("GdiplusGenericError"), -2147467259)
    6.         Case 2
    7.             Return New ArgumentException(SR.GetString("GdiplusInvalidParameter"))
    8.         Case 3
    9.             Return New OutOfMemoryException(SR.GetString("GdiplusOutOfMemory"))
    10.         Case 4
    11.             Return New InvalidOperationException(SR.GetString("GdiplusObjectBusy"))
    12.         Case 5
    13.             Return New OutOfMemoryException(SR.GetString("GdiplusInsufficientBuffer"))
    14.         Case 6
    15.             Return New NotImplementedException(SR.GetString("GdiplusNotImplemented"))
    16.         Case 7
    17.             Return New ExternalException(SR.GetString("GdiplusGenericError"), -2147467259)
    18.         Case 8
    19.             Return New InvalidOperationException(SR.GetString("GdiplusWrongState"))
    20.         Case 9
    21.             Return New ExternalException(SR.GetString("GdiplusAborted"), -2147467260)
    22.         Case 10
    23.             Return New FileNotFoundException(SR.GetString("GdiplusFileNotFound"))
    24.         Case 11
    25.             Return New OverflowException(SR.GetString("GdiplusOverflow"))
    26.         Case 12
    27.             Return New ExternalException(SR.GetString("GdiplusAccessDenied"), -2147024891)
    28.         Case 13
    29.             Return New ArgumentException(SR.GetString("GdiplusUnknownImageFormat"))
    30.         Case 14
    31.             Return New ArgumentException(SR.GetString("GdiplusFontFamilyNotFound", New Object() { "?" }))
    32.         Case 15
    33.             Return New ArgumentException(SR.GetString("GdiplusFontStyleNotFound", New Object() { "?", "?" }))
    34.         Case &H10
    35.             Return New ArgumentException(SR.GetString("GdiplusNotTrueTypeFont_NoName"))
    36.         Case &H11
    37.             Return New ExternalException(SR.GetString("GdiplusUnsupportedGdiplusVersion"), -2147467259)
    38.         Case &H12
    39.             Return New ExternalException(SR.GetString("GdiplusNotInitialized"), -2147467259)
    40.         Case &H13
    41.             Return New ArgumentException(SR.GetString("GdiplusPropertyNotFoundError"))
    42.         Case 20
    43.             Return New ArgumentException(SR.GetString("GdiplusPropertyNotSupportedError"))
    44.     End Select
    45.     Return New ExternalException(SR.GetString("GdiplusUnknown"), -2147418113)
    46. End Function

    When status is 2 it would throw an invalid parameter type exception which is most likely the exception you're getting. Hopefully, this will give you some kind of clue where to look. The error may have something to do with the image itself.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    @niya - I didn't see this post until now - thanks for this info!

    How did you get the source of WIDTH and STATUSEXCEPTION??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Simple code that makes a picturebox visible and not visible

    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Simple code that makes a picturebox visible and not visible

    Quote Originally Posted by Niya View Post
    Thanks - just purchased .NET Reflector VSPro!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width