.net中怎么实现下拉框多选,然后获取选中的值

.net中怎么实现下拉框多选,然后获取选中的值,第1张

您可以选择使用CheckListBox控件。CheckListBox支持多选

由于不清楚您用什么语言,所以我写了VBnet 、C#net

vbnet Code

' Determine if there are any items checked

If CheckedListBox1CheckedItemsCount <> 0 Then

' If so, loop through all checked items and print results

Dim x As Integer

Dim s As String = ""

For x = 0 To CheckedListBox1CheckedItemsCount - 1

s = s & "Checked Item " & (x + 1)ToString & " = " & CheckedListBox1CheckedItems(x)ToString & ControlCharsCrLf

Next x

MessageBoxShow(s)

End If

C#net Code

// Determine if there are any items checked

if(checkedListBox1CheckedItemsCount != 0)

{

// If so, loop through all checked items and print results

string s = "";

for(int x = 0; x <= checkedListBox1CheckedItemsCount - 1 ; x++)

{

s = s + "Checked Item " + (x+1)ToString() + " = " + checkedListBox1CheckedItems[x]ToString() + "\n";

}

MessageBoxShow (s);

}

vbnet Code

Private Sub Button1_Click(ByVal sender As SystemObject, ByVal e As SystemEventArgs) Handles Button1Click

Dim selectstr As String = ""

For i As Integer = 0 To MeCheckedListBox1ItemsCount - 1

If MeCheckedListBox1GetItemChecked(i) Then

selectstr &= MeCheckedListBox1Items(i)ToString

End If

Next

MsgBox(selectstr)

End Sub

希望能帮到您。

//在checkedListBox1添加事件、label1为显示checkedListBox选中值的数量

//checkedListBox1CheckedItemsCount即checkedListBox选中值的数量

//这个事件是指示某项的选中状态将要被更改、在更改前会执行这个事件

//所以checkedListBox1CheckedItemsCount获取的是更改之前的值

//所以要在checkedListBox1CheckedItemsCount的上加减1;

//如果你勾一个值则加1

//反之减1

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)

{

if (eNewValue == CheckStateChecked)

label1Text = (checkedListBox1CheckedItemsCount + 1)ToString();

else label1Text = (checkedListBox1CheckedItemsCount - 1)ToString();

}

sql语句select 书名 from 书籍信息

界面

for(int i=0;i<dsTables[0]RowsCount;i++)

{

Checkedlistbox1ItemsAdd(dsTables[0]Rows[i]["书名"]ToString());

}

/// <summary>

/// 组装checkbox查询语句

/// </summary>

/// <param name="chklst_set_sql"></param>

private void ufn_chklst_where(ref string chklst_set_sql)

{

string[,] str_check_item = new string[chklst_ctrl_workstationItemsCount, 2];

//指定那一列不需要作为查询条件

ArrayList ary_check_item = new ArrayList();

ary_check_itemAdd("");

int int_cnt = 0;

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

{

string str_check = "N";

if (ary_check_itemContains((object)chklst_ctrl_workstationGetItemText(chklst_ctrl_workstationItems[i])))

{

int_cnt += 1;

}

else

{

if (chklst_ctrl_workstationCheckedItemsContains(chklst_ctrl_workstationItems[i]))

{

str_check = "Y";

}

str_check_item[i - int_cnt, 0] = str_check;

str_check_item[i - int_cnt, 1] = cls_stringpub_remove_str(chklst_ctrl_workstationGetItemText(chklst_ctrl_workstationItems[i]), "", "R");

}

}

//组装checklistBox的查询语句

if (str_check_itemLength > 0)

{

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

{

chklst_set_sql = cls_stringpub_sql_where(chklst_set_sql, str_check_item[i, 1], "=", str_check_item[i, 0], "");

}

}

}

//才写的,希望帮到你!有时间帮我看看这个问题>

不知道你的表示listview还是dategridview

这个逻辑不难理解吧

循环获取box1中的选择项,对每项循环 添加 box2的选择内容

(当然是根据box2的选择值查询表2得到的详细值)

然后每次获得一行完整的表3项,就添加到表3

下面以listview 为例

            listView3ItemsClear();

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

            {//获得表1的选择值

                for (int j = 0; j < checkedListBox2CheckedItemsCount; j++)

                {//获得表2的选择值

                

                    //新的表3的行

                    ListViewItem lvi = new ListViewItem();

                    lviText = checkedListBox1CheckedItems[i]ToString();

                    for (int k = 0; k < listView2ItemsCount; k++)

                    {//获得完整的表2 的值,并添加到ListViewItem

                        if (listView2Items[k]Text == lviText)

                        {

                            lviSubItemsAdd(listView2Items[k]SubItems[0]Text);

                            lviSubItemsAdd(listView2Items[k]SubItems[1]Text);

                            lviSubItemsAdd(listView2Items[k]SubItems[2]Text);

                            listView3ItemsAdd(lvi);

                            break;//这里是跳出 int k = 0 这个循环

                        }

                    }

                }

            }

以上就是关于.net中怎么实现下拉框多选,然后获取选中的值全部的内容,包括:.net中怎么实现下拉框多选,然后获取选中的值、C#程序checkedListBox选中值的数量如何显示。checkedListBox选中一个方块就显示1 选中3个就显示3、C#窗体设计中,如何用Checkedlistbox列出数据库表中的某个值。急急急~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存