ListBox 问题 windows Forms

harisonh2l 2004-12-03 01:39:03
我现在将ListBox设置为可多选的,那怎样获取所有多选中的每一项的SelectedValue呢?
...全文
219 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiroki513 2005-03-22
  • 打赏
  • 举报
回复
同意用kuya(无知) 的方法
for (int m = 0; m < lst.SelectedItems.Count; m ++)
{
DataRowView drv = (DataRowView)lst.SelectedItems[m];
Name = drv["name"].ToString();//Text
id = drv["id"].ToString();//你要的Value
}
Robinsonzhan 2004-12-07
  • 打赏
  • 举报
回复
UP
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
怎样转?
Dronven 2004-12-03
  • 打赏
  • 举报
回复
取出ListBox中的DataSource转成DataTable,应该就可以实现了
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
下面是核心代码:
ArrayList selectArrayList = new ArrayList();
foreach(int index in this.listBox1.SelectedIndices)
{
selectArrayList.Add(index);
}

this.listBox1.SelectionMode = SelectionMode.One;
this.listBox1.ClearSelected();

int[] values = new int[10];

for(int i=0;i<selectArrayList.Count;i++)
{
this.listBox1.SetSelected((int)selectArrayList[i],true);
values[i] = (int)this.listBox1.SelectedValue;
this.listBox1.SetSelected((int)selectArrayList[i],false);
}

this.Infor.Text = "Begin: ";
for(int i=0;i<values.Length;i++)
{
this.Infor.Text = this.Infor.Text + values[i].ToString() + ",";
}

this.listBox1.SelectionMode = SelectionMode.MultiExtended;
this.listBox1.ClearSelected();
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
好,谢谢大家,我总结出来的,测试成功!!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace ListBoxPra
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label Infor;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

private DataTable dataTable;
private DataView dataView;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.Infor = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"});
this.listBox1.Location = new System.Drawing.Point(184, 48);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(160, 199);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 376);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "OK";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Infor
//
this.Infor.Location = new System.Drawing.Point(24, 272);
this.Infor.Name = "Infor";
this.Infor.Size = new System.Drawing.Size(472, 64);
this.Infor.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 446);
this.Controls.Add(this.Infor);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
ArrayList selectArrayList = new ArrayList();
foreach(int index in this.listBox1.SelectedIndices)
{
selectArrayList.Add(index);
}

this.listBox1.SelectionMode = SelectionMode.One;
this.listBox1.ClearSelected();

int[] values = new int[10];

for(int i=0;i<selectArrayList.Count;i++)
{
this.listBox1.SetSelected((int)selectArrayList[i],true);
values[i] = (int)this.listBox1.SelectedValue;
this.listBox1.SetSelected((int)selectArrayList[i],false);
}

this.Infor.Text = "Begin: ";
for(int i=0;i<values.Length;i++)
{
this.Infor.Text = this.Infor.Text + values[i].ToString() + ",";
}

this.listBox1.SelectionMode = SelectionMode.MultiExtended;
this.listBox1.ClearSelected();
}

private void Form1_Load(object sender, System.EventArgs e)
{
Init();
}

private void Init()
{
this.InitDataTable();
this.InitData();
this.InitDataView();
this.BindData();

}

private void InitDataTable()
{
dataTable = new DataTable();
DataColumn c1 = new DataColumn();
c1.DataType = System.Type.GetType("System.Int32");
c1.ColumnName = "id";
this.dataTable.Columns.Add(c1);
DataColumn c2 = new DataColumn();
c2.DataType = System.Type.GetType("System.String");
c2.ColumnName = "name";
this.dataTable.Columns.Add(c2);
}

private void InitData()
{
for(int i=0;i<10;i++)
{
DataRow dataRow = this.dataTable.NewRow();
dataRow[0] = i;
dataRow[1] = "行" + i.ToString();
this.dataTable.Rows.Add(dataRow);
}
}

private void InitDataView()
{
this.dataView = new DataView(this.dataTable,"","",DataViewRowState.CurrentRows);
}

private void BindData()
{
this.listBox1.DisplayMember = "name";
this.listBox1.ValueMember = "id";
this.listBox1.DataSource = this.dataView;
}
}

}
suxiao 2004-12-03
  • 打赏
  • 举报
回复
up!@
亚非 2004-12-03
  • 打赏
  • 举报
回复
this.listBox1.DataSource ;
this.listBox1.DisplayMember ;
this.listBox1.ValueMember;
这三个属性你是怎么赋值的,DataSource是dataview,那你的ValueMember是什么呢,
我做过的,我的方法可以的。
我的MSN:kuya_008@hotmail.com
joky1981 2004-12-03
  • 打赏
  • 举报
回复
你用一个listBox2显示出所有选中得Value
listBox2.DataSource = selValues;结果就出来了^_^
joky1981 2004-12-03
  • 打赏
  • 举报
回复
我这个应该可以把,我测试用得数据源是ArrayList,方法是一样得
joky1981 2004-12-03
  • 打赏
  • 举报
回复
ArrayList al = new ArrayList();
for(int i=0;i<listBox1.SelectedItems.Count;i++)
{
al.Add(listBox1.SelectedIndices[i]);
}
ArrayList selValues = new ArrayList();

listBox1.SelectionMode = SelectionMode.One;
for(int i=0;i<listBox1.SelectedItems.Count;i++)
{
listBox1.SetSelected(Convert.ToInt32(al[i]),true);
selValues.Add(listBox1.SelectedValue.ToString());
listBox1.SetSelected(Convert.ToInt32(al[i]),false);
}
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
都不行
Robinsonzhan 2004-12-03
  • 打赏
  • 举报
回复
ListBox.SelectedIndexCollection indices = listBox1.SelectedIndices;
int selected = indices.Count; //这是总共的选中数量
if(indices.Count >0)
{
for(int n = selected -1;n >= 0;n--)
{
int index = indices[n];//这是遍历的下标
textBox1.Text += listBox1.SelectedItems[n].ToString();
//listBox1.Items.RemoveAt(index);
//自行处理
}
}
mur 2004-12-03
  • 打赏
  • 举报
回复
我用的是TreeView实现的这个功能。
string fields = "";
foreach(TreeNode n in this.treeView1.Nodes)
{
if(n.Checked)
{
if(fields.Length>0)
fields += ","+n.Text;
else
fields += n.Text;
}
}
return fields;
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
dataview
亚非 2004-12-03
  • 打赏
  • 举报
回复
你的listbox.datasource 是什么类型的
harisonh2l 2004-12-03
  • 打赏
  • 举报
回复
不行啊
亚非 2004-12-03
  • 打赏
  • 举报
回复
YourType item = (YourType)this.listBox1.SelectedItems[i];
item.SecondValue 这就是你要的selectvalue
hanbinghai 2004-12-03
  • 打赏
  • 举报
回复
for(int i = 0;i < this.listBox1.SelectedItems.Count;i++)
{
System.Diagnostics.Debug.WriteLine(this.listBox1.SelectedItems[i].ToString());
}
jxzhang615 2004-12-03
  • 打赏
  • 举报
回复
帮顶!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧