Marc Vitalis
Marc Vitalis

Reputation: 2249

Force create handle for Control

I'm currently creating a silent print module. The current control I'm using is, it's making sure that the control handle is already created (IsHandleCreated). I did everything to cheat this with no luck at all.

Do you have ideas in mind on how can I create a handle for the control without displaying any in the screen?

Upvotes: 9

Views: 12178

Answers (5)

denisenkom
denisenkom

Reputation: 414

Try to overload CreateParams property getter. In it clear the WS_VISIBLE flag.

Upvotes: 5

NucS
NucS

Reputation: 629

Calling private method CreateHandle will do the work.

MethodInfo ch = frm.GetType().GetMethod("CreateHandle", BindingFlags.NonPublic | BindingFlags.Instance);
ch.Invoke(frm, new object[0]);

Upvotes: 0

NthDeveloper
NthDeveloper

Reputation: 1009

I solved this annoying handle creation problem by settings the WS_VISIBLE of CreationParams. You may either override the CreationParams property of Control or call the CreateHandle method with appropriate CreateParams instance. See the link

Upvotes: 0

Oliver
Oliver

Reputation: 45101

I had the same problem with some other controls and used the Control.CreateControl() method:

private void CheckForExistingHandle(Control control)
{
    if (!control.IsHandleCreated)
        control.CreateControl();
}

But i don't know how it works with a print module.

Upvotes: 3

wj32
wj32

Reputation: 8403

You have to access the Handle property (put the result in a dummy variable or something). Look in Reflector; it forces handle creation.

Upvotes: 23

Related Questions