MFC 列表控件修改实例(源码下载)

1、本程序基于前期我的博客文章《MFC下拉菜单打钩图标存取实例(源码下载)》

2、程序功能选中列表控件某一项,修改这一项的按钮由禁止变为可用,双击这个按钮弹出对话框可对这一项的记录数据进行修改,点击确定保存修改数据。

3、首先在主界面添加一个修改参数按钮。

4、在myDlg.h 文件

class CMyDlg : public CDialog中添加代码

cpp 复制代码
public:
void UpdateButton(void);
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);

5、选中列表控件,右击选择"事件...",在弹出的事件句柄中选择"LVN_ITEMCHANGED",如下图所示。

cpp 复制代码
void CMyDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	POSITION pos = m_ctrlType.GetFirstSelectedItemPosition();
	if(pos != NULL)
	{
		int nItem = m_ctrlType.GetNextSelectedItem(pos);
		if(m_nTypeIndex != nItem)
		{
			m_nTypeIndex = nItem;
		}
	}
	else
		m_nTypeIndex = -1;	
	*pResult = 0;
}

6、在myDlg.cpp 文件中添加代码

cpp 复制代码
LRESULT CMyDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	static int nTypeIndex = -2;
	if(::IsWindow(m_ctrlType.m_hWnd))
	{
		if(m_nTypeIndex != nTypeIndex)
		{
			nTypeIndex = m_nTypeIndex;
			UpdateButton();
		}
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CMyDlg::UpdateButton()
{
	GetDlgItem(IDC_BUTTON3)->EnableWindow(m_nTypeIndex > -1);
}

7、在myDlg.cpp 文件中参数按钮中添加代码

cpp 复制代码
void CMyDlg::OnButton3() 
{
	CTypeParaDlg dlg;
	dlg.m_pPara = &theApp.m_allPara[m_nTypeIndex];

	if(dlg.DoModal() == IDOK)
	{

    	LV_ITEM item;
		item.mask = LVIF_TEXT|LVIF_IMAGE;
		item.iItem = m_nTypeIndex;
		item.iSubItem = 0;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strTypeName;
		item.iImage = 0;
		m_ctrlType.SetItem(&item);
		item.iSubItem = 1;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strBrand;
		m_ctrlType.SetItem(&item);
		item.iSubItem = 2;
		item.pszText = theApp.m_allPara[m_nTypeIndex].m_strRemark;
		m_ctrlType.SetItem(&item);
	}	
}

运行程序

源码下载

相关推荐
我想进大厂2 分钟前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
光而不耀@lgy8 分钟前
C++初登门槛
linux·开发语言·网络·c++·后端
啊丢_19 分钟前
C++——Lambda表达式
开发语言·c++
Wendy_robot1 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
转基因2 小时前
Codeforces Round 1020 (Div. 3)(题解ABCDEF)
数据结构·c++·算法
whoarethenext2 小时前
c++的jsoncpp使用
开发语言·c++·jsoncpp
我想进大厂3 小时前
图论---Kruskal(稀疏图)
数据结构·c++·算法·图论
_w_z_j_4 小时前
C++----模拟实现string
开发语言·c++
特立独行的猫a4 小时前
redis客户端库redis++在嵌入式Linux下的交叉编译及使用
linux·数据库·c++·redis·redis客户端库
张槊哲4 小时前
const(C++)
开发语言·c++