Mohammed Asaad
Mohammed Asaad

Reputation: 138

How can I get an image of control to another control?

Look this class:

public class ControlContainer : Control
{
    public ControlContainer() { DoubleBuffered = true; SetStyle(ControlStyles.SupportsTransparentBackColor, true); }
    Control _Control;
    public Control Control { get { return _Control; } set { _Control = value; SetHandlers(); } }

    public void SetHandlers()
    {
        if (_Control == null) return;
        Region = _Control.Region;
        Size = _Control.Size;
        _Control.Invalidated += new InvalidateEventHandler(_Control_Invalidated);


    }

    void _Control_Invalidated(object sender, InvalidateEventArgs e)
    {

        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (_Control == null) return;
        Bitmap p = new Bitmap(_Control.Width, _Control.Height);


        _Control.DrawToBitmap(p, new Rectangle(0, 0, _Control.Width, _Control.Height));
        e.Graphics.DrawImage((Image)p, 0, 0);
        base.OnPaint(e);
    }


}

As you see the mission of this control is to get an image of another control and draws it. But if we handled a TextBox control named 'textBox1' as the following:

Suppose we have added a new instance of the type ControlContainer above and its name is controlContainer1 and we assign the value of the property 'Control' of controlContainer1 to textBox1.

Why if I write something in textBox1 the event 'Invalidated' doesn't fire? and why the pointer of the writing "|" doesn't appear by the method 'DrawToBitmap"?

Upvotes: 0

Views: 99

Answers (1)

Florian
Florian

Reputation: 547

Short answer: Because the TextBox control doesn't support the event Invalidated. Perhaps it will help if you call Invalidate() on event TextChanged of the textbox.

Longs answer: It's beacuse how windows is internal handling a Win32 native textbox. If you start editing, a control without borders, the same color at the same location as the textbox will be created. Now all events go to this Win32 internal editbox. You can observe this behaviour if you override the OnPaint for a textbox and fill the whole textbox with FillRectangle. As soon as the textbox is edited, you see the new editbox in the (original) color of the proprty BackColor.

Upvotes: 1

Related Questions