Re: Buttons don't display when in 'while' loop
"form5.state = 1" troubles me. Form5 sounds like a default instance, as nobody would name a variable that way on their own. Setting something for the default instance will only update the default instance. It isn't clear that you are showing the default instance, so it may be the case, as suggested in #31, that you are setting the variable on the default instance, but displaying a different instance, which would result in exactly the behavior you are seeing: One instance updates, but that one isn't visible. The visible instance isn't updated.
Re: Buttons don't display when in 'while' loop
Ah, my bad. The line isn't form5.state=1, it's 'form5.l(1)' which should run the subroutine l and update everything within that form.
Re: Buttons don't display when in 'while' loop
I edited the post so it's correct now.
I'm not quite sure how to stray from the default instance. I'll look into that. I just need a simple function that receives a variable, then updates the picture box. I keep running into this problem where it won't update the picture box, or g.graphics. The variables are updating and the code is running all the way through, but no visual updates take place.
the code I posted is the complete code for form5. It's a blank form with a picture box, nothing else.
Re: Buttons don't display when in 'while' loop
have you tried just refreshing the thing that needs refreshing
i used to do that a lot!
picturebox1.refresh
here to suggest
Re: Buttons don't display when in 'while' loop
Yeah I tried that. It doesn't seem to do anything. My graphics are still hidden.
Re: Buttons don't display when in 'while' loop
The code of Form5 is one thing, but the key is how you display that form. The best way to do it would look something like this:
Code:
Dim myForm as New Form5
myForm.Show
or
myForm.ShowDialog
If that is how you are showing the form, then the problem is due to defaults. After all, Form5 is a type of form, but it is also a specific instance of the form. That's what the defaults are about. A specific instance is quietly created, and given the same name as the class type, which has caused mayhem ever since it was introduced back in VS2005. Your code is going to do something to the default instance, but if you aren't showing the default instance (as my example is not), then you are updating a form that isn't visible, while NOT updating a form that IS visible. In other words, the wrong form is being updated.
You are updating the default instance, but it is not clear whether you are showing the default instance. I hate the default instances. They do nothing more than cause confusion. They don't even behave exactly the same as other instances, though the differences are subtle. If you are using a line like form5.Show, then I would suggest changing things around to use non-default instances.
If you were using my example, the call you would have to make would be to:
myForm.l(1) (or whatever that sub name is).
Re: Buttons don't display when in 'while' loop
That makes so much sense. So when I use Form5.Show, it's showing Form5 as it was when that line of code was executed. It's showing the default instance. I get it now.
So how do I show the updated instance? How do I get form5 to show itself updated?
For instance, if I want the picture box to refresh every time my main form receives the letter P on the com port, then would I use
if(serialcharacter = "P") Then
Form5.l(1)
Form5.Show
this doesn't seem right because this would mean I have to close the form in every frame as well, otherwise it will open up infinite instances of Form5.
EDIT: By the way, the form appears when I click a button that looks like this:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form5.Show()
End Sub
Re: Buttons don't display when in 'while' loop
You don't quite get it, but with that last snippet, it is clear that you are ONLY showing the default instance.
When you create an object:
Dim myObj As New SomeObject
then myObj is a variable that holds the new SomeObject that was just created (roughly speaking). You Always have to create a new instance of a class to use it. This is just as true of forms as for other types of classes, so you have to have a New Form5 somewhere....but you don't. The reason is that VS is creating an instance of Form5, and calling that instance Form5. In other words, hidden somewhere that you don't see is a line like this:
Public Form5 As New Form5
That actual line isn't really there. Somebody on here once posted the actual code that does this, and it doesn't look exactly like that. For instance, the default instance isn't there if you don't use it, but is just created on demand. Still, as far as you are concerned, that line is there. Because of that, when you call:
Form5.Show, it works. If that line wasn't there, then Form5 would be an undeclared variable, and you'd get an exception when you tried that line. However, the variable Form5 is being quietly, and secretly, declared for you such that it is there when you go to show it. That may seem useful, but it mostly sucks. However, it doesn't seem relevant, in this case, because you are showing the default instance in that button click event. It appears that you never have any line like:
Code:
Dim <some name> As New Form5
anywhere, so you are ONLY working with the default instance. That should work. All that I was saying isn't relevant. The default instance isn't a default display, it is an instance of the class. The class is responsible for drawing itself. You could easily have multiple instances:
Dim A as New Form5
A.Show 'Shows one instance of Form5
Dim B as New Form5
B.Show 'Shows another instance of Form5
A.l(1) 'This should update just one of the two forms on the screen.
That's what I thought you were doing. I thought that you had something like this:
Code:
Dim A as New Form5
A.Show 'Show instance called A
Form5.l(1) 'Update default instance
You aren't doing that, because you are only showing the default instance, and only updating the default instance. I hate default instances, but what you have shown should not have any problem due to default instances.
I did notice that you had Me.Update. The term, I believe, is Me.Refresh, and you should only need to refresh the picturebox, but refreshing the whole thing should work, too (though not quite as quickly, since you are redrawing EVERYTHING).
Can't look at the code now, either, because it's on the last page. Just a quirk of the forums.
Re: Buttons don't display when in 'while' loop
Ok, now that I've taken a second look at the code, I'd suggest a little test:
Comment out all the code dealing with the picturebox. Those are weird and potentially time consuming. The first thing to look at is that label, which is pretty simple, so set aside the rest. Right now, your loop will run 255 times before state is set to 0. That's fine, but you might consider running it only once or twice for testing purposes. The key is to see whether or not the label updates if there isn't anything else going on in there.
Re: Buttons don't display when in 'while' loop
ok so I made a few changes. In my main code, for the button I changed it to:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm5 As New Form5
frm5.Show()
End Sub
The frm5 code is as follows:
Code:
Public Class Form5
Public Sub l(ByVal state As Integer)
While (state = 1)
lbl1.Text = "BAM" 'never updates
MsgBox("Finished") 'message box displays, but label and pix box don't update
state = 0
End While
End Sub
End Class
Still no update. I tried using Form5.l(1) and frm5.l(1) - after making the necessary changes.. No luck with either.
Re: Buttons don't display when in 'while' loop
How were you able to call that sub? With the change you made to the button, you are now showing an instance of Form5 called frm5, but it would not be accessible from anywhere other than that button sub, because frm5 is a local variable to that sub. To call the public sub, you'd have to have the line
frm5.l(1)
but the compiler will say that frm5 doesn't exist if that line is anywhere outside of the button sub. If you called Form5.l(1) instead, then you called the sub on the default instance, though you were showing the instance called frm5.
EDIT: I see that you called it frm5.l(1) after making whatever necessary changes. That would be the right test. If the label still isn't updating, then something very strange is happening.
Re: Buttons don't display when in 'while' loop
Yes, you caught that correctly.
after one more test, I made this change to my main form
Dim frm5 As New Form5
frm5.Show()
frm5.l(1)
This opens up the new form and the label shows "BAM" as it should, but when I click Ok on the message box, the whole window becomes invalid. I can't close it, other windows leave a trail when I move them around above it.
Does this seem correct? The problem is that I need form5 to update itself every time state = 1. Otherwise I would constantly have to be opening 'show' and closing/disposing of the form to get it to display the constantly changing picture box. This seems messy?
Re: Buttons don't display when in 'while' loop
I've had a little more success. I now have the picture box updating and the label, in realtime. I mean I can see the label count from 0 to 255 and the picture box fade from black to white.
This is an improvement, but this is only one frame. How would I go about doing this 30 times per second?
If I create a new form instance each time (frm5 = new Form5, frm5.show), it will pop up the new form in a different position on the screen every time - And, I can't move the new form window around. It's stuck and doesn't accept close, minimize, or restore mouse commands..
It seems this form is invalid as soon as it's created.
Re: Buttons don't display when in 'while' loop
It sounds to me like that form is spending so much time doing something that it can't respond well to mouse inputs, or any other kind of input. The bit about other windows leaving tracks suggests that the initial window doesn't have time to redraw whenever it is being invalidated.
As for redrawing that fast, there probably are limits to what you can do with GDI+, though from what you have shown, I don't think you are there. I'm no expert when it comes to drawing, but I do have one program that forms and draws a series of bitmaps onto the screen. The calculations for the bitmaps are kind of complicated, though perhaps not as complicated as your pixel by pixel drawing. After a bit of work, I got the drawing down to about 10ms, which would mean 100 fps, except that I am doing up to 100 of these drawings each time, or 1 fps. Ultimately, I had to change the algorithm so that I was only ever drawing one or two, rather than the full set, each time around. You, to, may be up against a limit on GDI+. You might use the lightweight profiler class I have over in the .NET CodeBank to take a look at your actual timing, which might show you something...or not.
Re: Buttons don't display when in 'while' loop
Thanks Shaggy for all of your help, after reading through your posts and making suttle changes here and there I found my mistake.
I was doing this:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frmVid = New frmVideo
frmVid.Show()
End Sub
-Which is perfectly ok, until I tried refreshing frmVid using frmVid.Show() again, when refreshing wasn't needed because it's not the default instance anymore.
frmVid.display(1) now works perfectly and the bitmap/picture box refreshes every time.
Thanks again. It's exciting to have complete control of this tiny camera now, and after this feat I'm all pumped up with that 'Now... I can program anything' mentality. Until the next hurdle :P
Cheers!
Re: Buttons don't display when in 'while' loop
Good show. Mark this thread resolved and start a new one with any new questions. I find that after threads get to a certain size, they don't get the eyeballs anymore, so you don't get new views on the matter.
Re: [RESOLVED] Buttons don't display when in 'while' loop
Agreed - and thanks again.