Combo box and list box
combobox and com
demo
Based on the dialog box, the following are all default settings
Go to the next step and finish
Modify the id of both
All combo boxes and list boxes have an index, which starts from 0
List box control
comes with sorting
As long as it is greater than -1, there is currently selected
Add click events to two buttons
增加按钮
void CdemoDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString strText;
GetDlgItemText(IDC_EDT_TEXT,strText);
m_cmb_demo.AddString(strText);
m_cmb_demo.SetCurSel(m_cmb_demo.GetCount()-1);//因为从0开始,所以最后一个是count-1,当前选中的是最后一个,即总个数减一
//添加后,默认是最后一个
m_lst_demo.AddString(strText);
m_lst_demo.SetCurSel(m_lst_demo.GetCount()-1);
}
删除按钮
void CdemoDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex;
//组合框
nIndex = m_cmb_demo.GetCurSel();//得到索引 //每一项都会有一个索引
if(nIndex>-1)//说明有选中的项,下标从0开始,没有就是-1;
{
m_cmb_demo.DeleteString(nIndex);
if(nIndex<m_cmb_demo.GetCount())
m_cmb_demo.SetCurSel(nIndex);//设当前选中的为下一项,删除第3项,则下一项又变成了第三项
else
m_cmb_demo.SetCurSel(0); //如果所删的元素后面没有了,那就选中设为第一项
}
//列表框
nIndex = m_lst_demo.GetCurSel(); //每一项都会有一个索引
if(nIndex>-1)//说明有选中的项,下标从0开始,没有就是-1;
{
m_lst_demo.DeleteString(nIndex);
if(nIndex<m_lst_demo.GetCount())
m_lst_demo.SetCurSel(nIndex);//设当前选中的为下一项,删除第3项,则下一项又变成了第三项
else
m_lst_demo.SetCurSel(0); //如果所删的元素后面没有了,那就选中设为第一项
}
//删的时候,两边都选
}
Prevent carriage return to exit
void CdemoDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
//CDialogEx::OnOK();
}
Simple use of Listbox control in MFC
http://blog.sina.com.cn/s/blog_6ca43ae701019h8k.html
http://blog.csdn.net/winerdaxian/article/details/6560060
http://www.jizhuomi.com/software/186.html
https://my.oschina.net/ranjiewen/blog/781090
https://blog.csdn.net/chengke1866/article/details/100695768?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242
The listbox control in MFC is to display a series of text, each text occupies a line.
Listbox control can set properties as:
LBS_CHILD: (default) child window
LBS_Visible: (default) visible
LBS_Multiplesel: Multiple lines can be selected
LBS_Extendedsel: You can use shift or ctrl to select multiple rows
LBS_SORT: All rows are sorted alphabetically
Operate the Listbox:
Int listbox.AddString (LPCTSTR Str): Add a str text line to the first line of the listbox, that is, at this time nIndex is 0;
Int listbox.DeleteString (uint nIndex): delete the text line of the nIndex line in the listbox, note that nIndex starts from 0;
int listbox.InsertString (uint xIndex, LPCTSTR Str): insert a text line of str in the nIndex line of the listbox;
Int listbox.GetCount (): Get the total number of rows in the listbox;
Int listbox.GetCurSel (): Get the number of lines of text selected by listbox.
Void listbox.ResetContent (): Clear all data in the listbox;
Int listbox.GetCurCount (): Get the number of selected rows in the listbox;
Int listbox.FindString (int nstart, LPCTSTR Str ): start the search from the nstart line to find the text line of Str;
Int listbox.SelectString (int nstart, LPCTSTR Str ): Starting from the nstart line, select the line containing the Str string;
To double-click a row in the Listbox, the steps to generate a response function:
> In BEGIN_MESSAGE_MAP () END_MESSAGE_MAP added between the response function () message mapping as a function named C Dlg :: OnLbnDblClk, add the following code is
ON_LBN_DBLCLK (IDC_LIST1, C & ID Dlg :: OnLbnDblClk) // IDC_LIST1 listbox control is
then C Add Dlg.h in function OnLbnDblClk () statement last in C () Add code to implement the function Dlg :: OnLbnDblClk
Just follow the default and
then double-click, it will locate the place to write the code
c++mfclistbox double click event
void CdemoDlg::OnLbnSelchangeListDemo()
{
//对每一行添加事件的方法
// 列表控件可以返回第几行,然后你可以通过行获得行内容。直接点击列表控件可添加点击事件。
//CString strText1;
//GetDlgItemText(IDC_LIST_DEMO,strText1);
MessageBox(_T("点击弹出一个框"));//弹出一个框
// MessageBox()
//列表框
//int nIndex1;
// nIndex1 = m_lst_demo.GetCurSel(); //每一项都会有一个索引
//m_lst_demo.DeleteString(nIndex1);
/*
if(nIndex1>-1)//说明有选中的项,下标从0开始,没有就是-1;
{
m_lst_demo.DeleteString(nIndex1);
if(nIndex1<m_lst_demo.GetCount())
m_lst_demo.SetCurSel(nIndex1);//设当前选中的为下一项,删除第3项,则下一项又变成了第三项
else
m_lst_demo.SetCurSel(0); //如果所删的元素后面没有了,那就选中设为第一项
}
*/
}