Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm creating a application that generates reports form different .net application froms, the problem is there is multiple text boxes on the form and all the text boxes class name is the same, is there a way get the unique name for each control ?


private List<Data.Objects.ClassObject> GetChildClassesWithName(IntPtr hParent, int pI)
       {
           int ii = 1;
           int i = 0;
           int iMax = 100;

           IntPtr prevChild = IntPtr.Zero;
           IntPtr currChild = IntPtr.Zero;

           try
           {
               while (true & i < iMax)
               {
                   currChild = FindWindowEx(hParent, prevChild, null, null);
                   if (currChild == IntPtr.Zero)
                   {
                       return null;
                   }
                   else
                   {
                       ii++;

                       // Get current ClassName
                       int nRet;
                       StringBuilder ClassName = new StringBuilder(256);
                       nRet = GetClassName(currChild, ClassName, ClassName.Capacity);

                       Data.Objects.ClassObject item = new Data.Objects.ClassObject();
                       item._Cap = GetTextBoxText(currChild);
                       item.Name = ClassName.ToString();
                       item._Curr = currChild;
                       item.ParentID = pI;
                       item.CurrID = ii;

                       if (nRet != 0)
                       {
                           string[] ignore = { "" };
                           if (!ignore.Contains(ClassName.ToString()))
                           {
                               if (item._Cap != null)
                               {
                                   classlist.Add(item);
                               }
                               else if (item.Name == Data.DataManager.GetSettingValue("DesktopClassName"))
                               {
                                   item.CurrID = 1;
                                   item.ParentID = 12;
                                   item._doc = currChild;

                                   classlist.Add(item);
                               }
                           }
                           GetChildClassesWithName(currChild, ii);
                           prevChild = currChild;
                       }
                       else
                       {
                           return null;
                       }
                   }
               }

               return null;
           }
           catch (Exception ex)
           {
               string error = ex.Message;
               return null;
           }
       }
Posted
Comments
Dave Kreskowiak 25-Nov-14 9:16am    
Your question doesn't make a lot of sense because windows don't have "unique names" like you find in code. They have integer window handles that uniquely identify them to Windows.
[no name] 25-Nov-14 9:34am    
Thanks, but is the integer window handles the same each time or is it different for different process,threat,system etc ?
BillWoodruff 25-Nov-14 9:50am    
If you create these Forms, why can't you give each TextBox Control a unique name ?
Sergey Alexandrovich Kryukov 25-Nov-14 10:16am    
Makes no sense. There are no such names...
—SA

1 solution

Member 11117223 wrote:

Thanks, but is the integer window handles the same each time or is it different for different process, threat, system, etc?
Window handles are system-wide. Unlike pointers which are different in the address spaces of different process, window handle (as well as other handles, atoms, etc.) are designed specifically to be shared between all processes. They are handles, not usual pointers.

And there is no "etc.". The unit of isolation is application domain AppDomain; and all processes are in separate application domains. The application domains have their separate address spaces; pointers/handles (even to the same object shared by different processes) can have different values in different address spaces; and if some addresses, by any change, have the same numeric values, they point to different objects in different application domains or processes. You can create more then on application domain per process, but this is a different story: http://msdn.microsoft.com/en-us/library/system.appdomain%28v=vs.110%29.aspx[^].

Now, as Dave Kreskowiak at and I tried to explain, the question makes no sense, but that's not all. If you think that you can rely on the property Name of control, think again. Those names are only important for the designer, are used to generate code, to figure out the names of the generated members. During runtime, there are insignificant. If you create controls in your own code, you don't need to use these names. Moreover, you can modify the during runtime (say, assign null to all of them), just to see that they don't play any role. Therefore, you should never rely on those names. And of course, they don't even exist in raw Windows API you use through P/Invoke.

—SA
 
Share this answer
 
v2
Comments
[no name] 25-Nov-14 15:03pm    
Thanks for this, will look in different aspect now !
Sergey Alexandrovich Kryukov 25-Nov-14 15:32pm    
Very good. You are very welcome.
Your follow-up questions will be welcome.
Good luck, call again.
—SA

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