I am a beginner in building MFC application. I've just started using list controls (in report view) and I am facing some problems while updating the list. I have three buttons for add, update and delete. Everything works well except the update. Here's the code.
void CAddDetailsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_DEPARTMENT, departmentControl);
DDX_Text(pDX, IDC_NAME, m_name);
DDX_Text(pDX, IDC_ID, m_id);
DDX_Text(pDX, IDC_AGE_BUDDY, m_ageVariable);
DDX_CBString(pDX, IDC_DEPARTMENT, m_department);
DDX_Control(pDX, IDC_LIST1, m_listControl);
}
BOOL CAddDetailsDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: Add extra initialization here
ageSpin=reinterpret_cast<CSpinButtonCtrl*>(GetDlgItem(IDC_AGE_SPIN));
ageBuddy=reinterpret_cast<CEdit*>(GetDlgItem(IDC_AGE_BUDDY));
ageSpin->SetBuddy((ageBuddy));
ageSpin->SetRange32(18,60);
departmentControl.AddString("Human Resource");
departmentControl.AddString("Manager");
departmentControl.AddString("Administrator");
departmentControl.AddString("Desktop Engineer");
m_listControl.InsertColumn(0,"ID",0,100);
m_listControl.InsertColumn(1,"Name",0,100);
m_listControl.InsertColumn(2,"Age",0,60);
m_listControl.InsertColumn(3,"Department",0,100);
m_listControl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
m_ageVariable="18";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAddDetailsDlg::OnBnClickedEdit()
{
// TODO: Add your control notification handler code here
UpdateData();
if((m_id=="")||(m_name=="")||(m_department=="")||(m_ageVariable==""))
{
MessageBox("Please choose an item to edit","Error");
}
else
{
int index=m_listControl.GetSelectionMark();
m_listControl.SetItemText(index,0,m_id);
m_listControl.SetItemText(index,1,m_name);
m_listControl.SetItemText(index,2,m_ageVariable);
m_listControl.SetItemText(index,3,m_department);
MessageBox("Successfully Updated","Info");
}
}
void CAddDetailsDlg::OnBnClickedNewButton()
{
// TODO: Add your control notification handler code here
UpdateData();
if((m_id=="")||(m_name=="")||(m_department=="")||(m_ageVariable==""))
{
MessageBox("Please fill in all the details","Error");
}
else
{
int count=m_listControl.GetItemCount();
count=m_listControl.InsertItem(count,m_id);
m_listControl.SetItemText(count,1,m_name);
m_listControl.SetItemText(count,2,m_ageVariable);
m_listControl.SetItemText(count,3,m_department);
}
}
Note:-
The update function works fine if I update only the ID. If I try to update all/ many fileds, only the ID gets updated and nothing else. BTW, age is a spinControl, department is a comboBox and the other two are editBox.
Edit:-
I found that both, the value of variable m_name and the editBox value changes to the older values after the line m_listControl.SetItemText(index,0,m_id);. Its the same case with m_age and m_department.
If I comment the line m_listControl.SetItemText(index,0,m_id);, I can update everything at a time except the ID.
I am able to update everything by storing m_name, m_age and m_department in a local variable just before the line m_listControl.SetItemText(index,0,m_id); and using those variables in SetItemText(). But as I'm learning, I wanna know where I'm going wrong.
i think you forget to add UpdateData() before your code which is under Update_Bn_Click because at my side i use your code with updatedata() and its working fine.
UpdateData();
int index=m_List.GetSelectionMark();
m_List.SetItemText(index,0,m_id);
m_List.SetItemText(index,1,m_Name);
m_List.SetItemText(index,2,m_Age);
m_List.SetItemText(index,3,m_DepartMent);
Try pumping some messages after updating the items.
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Turn off your sorting.
In your designer:
Properties>Behaviour>Sort set to None.
You have to add Item into 0 colomn index first.
listcontrol->InsertItem(0,_T("text"));
then, you can set text to the subItem;
listctrol->SetItemText(0,1,_T(subText)):
First, make sure the Owner Data property of the control is set to FALSE.
Maybe try m_List.Update(index) after the last SetItemText().
I must admit that everywhere I need updated list elements, I use an Owner Data CListCtrl because I think its faster in case of a big number of items and easier to handle in the long term.
Related
I have a code like this to write install log to a static text and a list control, and i have a button to start the installer that be handle by function OnClickInstallBtn() but every time I call the WriteLogtoScreen(), only the static text change and nothing show up in the list until the OnClickInstallBtn() is done and everything on that list show up all at one.
How can i make it show up right away like the static text?
WriteLogtoScreen(LPCTSTR sLog)
{
int iItems;
iItems = m_ListLog.GetItemCount();
m_ListLog.InsertItem(iItems, sLog);
m_ListLog.Update(iItems);
m_ListLog.SetItemText(iItems, 0, sLog);
m_ListLog.Update(iItems);
UpdateData(FALSE);
SetDlgItemText(IDC_STATIC, sLog);
}
You should call RedrawWindow() if you want to force the redraw of your list explicitly in something like this :
void WriteLogtoScreen(LPCTSTR sLog)
{
int iItems;
iItems = m_ListLog.GetItemCount();
m_ListLog.InsertItem(iItems, sLog);
m_ListLog.Update(iItems);
m_ListLog.SetItemText(iItems, 0, sLog);
m_ListLog.Update(iItems);
UpdateData(FALSE);
//instant redraw
m_ListLog.RedrawWindow();
SetDlgItemText(IDC_STATIC, sLog);
}
I am trying to fix a validation bug in a MFC CEdit control. Currently, validation is performed in an OnChange event handler. But this does not work because it validates data before the user is finished entering it.
So, instead, I am trying to validate inside an OnKillFocus event handler. If validation fails, then I use GotoDlgCtrl() to return focus to the edit box that contained the invalid data. And when I call GotoDlgCtrl(), the kill focus event fires again, and I'm in an infinite loop.
So, I'd like to handle an event that fires just before the control loses focus, so that if I determine that the data is invalid, I can stop focus from leaving and instead get the user to enter correct data.
I know I've seen a Validating event someplace, but that was probably in the .Net world. But it offers the functionality I'm looking for.
Right-click the dialog resource and invoke Class Wizard:
Next, go to the Virtual Functions tab, locate PreTranslateMessage and add it:
Then, you can do something like this:
BOOL CTestDlgDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_CHAR)
{
CWnd *pControl = GetDlgItem(IDC_EDIT1);
if (pControl->GetSafeHwnd() == pMsg->hwnd)
{
if (pMsg->wParam == _TINT('!'))
{
AfxMessageBox(_T("Not allowed ! character"));
return TRUE;
}
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
Normally the control is a member variable of type CEdit so you could compare against m_edit.GetSafeHwnd() instead.
Results:
Update
I realise you stated:
But this does not work because it validates data before the user is finished entering it.
You could use WM_KEYUP instead:
BOOL CTestDlgDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYUP)
{
CWnd *pControl = GetDlgItem(IDC_EDIT1);
if (pControl->GetSafeHwnd() == pMsg->hwnd)
{
CString str;
GetDlgItemText(IDC_EDIT1, str);
if (str.Find(_T("!")) >= 0)
{
AfxMessageBox(_T("Not allowed ! character"));
return TRUE;
}
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
That is give you a chance to validate after the display has been updated.
An alternative it to customize your DoDataExchange handler. In there you can validate as required. Then in your code you simple test the return value of UpdataData(TRUE) for FALSE.
I'm using spin control in MFC, C++ to change the value for the number.
here is my code:
void CHello_worldDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
m_fSpinIncrement = m_fScalingFactor;
if(pNMUpDown->iDelta == -1) // Increment the value
{
IncrementData(m_fSpinIncrement);
}
else // Decrement the value
{
DecrementData(m_fSpinIncrement);
}
SetDlgItemText(IDC_TEXT3, IDC_SPIN1);
*pResult = 0;
}
is there any error inside? and the number change will be at another static textbox~
Thanks
You're doing too much work. Everything you're trying to do can be done better by simply setting the appropriate properties of the spin control:
Auto Buddy connects the spin control to the edit box preceding it.
Set Buddy Integer makes the edit box handle integers automatically.
Alignment set to 'Right' will make the spin control neatly dock against the edit box.
The only code you need is calling SetRange() in the OnInitDialog.
Did you call the SpinButtonCtrl::SetRange() method in any part of your code? From your code, I don't think it is getting the range you want.
I'd like to know more about how this system works, specifically when and how the framework actually decides to update a UI element.
My application has a 'tools' system where a single tool can be active at a time. I used the "ON_UPDATE_COMMAND_UI" message to 'check' the tool's icon/button in the UI, which affected both the application menu and the toolbars. Anyway, this was all working great until some point in the last couple of days, when the toolbar icons stopped getting highlighted properly.
I investigated a little and found that the update command was only being received when the icon was actually clicked. What's strange is this is only affecting the toolbars, not the menu, which is still working fine. Even when the buttons in the menu are updated the toolbar icon stays the same.
Obviously I've done something to break it - any ideas?
EDIT:
Never mind. I'd overwritten the Application's OnIdle() method and hadn't called the original base class method - that is, CWinApp::OnIdle() - which I guess is where the update gets called most of the time. This code snippet from https://msdn.microsoft.com/en-us/library/3e077sxt.aspx illustrates:
BOOL CMyApp::OnIdle(LONG lCount)
{
// CWinApp's original method is involved in the update message handling!
// Removing this call will break things
BOOL bMore = CWinApp::OnIdle(lCount);
if (lCount == 0)
{
TRACE(_T("App idle for short period of time\n"));
bMore = TRUE;
}
// ... do work
return bMore;
// return TRUE as long as there are any more idle tasks
}
Here's a good article that kinda explains how to do it. Don't use his code example with WM_KICKIDLE though, instead scroll down to the comments section. There are two code samples that explain how to do it better. I quote:
//Override WM_INITMENUPOPUP
void CDialog::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
if(pPopupMenu &&
!bSysMenu)
{
CCmdUI CmdUI;
CmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for(UINT i = 0; i < CmdUI.m_nIndexMax; i++)
{
CmdUI.m_nIndex = i;
CmdUI.m_nID = pPopupMenu->GetMenuItemID(i);
CmdUI.m_pMenu = pPopupMenu;
// There are two options:
// Option 1. All handlers are in dialog
CmdUI.DoUpdate(this, FALSE);
// Option 2. There are handlers in dialog and controls
/*
CmdUI.DoUpdate( this, FALSE );
// If dialog handler doesn't change state route update
// request to child controls. The last DoUpdate will
// disable menu item with no handler
if( FALSE == CmdUI.m_bEnableChanged )
CmdUI.DoUpdate( m_pControl_1, FALSE );
...
if( FALSE == CmdUI.m_bEnableChanged )
CmdUI.DoUpdate( m_pControl_Last, TRUE );
*/
}
}
}
See if this helps - http://msdn.microsoft.com/en-us/library/essk9ab2(v=vs.80).aspx
i'm trying to simply fill a progress bar, full when a check box is checked, and empty when the box is unchecked. there is an ONCLICK action for the check box, so i figured i would check the value every time it was clicked, and would fill the bar only when it was checked.
this code includes a couple different things i tried, anything with progCtrl gave me a runtime error. any thoughts would be helpful, thanks!
void Cgui1Dlg::OnBnClickedsetkill()
{
// TODO: Add your control notification handler code here
//IDC_PROGRESS.Value = 100;
//CProgressCtrl progCtrl;
//progCtrl.SetDlgCtrlID(IDC_PROGRESS);
//UpdateData();
//if(changefill)
//{
//IDC_PROGRESS.PBM_SETPOS(100);
//SendMessage(IDC_PROGRESS, PBM_SETPOS, 100);
//progCtrl.SetPos(100);
//}
//else
//{
//filled = FALSE;
//}
UpdateData(FALSE);
}
I would create a control variable for the progress control and the check button. Then, do:
void Cgui1Dlg::OnBnClickedsetkill()
{
if(c_Check.GetCheck()==BST_CHECKED)
{
c_Progress.SetPos(100);
}
else
{
c_Progress.SetPos(0);
}
}