finding opened window based on its handle ( C# )

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! When I create a new form programatically, I can save its handle in a collection of some type
Later, I need to find that form based on a handle I saved and call one of its functions. The newly create
form can be a child of an MDI form but it very well may not be, so I can not use MDIChildren

I am wandering how it can be done in C#, please

Thank you in advance

Dany
 
Use Control.FromHandle.

Niki

Daniel H. said:
Hi! When I create a new form programatically, I can save its handle in a collection of some type.
Later, I need to find that form based on a handle I saved and call one of
its functions. The newly created
 
Hi! Thank you for answering my question.
I am having difficulty using this method though and docs+internet do no
provide any examples

I have a collection FormHash and I add page name and form handle

if (!AppConfig.FormHash.Contains(this.m_pageName)) AppConfig.FormHash.Add(this.m_pageName,this.Handle)

Ideally this should work

IntPtr myPtr = (System.IntPtr) AppConfig.FormHash[this.m_pageName]
MainForm newForm2 =(MainForm) Control.FromHandle(myPtr)
newForm2.Activate()

However, the call bombs with the invalid cast error on MainForm newForm2 =(MainForm) Control.FromHandle(myPtr)

What am I doing wrong? -- must be really stupid

Any help is greatly appreciated

Dan

----- Niki Estner wrote: ----

Use Control.FromHandle

Nik

Daniel H. said:
Hi! When I create a new form programatically, I can save its handle in collection of some type
Later, I need to find that form based on a handle I saved and call one o
its functions. The newly create
 
Don't know; Use the debugger and see if the handle is really the same, look
what control is returned by "FromHandle".

BTW: Why do you store a handle (and not a reference to the main form) in the
first place?

Niki

Daniel H. said:
Hi! Thank you for answering my question.
I am having difficulty using this method though and docs+internet do not
provide any examples.

I have a collection FormHash and I add page name and form handle:

if (!AppConfig.FormHash.Contains(this.m_pageName)) AppConfig.FormHash.Add(this.m_pageName,this.Handle);

Ideally this should work:

IntPtr myPtr = (System.IntPtr) AppConfig.FormHash[this.m_pageName];
MainForm newForm2 =(MainForm) Control.FromHandle(myPtr);
newForm2.Activate();

However, the call bombs with the invalid cast error on MainForm newForm2
=(MainForm) Control.FromHandle(myPtr);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top