Retrieving control's original class by its ID?
-
Hello, In my program I dynamically create controls according to data read from a database. The control can be either CEdit or CComboBox. I also assign a unique control ID to each of the created controls (I know the ids). Once controls created when a button pressed it's supposed to save controls values in the database. Getting the text from the CEdit control isn't a problem as I just do GetDlgItemText(...). However, in case of the CComboBox I need to retrieve the ItemData using GetItemData. This is where I'm unsure of what to do. I don't save anywhere if control with specific ID is a CComboBox or CEdit. Is is possible to know only by ID what kind of a control it is and to retrieve its original class?. I was thinking just to make a struct: struct { CEdit *edit=NULL; CComboBox *combo=NULL; } myDynControls; and then make an array of this struct. Each coresponding to an ID. I won't have more than 10 controls at a time. What do you think? Is it a good idea? (i will delete everything after done working with ofcourse :))
-
Hello, In my program I dynamically create controls according to data read from a database. The control can be either CEdit or CComboBox. I also assign a unique control ID to each of the created controls (I know the ids). Once controls created when a button pressed it's supposed to save controls values in the database. Getting the text from the CEdit control isn't a problem as I just do GetDlgItemText(...). However, in case of the CComboBox I need to retrieve the ItemData using GetItemData. This is where I'm unsure of what to do. I don't save anywhere if control with specific ID is a CComboBox or CEdit. Is is possible to know only by ID what kind of a control it is and to retrieve its original class?. I was thinking just to make a struct: struct { CEdit *edit=NULL; CComboBox *combo=NULL; } myDynControls; and then make an array of this struct. Each coresponding to an ID. I won't have more than 10 controls at a time. What do you think? Is it a good idea? (i will delete everything after done working with ofcourse :))
Hi Daredevil, All MFC classes that derive from CObject (of which CWnd does) have a CRuntimeClass object associated with it. So to see if your object is a CEdit control:
CWnd* pWndControl = m_arrMyControls[i]; // or however you've implemented the collection of controls
if (pWndControl && pWnd->GetSafeHwnd())
{
CRuntimeClass* pClass = pWnd->GetRuntimeClass();
// MSDN says that pClass will never be NULL, but we should always check pointers before use
if (pClass)
{
CString strName = pClass->m_lpszClassName;
// Now you can do whatever you need to do based on what the runtime class is
// ...
// ...
// ...
}
}Hope this helps, Andy
-
Hello, In my program I dynamically create controls according to data read from a database. The control can be either CEdit or CComboBox. I also assign a unique control ID to each of the created controls (I know the ids). Once controls created when a button pressed it's supposed to save controls values in the database. Getting the text from the CEdit control isn't a problem as I just do GetDlgItemText(...). However, in case of the CComboBox I need to retrieve the ItemData using GetItemData. This is where I'm unsure of what to do. I don't save anywhere if control with specific ID is a CComboBox or CEdit. Is is possible to know only by ID what kind of a control it is and to retrieve its original class?. I was thinking just to make a struct: struct { CEdit *edit=NULL; CComboBox *combo=NULL; } myDynControls; and then make an array of this struct. Each coresponding to an ID. I won't have more than 10 controls at a time. What do you think? Is it a good idea? (i will delete everything after done working with ofcourse :))
How about using
IsKindOf()
?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen