Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Guys,

I want to ask a question. I am using the following function to learn the handle of a textbox in a windows form. But there are 3 different text boxes and they all have same class name(WindowsForms10.EDIT.app.0.378734a). How Can I know that I find the right one?

Thanks in advance

Lilly

childhandle = FindWindowEx(handle, IntPtr.Zero, "WindowsForms10.EDIT.app.0.378734a", null);
Posted

1 solution

This is not a problem. The class is the same but the handles are always different in different instances. In your example, if it returns a successfully found handle, its value will the same as the value of TextBox.Handle (inherited from Control) of one of the instances of your text boxes. In this way, you made a round trip. See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

In practice, this is never needed as you always have this handle through this property.

I don't know why would you try to find it using Windows API, perhaps because you did not know about this property.

A word of warning: the use of handles and Windows API (via P/Invoke) is not needed in correct .NET UI development; most of the problems can be solved using pure .NET libraries. Moreover, using handles would break compatibility with other platforms. Did you know that accurately written Forms application can run on many platforms without recompilation, not just Windows? This can be done using Mono (http://en.wikipedia.org/wiki/Mono_%28software%29[^]). Using handles (HWND) or P/Invoke will break such compatibility.

Too bad you did not share your ultimate goal. If you did, chances are, we would give you a good advice on how to do things using pure .NET libraries.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900