📄 materieldlg.cpp
字号:
//清空list控件的数据
for(int delcolumn=100;delcolumn>=0;delcolumn--)
m_oListmateriel.DeleteColumn(delcolumn);
//设置list对话框的列
DWORD dwStyle;
RECT rect;
LV_COLUMN lvc;
dwStyle = m_oListmateriel.GetStyle();
dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
m_oListmateriel.SetExtendedStyle(dwStyle);
m_oListmateriel.GetClientRect(&rect);
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
lvc.fmt=LVCFMT_LEFT;
lvc.iSubItem = 0;
lvc.pszText = _T("类别");
lvc.cx = 80;
m_oListmateriel.InsertColumn(1,&lvc);
lvc.iSubItem = 1;
lvc.pszText = _T("名称");
lvc.cx = 150;
m_oListmateriel.InsertColumn(2,&lvc);
lvc.iSubItem = 2;
lvc.pszText = _T("价格");
lvc.cx = 65;
m_oListmateriel.InsertColumn(3,&lvc);
//Read the 1st class's materiels to list.
// CString sql="Select * from MATERIEL where CLASSID=1";
// ReadtoList(sql);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMaterielDlg::OnButtonMadd()
{
m_oMname.SetWindowText("");
m_oMprice.SetWindowText("");
m_oMshopcode.SetWindowText("");
m_oMbarcode.SetWindowText("");
lNewID=GenNewID();
}
void CMaterielDlg::OnButtonMdel()
{
CString sql, stemp,ssclassid;
_RecordsetPtr m_pRecordset;
long lclassid;
m_oMname.GetWindowText(stemp);//得到选中项的文本
if(stemp=="")
return;
sql="select * from MATERIEL where NAME='"+stemp+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
lclassid=(long)(m_pRecordset->GetCollect("CLASSID"));
if(AfxMessageBox("确定删除此商品吗?",MB_YESNO)==IDYES)
{
m_pRecordset->Delete(adAffectCurrent);///删除当前记录
}
else
return;
m_pRecordset->Update();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("删除商品出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
ssclassid.Format("%d",lclassid);
CString sql1="Select * from MATERIEL Where CLASSID="+ssclassid+"";
ReadtoList(sql1);
}
void CMaterielDlg::ReadtoList(CString sql)
{
_RecordsetPtr m_pRecordset; //Must define it in function!!!!
//删除所有list中的数据。
m_oListmateriel.DeleteAllItems();
int numline=0;
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
LV_ITEM lvitem;
lvitem.pszText="";
lvitem.mask=LVIF_TEXT;
lvitem.iSubItem=0;
while(!m_pRecordset->adoEOF)
{
lvitem.iItem=numline;
m_oListmateriel.InsertItem(&lvitem);
//读出数据写入到list中
// m_oListmateriel.SetItemText(numline,0,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("CLASSID"));
m_oListmateriel.SetItemText(numline,0,curtext);
m_oListmateriel.SetItemText(numline,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
float fprice=(float)m_pRecordset->GetCollect("PRICE");
CString sprice;
sprice.Format("%.2f",fprice);
m_oListmateriel.SetItemText(numline,2,sprice);
numline++;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取商品到列表出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
//设置标题
CString temp;
temp.Format("共有%d条记录",numline);
SetWindowText(temp);
}
void CMaterielDlg::OnSelchangedTreeMclass(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CString sql,sql1;
long lclassid;
_RecordsetPtr m_pRecordset; //Must define it in function!!!
HTREEITEM hCurItem;
//Clear text in edit.
m_oMname.SetWindowText("");
m_oMprice.SetWindowText("");
m_oMshopcode.SetWindowText("");
m_oMbarcode.SetWindowText("");
lNewID=0;//Refresh lNewID's value.
hCurItem=m_oMclass.GetSelectedItem ();//Get Current item and its sub items.
curtext=m_oMclass.GetItemText(hCurItem);//Read current item text into edit control.
sql="SELECT * FROM MATERIELCLASS where NAME='"+curtext+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
lclassid=(long)(m_pRecordset->GetCollect("ID"));
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("获取类别名称出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
sclassid.Format("%d",lclassid);
sql1="Select * from MATERIEL where CLASSID="+sclassid+"";
ReadtoList(sql1);
UpdateData(false);
*pResult = 0;
}
void CMaterielDlg::OnClickListMateriel(NMHDR* pNMHDR, LRESULT* pResult)
{
CString smname,sshopcode,sbarcode,sprice;
CString sql;
float fprice;
int ndiscount,nsupply;
_RecordsetPtr m_pRecordset; //Must define it in function!!!
m_oMname.SetWindowText("");
m_oMprice.SetWindowText("");
m_oMshopcode.SetWindowText("");
m_oMbarcode.SetWindowText("");
//得到当前选中的行
POSITION pos = m_oListmateriel.GetFirstSelectedItemPosition();
//如果选中一行
if(pos)
{
int nItem = m_oListmateriel.GetNextSelectedItem(pos);
smname=m_oListmateriel.GetItemText(nItem,1);
m_oMname.SetWindowText(smname);
lNewID=0;//Refresh lNewID's value.
sql="Select * from MATERIEL where NAME='"+smname+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
lMID=(long)(m_pRecordset->GetCollect("ID"));
fprice=(float)(m_pRecordset->GetCollect("PRICE"));
ndiscount=(long)m_pRecordset->GetCollect("DISCOUNT");
nsupply=(long)m_pRecordset->GetCollect("SUPPLY");
sshopcode=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SHOPCODE");
sbarcode=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BARCODE");
sprice.Format("%.2f",fprice);
m_oMprice.SetWindowText(sprice);
m_oMshopcode.SetWindowText(sshopcode);
m_oMbarcode.SetWindowText(sbarcode);
m_oMdiscount.SetCurSel(ndiscount);
m_oSupply.SetCheck(nsupply);
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取商品资料到控件出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
*pResult = 0;
}
BOOL CMaterielDlg::bRepeat(CString strFieldValue)
{
CString sql;
BOOL br;
_RecordsetPtr m_pRecordset; //Must define it in function!!!!
sql="SELECT * FROM MATERIEL WHERE NAME='";
sql=sql+strFieldValue+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(m_pRecordset->adoEOF)
br=FALSE;
if(!m_pRecordset->adoEOF)
br=TRUE;
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString stemp;
stemp.Format("查找商品名称出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return br;
}
void CMaterielDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
m_oMclass.DeleteAllItems();
AddTree();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -