I cannot find how to protect a drawing in a PictureBox control from being corrupted (wiped out) when I call a dialog to save the form or print it out. The form is saved or printed out OK, but the dialogs wipe out most of the PictureBox drawing after they leave. Yet the buttons and progress bar on the form are restored OK -- so there must be a way to 'protect' the PictureBox drawing as well. How is it done?
The reason is that you need to repaint the picture box after the dialog has gone away.
Your picture box will get a Paint event, that will tell you that you need to do some re-drawing.
In there, use the supplied graphics contexts etc to draw on the picture box again. Probably you will want to write a separate method that does drawing, and call it for an initial view, and also during that paint. But that's up to you.
If you will always have olny one picture in your PictureBox, or you will be always using pictures with of same size you can just override OnEraseBkgnd function for this control and return TRUE on exit of this function.
This way content of your will not be altered by unplanned OnPaint or OnEraseBkgnd calls.
Related
Wondering if someone could help me....
I have a small .NET application where I have an Edit button on a main form. When the user clicks the Edit button, I want to popup a small form right next to it (on top of the main form) with a speech balloon tail attached on the side of the form pointing to the Edit button. So it gives the effect of a floating form pushed out from the Edit button.
I don't want the appearance of a normal speech bubble, I want it to look like an actual borderless form (with square corners). It could be a custom control or anything (however, I am not yet familiar with creating my own custom controls), but I need to add Text Controls, Pictures, Label Controls, etc. to this floating form.
Any help would be appreciated.
Thanks
UPDATE
I am trying to create something to this affect:
So imagine the ? being the Edit button and the other being a form with custom controls.
Like this?
To get this behavior:
Select an image which will represent your speech bubble. Put a PictureBox on the form, make it use this image. Place two labels, as above, adjust the font.
Pick your transparency key (color). Your image background and form background need to be set to that. I used black for demo purposes, which is a bad choice if you plan to have any black or text in your speech bubble. Set form's TransparencyKey property to this color in designer. More about TransparencyKey on MSDN.
(final step, not shown on the screenshot). Set FormBorderStyle = None.
Also make sure you have other ways to close your bubble, because X will be unavailable.
The final result may look something like the following:
Note: You may notice some drawing artifacts, most images on the internet have smooth borders, and transparency key does not support shades, has to be exact color. If you are fine with these minor artifacts, feel free to leave it at that. Or, edit it to get rough borders. Or find another one that better suits your needs.
I'd like to display an image which acts as a button. This means that when I click on the image a mouseUp handler is executed.
image area does not have a mouseUp handler
the buttons seem not to have a property where I can assign an image to them.
You can add a mouseUp handler to an image's script, and it will be triggered when the image is clicked:
on mousedown
answer "hi"
end mousedown
You can give a button an image by setting it's "icon" property to the id of an image on the stack (you will generally then want to unset the 'opaque', 'threed', and 'shadow' properties in order to make it look pretty).
What David said.
Though "mouseUp" is introduced as a natural button-related handler, be aware that any object can have such a thing, including a field if it is locked, and there are even ways around an unlocked field as well. The important thing is to know that the language and its environment are far broader and richer than you might at first infer from the tutorials and lessons.
Craig Newman
You can assign icons to buttons. This solves all your problems at once.
Create a new image control, e.g. by importing an image using the File menu. Create a new button and set the icon of the button to the id of the image control. If you like, you can use another image control for the hilitedIcon of the button. Add mouseUp and mouseDown handlers to the button as desired.
If the image is too big, resize the button to make the image fit. Set the showName of the button to false or change the margins (something like 0,32,0,8) to move the text below the icon.
In VB6 I have been using pictureboxes as containers a lot.
For example I put 5 pictureboxes onto a form, and as soon as the user clicked the "Next" button, I brought the next picturebox into the foreground.
This has been extremely convenient.
Now I am fighting with doing something similar in VB.NET.
My attempts were not really successful. A picturebox does not really hold my controls, they seem to jump out now and then, and I can not really make out on which picturebox a control is currently located since the picturebox is not opaque as in VB6.
Can somebody please tell me how to do this in a good way in VB.NET?
This sounds like a job for the Panel control
For your issues with panels that you posted a screenshot for. Your panel is within another container, that's why it's displaying strangely. Try clicking the panel and cutting it (ctrl-x) then clicking the form header, and pasting it (ctrl-p). That will ensure it isn't within another control as sometimes that can happen in a way that isn't exactly obvious (like how you can see the control borders in your screenshot).
Sometimes, I have a picturebox lets say 100x100. But the image it will display is actually 100x400.
I don't want to increase the size of the picturebox itself. Instead, I would like to create a vertical scrollbar (or horizontal if needed).
I could not find a scrollbar in the toolbox, so I guess I have to code it. But, how?
And I still wonder if I didn't make a mistake and didn't see the scrollbar in the toolbox. My apologies then :(
I suppose you could add separate scrollbar controls and sync their Scroll events up with the offset at which the picture in the PictureBox is drawn, but that sounds like actual work. There's a better way.
Add a Panel control to your form, and set its AutoScroll property to "True". This will cause the control to automatically show scrollbars when it contains content that lies outside of its currently visible bounds. The .NET Framework will take care of everything for you under the covers, without you having to write a single line of code.
Drag and drop your PictureBox control inside of the Panel control that you just added. The Panel control will then detect that one of its child controls is larger than its visible area and show scrollbars, thanks to the AutoScroll property. When the user moves the scrollbars, the portion of the image in your PictureBox that is visible will be automatically adjusted. Magic.
(The reason you have to use a Panel control as a container is because PictureBox does not inherit directly from the ScrollableControl base class, which is what provides the AutoScroll property.)
I tried this and it worked well. But I noted that if the picturebox is docked in the panel, the picturebox is automatically set to the size of the parent panel, and can't be set larger (at least not in any way I could find). This defeats the purpose of the technique. So -- put the picturebox on the panel, but don't dock it, and it will work perfectly.
There are no automatic scroll bars on a picture box, but you can add the VScrollBar (and HScrollBar) control to the form and handle the image scrolling manually by redrawing it at a different offset each time the Scroll event is fired.
In VisualBasic.Net When I activate a picture box and then draw something on it, it draws and then immediately goes blank. Works fine when I re-draw it, but almost always messes up the first time I draw on it. This has happenned with several different programs, and the help file is no help.
Try setting the DoubleBuffered property
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx
If that's not it, please provide more info.
Usually, if you're drawing something on a picture box or on another control, you have to take over the OnPaint event, and you're responsible of persisting what you draw on this event.
Thank you Andrew, but no help. I'm using .Net Framework 1.1, which does not offer the DoubleBuffered property... it was new in 2.0.
Not sure what additional info to provide.. the code is 300 lines long. When a button is clicked, the code expands my form, makes two picture boxes visible (one on top of the other (the back one is for some graph labels), and then uses some graphic brushes and pens to draw a graph on the front box. There's some database activity and calculations going on in the background at the same time.
I assume you're using the standard PictureBox component. Do you draw in the Paint-Handler? If not then the PictureBox will just erase your painted stuff next time it's asked to redraw itself (erase background etc.).
Yes, I believe I am using the standard picture box.
By Paint-Handler, I assume you mean a [Control].PaintEvent Handler. No I'm not using an event handler to do the drawing... drawing my chart is not an event in itself, but part of a much larger response to a button click event.
If you are saying that having the drawing code be part of a separate and specific handler can solve my problem, than I guess I could raise an internal event every time I want to redraw the chart. But I Would rather just figure out what is causing the PB to redraw itself without being told to.
If you cannot use the DoubleBuffered than you can HIDE a second picture box. You do the drawing in it and once it's completed you draw back to the VISIBLE one. This way the process of drawing is done on the hidden one and the white/flickering will not be shown.