打开对话框,选择文本,判断是否有重复的

本文实现的功能对话框为:

 

        

                          图  1                                                                                      图 2

功能:通过点击“添加按钮”来打开选择文件对话框,如图2,选择我们要用的图纸,这里我们默认只能选择.dwg文件,然后点击“打开”,就会将该图纸的路劲保存在图1中的listbox控件中,这里就需要我们判断选择加入的图纸是否已经加入到listbox空间中了。下面就是我的实现代码:

void CMyCombineDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog dlg(true, _T(".dwg"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("All Files(*.dwg)|*.dwg||"), AfxGetMainWnd());
//默认打开的时候显示D盘
dlg.m_ofn.lpstrInitialDir = _T("D:\\");
if (dlg.DoModal() == IDOK)
{
//获取文件路径
CString pathName = dlg.GetPathName();
//获取文件名
//CString fileName = dlg.GetFileName();

//使用FindString()函数来在listbox空间中查找字符串pathName,

//其中第一个参数为查找的起始位置,第二个参数为要查找的字符串,返回值为该字符串在listbox控件中的行数
int str = m_list.FindString(0, pathName);

//返回值在listbox的行数范围内,则说明已经添加了该图纸
if (str >=0 && str <=m_list.GetCount())
{
MessageBox(_T("该图纸已经选择了!"));
return;
}
else
{
//将选择的文件路径保存到list空间中
m_list.AddString(pathName);
}
}
}

 

///////////////////////////////////////////////////////////////////////////////////////

2.删除listbox中选中的行。

鼠标选择listbox中要删除的那一行,然后点击“移除图纸”,则可以从listbox控件从移除该行,实现代码:

void CMyCombineDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
//获取选择项信息,通过GetSelCount()函数获得用户所选择的行数

int nItemcount = m_list.GetSelCount();

//使用memset来申请一段内存,用来保存条目索引
int *indexBuf = new int[nItemcount];
memset(indexBuf, 0, nItemcount * sizeof(int));
//存储选中的条目索引
m_list.GetSelItems(nItemcount, indexBuf);

for (int loop = nItemcount; loop >= 0; loop--)
{
m_list.DeleteString(indexBuf[loop]);
}
delete[] indexBuf;
}

posted on 2015-09-14 15:41  !!-阳光-!!  阅读(513)  评论(0编辑  收藏  举报

导航