Question

I have a form with a control I draw on with another application. The other application loads too slow to start it each time I show my window.

So I load the secondary application in advance. I use a control handle as argument, telling the application which handle to draw on.

This works fine the first time. When I close my dialog (clicking the ok button, setting the dialog result to ok) and open it again, the control handle changed.

How can I keep my control handle the same between two loads of the dialog?

public partial class ListForm : Form
{

FilterDialog filterDialog;

public ListForm()
{
     InitializeComponent();
     filterDialog = new FilterDialog(); //this is where I load my second form, which I draw in
}

the Dialog I draw in

public FilterDialog()
{
    InitializeComponent();
    string applicationlocation = @"...";
    //set the argument
    string hexvalue = btnImage.Handle.ToInt32().ToString("X8");
    process.StartInfo.Arguments = "0x" + hexvalue;
    process.StartInfo.FileName = applicationlocation;
    process.StartInfo.UseShellExecute = false;
    process.Start();
}

private void FilterDialog_Load(object sender, EventArgs e)
{
    if (this.filter != string.Empty)
       txtFilter.Text = filter;
}
Was it helpful?

Solution

This code seems to work:

class CustomHandleForm : Form
{
    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        base.DestroyHandle();
    }

    protected override void DestroyHandle() { }        
}

Tested with:

Form f = new CustomHandleForm();
f.Show();
var h1 = f.Handle;
f.Hide();
f.Show();
var h2 = f.Handle;

Trace.Assert(h1 == h2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top