• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Regain form instance from handle

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
gvector1
Asked:
gvector1
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
AlexFMCommented:
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
 
gvector1Author Commented:
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
 
gvector1Author Commented:
Nevermind.  All I have to do is focus the window.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now