如果是mfc,我们可以将结构插入列表控件吗? [英] Can we insert structure into list control if mfc
本文介绍了如果是mfc,我们可以将结构插入列表控件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有学生结构,我想将它存储在列表控件中我们该怎么做?
参见编码lstStudent是列表对象
我尝试过:
I have student structure and I wanted to store it in list control how can we do this?
see in coding lstStudent is the list object
What I have tried:
struct Student
{
char sname[25];
char id[5];
char sclass[10];
};
void CStudent::SaveData()
{
Student s1;
CString sname,id,sclass,m;
m_txtStudentName.GetWindowTextW(sname);
m_txtStudentName.GetWindowTextW(id);
m_txtClass.GetWindowTextW(sclass);
sprintf(s1.sname,"%S",sname);
sprintf(s1.id,"%S",id);
sprintf(s1.sclass,"%S",sclass);
m_lstStudent.add(s1);
}
}
推荐答案
你可能意味着 CListView类 [ ^ ],但您必须从每个列的结构中手动添加项目。或者您可以切换到提供数据绑定的C#。
You probably mean CListView Class[^], although you have to add items manually from the structure for each column. Or you could switch to C# which provides data binding.
您必须将相应的列添加到列表控件中,该列必须处于报告模式(LVS_REPORT
样式,通常在资源文件中定义)。在父窗口的初始化函数中执行此操作(例如OnInitDialog
,带对话框窗口):
You have to add corresponding columns to your list control which must be in report mode (LVS_REPORT
style, usually defined in the resource file). Do this within the initialisation function of the parent window (e.g.OnInitDialog
with dialog windows):
m_lstStudent.InsertColumn(0, _T("Name"));
m_lstStudent.InsertColumn(1, _T("ID"));
m_lstStudent.InsertColumn(2, _T("Class"));
然后在添加新项目后设置列数据:
Then set the column data after adding a new item:
// Insert new item and set text for first column
int pos = m_lstStudent.InsertItem(0, sname);
// Set text for other columns
m_lstStudent.SetItemText(pos, 1, id);
m_lstStudent.SetItemText(pos, 2, sclass);
这篇关于如果是mfc,我们可以将结构插入列表控件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文