Reputation: 45
I am trying to create multiple row of controls (edit box , checkbox and a delete button) in an M.F.C Dialog dynamically at run-time on button click. And also all the controls added should be displayed in a scroll window. But unfortunately i can't find any solution to this problem. Anyone got any ideas?.
I can create a single row of controls. My problem is how to add multiple rows when click the Add Button and also Delete that row by clicking the Delete button on the same row.
It should look somewhat like this after i clicked Add multiple times:
| Edit Box: Type in a Name | |Add|
|Edit Box| |Name 1 (Edit Box)| |Edit Box| |Delete| ^
|Edit Box| |Name 2 (Edit Box)| |Edit Box| |Delete| |
|Edit Box| |Name 3 (Edit Box)| |Edit Box| |Delete| |
etc... v
Here is some code of mine:
int CSettingDlg::AddControlSet() //Create a single row of controls
{
int d = 3500;
if (m_pStrAdd.IsEmpty() == FALSE)
{
GetDlgItem(IDC_TEST1)->GetWindowRect(&rect);
ScreenToClient(&rect);
EditBox = new CEdit;
EditBox->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY, rect, this, d++);
//using the same code above to create 2 more Edit box
Delete = new CButton;
Delete->Create(_T("Del"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, rect, this, d++);
return TRUE;
}
}
void CSettingDlg::OnBnClickedAddSettingdlg() //Add button Event handler
{
UpdateData();
AddControlSet();
}
Upvotes: 1
Views: 943
Reputation: 1815
When you are in dealing with dynamic creation of control in window environment, you should aware X and Y direction to draw/create a control on form.
Following is direction of monitor:
Now lets say you are creating control on form starting with CRect rect(0, 0, 100, 50);
Then one control in single row will be place in this location (Edit Box in your case) and when you are adding delete button next to edit box in same row then you should add few co ordinates in X axis to get new location of delete button. Hence rect of next location will something like, CRect rect(105, 0, 205, 50);
Same way when to move to next row then Y axis should be added with few coordinates to get new location for next row. For example, Next row edit control coordinates will beCRect rect(0, 55, 100, 105);
Upvotes: 1