I created my own control and overwritten the onpaint event,
the problem is that the paint event stopped working
Any ideas why? And how to restore it?
Let's have a telepathic guess here:
You forgot to call base.OnPaint(...) inside your override. Meaning that the base functionality is no longer invoked.
Maybe the control is obscured. I had a similar problem, and the problem was that the form in the Designer view was bigger than the actual form when the application was run. My custom control had anchors on all sides, and when the main form was reduced in size, the custom control went to zero size (actually negative I guess).
In this mode, the OnPaint override nor a delegate assigned to the Paint event is not called at all.
Maximize your form and make sure that you didn't accidentally reduce the size to zero!
Related
I have a WinForms form containing many controls. On resizing the form, I need to place and resize the components and redrawing pictures in PictureBoxes.
I had problems with flickering, so now I call SuspendLayout at the beginning of the SizeChanged event (and ResumeLayout at the end).
But it was not enough, so I use a boolean variable indicating, if the form should be repainted. I set it to true at SizeChanged event and false after the Paint event. This solved the problem, because for some mysterious reason the Paint event was raised incredibly often.
The form still did not repaint correctly sometimes. I solved that by calling SizeChanged method in ResizeEnd event (and I don't know why it worked).
Now almost everything works, but after maximizing/unmaximizing the form, the pictures in the PictureBoxes are not repainted. Everything is sized and placed right, except for the pictures, which have the same size (but the PictureBoxes are resized). I have no idea why or what else I can do (except for disabling maximizing).
I would appreciate any advice.
I have a problem with panel and weird area on it. I fill my panel with many PictureBoxes 32x32px, and a small area of this panel is filled with white area.
Here is how it looks like:
You can see that the first PictureBox has specified grass image, which is 32x32px, but the PictureBox below has only half of it's image. It's very strange.
I have also an onClick event specified for PictureBoxes to change it's background to other image. If I click on 'working' PictureBox it's background changes, but when I click on 'corrupted' one, it doesn't.
So basically, my question is - what could be the reason for such effect? Is it possible to find it out without analysing a code? I would like to avoid putting a code here, because it's very complicated and long.
EDIT
I used WinSpy++ and it is the result (red point is a place where i hover cursor)
so we can see that PictureBox is partly hidden behind this white area.
I don't know if the question is still active, but I'll try to respond anyway. It is more a comment on it, but since I'm not allowed to make comments yet I'll just answer.
I had encountered a similar issue when implementing some picutreBox drawing using onPaint event handler. The problem was that I called pictureBox.Invalidate() during onPaint and it caused displaying of the unwanted white color box. You might want to avoid using Invalidate() or Refresh() in your onPaint event if there is one.
If this is not the case it might also help to refresh the form or pictureBoxes that are corrupted. Try to call this.Refresh() after the form initialization, preferably in onLoad or onShown event handler.
If it still doesn't help then the problem is somewhere else, I'd guess there is a control hidden somewhere that causes that. But we'll need to see some code in order to suggest any other advice.
I have a simple System.Windows.Forms.Form.
Based on business requirements, once certain functionality becomes available as a result of some background processing, I am increasing the form's size and opening up a previously hidden area with additional controls (buttons etc). The changes to the form's size are done by a background thread, using BeginInvoke.
All this works fine. However, if the user is dragging around the form on the screen, and coincidentally during this time the method that changes form's size is called, the size change does not become effective (technically, the form changes size, but instantaneously reverts to the previous size).
I am changing the form size by setting the Form.Size property, but have tried other ways like setting Form.ClientSize, and calling Form.SetBounds(). Have also tried out Form.SuspendLayout()/Form.ResumeLayout() and forcing Form.PerformLayout().
Nothing I have tried so far works, and when it is being moved around, the form refuses to change size.
Put code in the Form_LocationChanged event to detect if the previously hidden area is visible (or should be via a bool variable) and resize the form accordingly. Otherwise the ResizeEnd event fires after a move ends, try that.
I feel quite limited by the default ContextMenuStrip, as it only can contain buttons, and no Controls.
I was wondering that for a long time, and I already tried it, using forms, but it never really worked out.
I already have I idea on how to set the whole thing up, with events and items. The only problem I have is the paint method.
When you open a ContextMenu (ContextMenuStrip) you can set its position on the mouse cursor, and it will be there, even if that means that it goes beyond the active form. (So I can't use the Controls Class as inheritance, as they can only draw themself as a part of a form.
Now I thought to use the Form Class as a base for my ContextMenu, but those where placed on the screen randomly.
So what I actually need is a class (or something similar) that can draw itself, without problems, and can be placed accurately on the screen.
Any hint would be nice, thanks.
Greg the Mad
Your first statement is false -- you can have a TextBox or a ComboBox in a ContextMenuStrip.
MSDN ToolStripComboBox
MSDN ToolStripTextBox
From the designer there is a small drop-down arrow when your mouse is in the "Type Here" box (sometimes hard to click) that will allow you to change the type.
If you are looking to allow for any type of control to be displayed in a top down fashion inside of a container to be positionable... you could always make a custom control using FlowLayoutPanel. With it's properties FlowDirection=TopDown and WrapContents=False to keep a vertical approach. This will handle your "menu" basics and your new control can expose whichever events you wish from each Control. You will have to handle the logic of showing the panel and positioning with it's Location property as well.
I forgot to address the issue with drawing outside of the parent form. Notice that ContextMenus are smart and when they reach a boundary of their parent they draw away from it. You should logically be able to draw in the correct direction (Up/Down or Left/Right) from any right mouse click. Per your attempt with a Form, set StartPosition=Manual then prior to calling Show() or ShowDialog() set it's Location property respective to the X and Y parameters provided in the event args of MouseClick.
I have a User Control that has some WinForm Controls inside. I want to be able to paint those inner controls myself (for scrolling purposes, some of them will remain, others will move).
Even when I override the OnPaint and the OnPaintBackground method, those controls keeps showing on my usercontrol which is completely black now because there is no painting methods on it.
Is there a way I can suppress those controls from been painted and then paint them by myself with the DrawToBitmap method of each control?
Yes. Remove them from (or better yet, never add them to) the UserControl's set of child controls. Simulating interaction with the controls is going to be a PITA, though.
Do you just need the controls to "look" like they're there. Or do they need to actually be there? If its the latter, you would be better off faking the scrolling somehow by just repositioning the controls manually.
Trying to re-invent the windowing system is an exercise in pain. You will be better off if you learn and work within its paradigms.
If you don't need interaction, just set every child control .Visible = false.
Otherwise, have you tried WM_SETREDRAW?