System windows forms controlstyles




















How can I solve these 2 problems? OnPaint e 'If the text is empty now, the watermark text should be written instead. ForeColor e. DrawString Me. Text, Me. TextChanged Me. Invalidate End Sub Watermark appears. When I remove the text, the watermark does not re-appear, unless I hover with the mouse over it. Improve this question. Herb Herb 1, 2 2 gold badges 9 9 silver badges 29 29 bronze badges. When overriding OnPaint you should call MyBase. OnPaint e before doing anything else this applies to any overridden method, by the way.

VisualVincent, thanks for the suggestions. I incorporated them, but the behaviour stll is not as it should be. See the Edit, please. Very odd that mouse hover causes such strange behaviour, and unfortunately I am in not in a position to test it right now By the way, do you need to be able to customize this watermark color, font, etc.

VisualVincent, the watermark font is the same as used for the Text, but the color needs to be user definable. This cannot work reliably, TextBox dates back from the era where Windows had to run on sux hardware. TextBox already supports a watermark well: stackoverflow. Add a comment. Active Oldest Votes. Edit: Resurrected due to OP giving up on custom fore color requirement. Improve this answer. CreateGraphics oGraphics.

TextChanged If Me. I think what rather solved your problem was CreateGraphics as it creates a new graphics object drawing on top of the text box. CreateGraphics then use g to draw, otherwise it causes memory leaks. VisualVincent, thanks for your patience.

However, going with this answer causes all sorts of strange side effects: text sometimes is drawn with a different font, sometimes it overlaps itself, etc. I can not recommend using it. In fact, it is probably best to remove the question altogether. Hans Passant has provided a link to a PInvoke solution in the comments of the original question, probably the same you were mentioning there. It's the same as what I mentioned, yes. Although you cannot customize the color using that solution.

TnTinMn posted a similar answer with example code, but removed it again as I told him you needed to customize the color. A workaround would be to place a label inside the text box using Me. The following example demonstrates a use of ControlStyles with the StyleChanged event. Controls use this enumeration in various properties and methods to specify functionality. A control can enable a style by calling the SetStyle method and passing in the appropriate ControlStyles bit or bits and the Boolean value to set the bit s to.

For example, the following line of Visual Basic code would enable double-buffering. However the preferred method for enabling double buffering, which yields the same result, is to set the DoubleBuffered property for the control to true. If the SupportsTransparentBackColor bit is set to true , and the BackColor is set to a color whose alpha component is less than , OnPaintBackground will simulate transparency by asking its parent control to paint the background.

This is not true transparency. If there is another control between the control and its parent, the current control will not show the control in the middle. When the UserMouse bit is set to true , the following methods are still called: Control. OnMouseDown , Control. OnMouseUp , Control.

OnMouseEnter , Control. OnMouseMove , Control. OnMouseHover , Control. OnMouseLeave , and Control. When the control is clicked, if the StandardClick bit is set to true the Control. OnClick method is called and it raises the Control. Click event. When the control is double-clicked, and both the StandardClick and StandardDoubleClick bits are set to true , the click is passed on to the DoubleClick event. Then the Control.

OnDoubleClick method is called and it raises the Control. DoubleClick event. For more information on control click and double click behaviors, see the Control. Click and Control. When flickering occurs, you are seeing the rendering results of each phase in quick succession.

The first phase erases the invalid region by painting it with a Windows-level background brush. The second phase sends the PaintBackground event for your form or control to paint the background, something that your base class generally handles for you using the BackColor and BackgroundImage properties. But you can handle it yourself:. OnPaintBackground e ; e. FillRectangle Brushes.

Black, this. Double buffering is a technique by which you can combine the three phases into a single paint operation and thereby eliminate flicker. To make this work, you apply the three painting phases to a second, internally managed graphics buffer, and, when they're all finished, they're rendered to the screen in one fell swoop. ControlStyles enumeration to true: [7].

You should avoid the DoubleBuffer style except for backwards compatibility. SetStyle ControlStyles. OptimizedDoubleBuffer, true ; this. To save time, you can implement double buffering by opening your form in the Windows Forms Designer and setting its DoubleBuffered property to true from the Properties window.

Consequently, you should set DoubleBuffered to true on all custom controls and user controls to ensure that double buffering is enabled:. Requesting double buffering using either ControlStyles or the DoubleBuffered property is an all-or-nothing approach; each paint operation creates a new buffer, renders to it, renders from the buffer to the screen, and releases the buffer.

The more intensive your rendering requirements are, the more likely it is that you'll demand more fine-grained control and flexibility from double buffering. When animating, for example, you probably prefer to retain your double buffer across paintsrather than create and dispose of each paint operationand thus avoid costly memory allocation. For this, you can do as ControlStyles. OptimizedDoubleBuffer does and use buffered graphics support from System.

In most cases, you create a buffered graphics context from which you allocate one or more buffered graphics drawing surfaces, each of which represents a graphics surface to which you'll render. The buffered graphics context is actually the off-screen buffer that you render to. It is exposed via System. Your first step is to instantiate BufferedGraphicsContext and specify the size of the offscreen buffer using the MaximumBuffer property:. After you've created the off-screen graphics buffer, you create a Graphics object that allows you to render to it.

You also specify the target graphics surface that your buffered graphics will ultimately render to. Both needs are satisfied by calling BufferedGraphicsContext's Allocate method:.



0コメント

  • 1000 / 1000