[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regain form instance from handle

Posted on 2006-05-16
5
Medium Priority
?
253 Views
Last Modified: 2010-04-16
Is there any way to create and instance of a form from a handle?

Ex.

Form myForm = new Form();
IntPtr ptr = myForm.handle;

........
//This is where I would like to be able to create an instance of that form so that I can regain managed control of the form.  The IntPtr is used as a holder for a particular form.  When created, the handle is stored in the IntPtr.  Later on when an instance of that form is created, if the IntPtr is holding a value, I would like to pull that instance and make it active instead of creating another instance.  So can an instance of a form be created from an Handle?

Form anotherform = ptr;  

0
Comment
Question by:gvector1
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16693173
One has to wonder why you would do it this way?...

You ALREADY have a reference that you can use to test whether the form is still valid or not:

    Form myForm = new Form();

Just keep "myForm" around so you can test it later.  Either move it to class level or store it in some kind of collection for example.

Do you need to see code on how this is accomplished?

It really sounds like you are making this way more complicated than it needs to be...
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16693284
Such problem exists when we must find managed class reference from unmanaged callback function using context information. Context information is usually 32-bits integer passed to unmanaged function. Having this number we must call managed function from class instance which initiated callback.
This is done using GCHandle structure and not window handle. If this is your case, you can see how it is done in EnumWindows sample, GCHandle MSDN description. I can give you additional information if you need.
0
 

Author Comment

by:gvector1
ID: 16693777
Okay, if I just keep an instance of my form around for use later how can I bring it to the front later.
Ex.

If my form is minimized and I change the windowstate to normal, it will come up and display in the front, but if my form is in the background, how can I cause it to come to the foreground???
0
 

Author Comment

by:gvector1
ID: 16693805
Nevermind.  All I have to do is focus the window.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16693852
Here is an example that illustrates it further:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Form frm = new Form();
       
        private void Form1_Load(object sender, EventArgs e)
        {
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new Form();
            }
            if (frm.WindowState == FormWindowState.Minimized)
            {
                frm.WindowState = FormWindowState.Normal;
            }
            frm.Show();
            frm.BringToFront();
        }

    }
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question