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
*** 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".
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.
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.
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.
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".
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.
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.
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
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".
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:
'
Public ReadOnly Property Width As Integer
Get
Dim num As Integer
Dim status As Integer = Gdip.GdipGetImageWidth(New HandleRef(Me, Me.nativeImage), num)
If (status <> 0) Then
Throw Gdip.StatusException(status)
End If
Return num
End Get
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:
'
Friend Shared Function StatusException(ByVal status As Integer) As Exception
Select Case status
Case 1
Return New ExternalException(SR.GetString("GdiplusGenericError"), -2147467259)
Case 2
Return New ArgumentException(SR.GetString("GdiplusInvalidParameter"))
Case 3
Return New OutOfMemoryException(SR.GetString("GdiplusOutOfMemory"))
Case 4
Return New InvalidOperationException(SR.GetString("GdiplusObjectBusy"))
Case 5
Return New OutOfMemoryException(SR.GetString("GdiplusInsufficientBuffer"))
Case 6
Return New NotImplementedException(SR.GetString("GdiplusNotImplemented"))
Case 7
Return New ExternalException(SR.GetString("GdiplusGenericError"), -2147467259)
Case 8
Return New InvalidOperationException(SR.GetString("GdiplusWrongState"))
Case 9
Return New ExternalException(SR.GetString("GdiplusAborted"), -2147467260)
Case 10
Return New FileNotFoundException(SR.GetString("GdiplusFileNotFound"))
Case 11
Return New OverflowException(SR.GetString("GdiplusOverflow"))
Case 12
Return New ExternalException(SR.GetString("GdiplusAccessDenied"), -2147024891)
Case 13
Return New ArgumentException(SR.GetString("GdiplusUnknownImageFormat"))
Case 14
Return New ArgumentException(SR.GetString("GdiplusFontFamilyNotFound", New Object() { "?" }))
Case 15
Return New ArgumentException(SR.GetString("GdiplusFontStyleNotFound", New Object() { "?", "?" }))
Case &H10
Return New ArgumentException(SR.GetString("GdiplusNotTrueTypeFont_NoName"))
Case &H11
Return New ExternalException(SR.GetString("GdiplusUnsupportedGdiplusVersion"), -2147467259)
Case &H12
Return New ExternalException(SR.GetString("GdiplusNotInitialized"), -2147467259)
Case &H13
Return New ArgumentException(SR.GetString("GdiplusPropertyNotFoundError"))
Case 20
Return New ArgumentException(SR.GetString("GdiplusPropertyNotSupportedError"))
End Select
Return New ExternalException(SR.GetString("GdiplusUnknown"), -2147418113)
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.
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
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