列表框控件中要显示和选择数据项,要设置哪些属性

列表框控件中要显示和选择数据项,要设置哪些属性,第1张

为了使用列表控件,首先需要向列表框控件中添加数据。在MFC类库中,列表框控件被封装为CListBox类。CListBox类提供了AddString方法e5a48de588b67a686964616f31333262343161向列表框中添加数据,该方法语法如下:

int AddString(LPCTSTR lpszItem);

参数说明如下。

lpszItem:表示向列表中添加的字符串数据。

返回值:表示新添加的数据在列表框中的索引位置。索引从零开始。

例如,下面的语句利用AddString方法向列表中添加数据。

m_DataListAddString("历史");

m_DataListAddString("地理");

m_DataListAddString("政治");

m_DataListAddString("生物");

m_DataListAddString("体育");

此外,还可以使用InsertString方法向列表框中添加数据,该方法语法如下:

int InsertString(int nIndex, LPCTSTR lpszItem);

参数说明如下。

þ nIndex:表示列表中的一个索引位置,方法将在该位置处添加数据,如果该参数为-1,则在列表框中数据的末尾位置添加。

þ lpszItem:表示向列表中添加的字符串数据。

返回值:表示新添加的数据在列表框中的索引位置。索引从零开始。

例如,下面的语句应用InsertString方法向列表框中添加数据。

m_DataListInsertString(0, "历史");

m_DataListInsertString(1, "地理");

m_DataListInsertString(2, "政治");

m_DataListInsertString(3, "生物");

m_DataListInsertString(4, "体育");

向列表框中添加数据

772 为列表框中的项目添加复选功能

在使用列表框控件时,有时需要利用列表框进行多项选择。这就需要列表框中的选项具有类似复选框的功能。那么如何让列表框中的数据以复选框的形式显示呢?可以按如下的方法实现。

例73 为列表框中的项目添加复选功能。

(1)创建一个基于对话框的工程,向对话框中添加列表框和按钮控件,如图749所示。

(2)利用类向导为列表框命名为m_DataList,类型为CListBox,如图750所示。

图749 对话框资源设计

图750 为列表框控件关联名称

(3)在对话框的头文件中将m_DataList的类型CListBox修改为CCheckListBox。

CCheckListBox m_DataList;

CCheckListBox派生于CListBox,它为数据项提供了复选功能。

(4)在列表框的属性窗口中设置Has strings属性,并设置Fixed自绘风格,如图751所示。

图751 设置列表框属性

(5)在对话框初始化时向列表框中添加数据。

m_DataListInsertString(0, "历史");

m_DataListInsertString(1, "地理");

m_DataListInsertString(2, "政治");

m_DataListInsertString(3, "生物");

m_DataListInsertString(4, "体育");

(6)处理“确定”按钮的单击事件,统计用户选择的学科信息。

void CCheckListDlg::OnConfirm()

{

int nItemCount = m_DataListGetCount(); //获取项目数量

CString szContent, szItemData; //定义字符串变量

szContent = "";

for (int i=0; i<nItemCount; i++) //遍历列表项

{

if (m_DataListGetCheck(i) == BST_CHECKED) //判断列表项是否被选中

{

m_DataListGetText(i, szItemData); //获取列表项文本

szContent += szItemData + "\r\n"; //记录列表项文本

}

}

if (!szContentIsEmpty())

{

MessageBox(szContent, "您选择的学科"); //显示用户选择的信息

}

}

(7)运行程序,效果如图752所示。

图752 统计列表框信息

773 同时选择多个项目

默认情况下,列表框控件中的项目只能同时选择一项,但在实际开发中,有时需要选择多项数据。为此,需要设置列表框的Selection属性为Multiple,如图753所示。

图753 列表框属性设置

这样,在列表框中即可选择多项数据,如图754所示。

图754 多项选择数据

你的gruopBox1里还有其他的控件,比如Button,所以你直接这样写是不行的,需要加一个判断,把foreach那段修改成下面吧:

foreach (Control control in groupBox1Controls) //这里是Control,不是CheckedListBox

            {

                if (control is CheckedListBox) //这里加一个判断,控件是否是CheckedListBox

                {

                    arrChkInfoAdd(controlSelectedValue);

                }

            }

SystemFormsListBox 的一些相关属性

SelectedIndex 已重写。获取或设置 ListBox 中当前选定项的从零开始的索引。

SelectedIndices 获取一个集合,该集合包含 ListBox 中所有当前选定项的从零开始的索引。

SelectedItem 获取或设置 ListBox 中的当前选定项。

SelectedItems 获取包含 ListBox 中当前选定项的集合。

SelectedValue 获取或设置由 ValueMember 属性指定的成员属性的值。

-------------------------------------------------------------

也可这样调用:(设a是ListBox对象)

aItems[SelectedIndex]ToString()

aSelectedItemToString()

用下面的方法就可以,楼上说的是CheckBoxList WebForm里的

checkedListBox1GetItemChecked(i);//判断第i+1个项是否被选中

//将没有被选中的项显示出来

for (int i = 0; i < checkedListBox1ItemsCount; i++)

{

if (!checkedListBox1GetItemChecked(i))

{

MessageBoxShow(checkedListBox1Items[i]ToString());

}

}

前台:

<div>

<asp:CheckBoxList ID="CheckBoxList1" runat="server">

</asp:CheckBoxList>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</div>

后台:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindCKBoxList();

}

}

static SqlConnection conn = new SqlConnection(@"server=;database=;uid=;pwd=;");

private void BindCKBoxList()

{

SqlDataAdapter dapt = new SqlDataAdapter(@"select from item",conn);

DataTable dt = new DataTable();

daptFill(dt);

CheckBoxList1DataTextField = "name";//项目名称

CheckBoxList1DataValueField = "id";//项目编号

CheckBoxList1DataSource = dt;

CheckBoxList1DataBind();

}

protected void Button1_Click(object sender, EventArgs e)

{

SqlCommand comm=new SqlCommand();

commConnection = conn;

SqlParameter[] sp={new SqlParameter("@itemid",SqlDbTypeInt),

new SqlParameter("@staffid",SqlDbTypeVarChar)};

connOpen();

for (int i=0;i<CheckBoxList1ItemsCount;i++)//遍历CheckBoxList

{

if (CheckBoxList1Items[i]Selected)

{

commParametersClear();

commCommandText=@"insert into staff values(@itemid,@staffid)";

sp[0]Value=CheckBoxList1Items[i]ValueToString();

sp[1]Value=TextBox1TextTrim();

commParametersAddRange(sp);

commExecuteNonQuery();

}

}

connClose();

}

以上就是关于列表框控件中要显示和选择数据项,要设置哪些属性全部的内容,包括:列表框控件中要显示和选择数据项,要设置哪些属性、c# checklistbox多选、c#中19. ListBox控件如何得到选中项索引或内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9781012.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存