不太明白ListBox中的Items.Insert()

csdn_wayne 2002-11-08 01:20:28
1、首先我不太明白ListBox.Items.Insert(A,B); 这里的A代表的是什么,我知道B是显示在列表框中的内容。我在用的时候,直接把A设为0,但我用lbCustomerList.SelectedIndex.ToString();得到的是一个从0开始的索引,但如果把A改成1就会出错,不知道这个A在这里是什么作用?

2、然后是我有一个表,两个字段都是字符型的(Z1,Z2),我想在列表框中显示Z2的内容,但我需要在选择列表框中的一项后,得到Z1的值作为参数。不知道怎么做比较好?
...全文
792 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2002-11-08
  • 打赏
  • 举报
回复
use sql statement like

select LastName + ' ' + FirstName as Name, Number from your table

then
lbName.DataSource = rdrEmployees;
lbName.DisplayMember = "Name";
lbName.ValueMember = "Number ;

saucer 2002-11-08
  • 打赏
  • 举报
回复
1. the first argument is an index where you want to put you object, you cannot use 1 at the beginning, unless you have at least one item in the listbox already

2. set "z2" as the DisplayMember, set "z1" as the ValueMember

ListBox1.DataSource = .... ;
ListBox1.DisplayMember = "Z2" ;
ListBox1.ValueMember = "Z1" ;


use ListBox1.SelectedValue to get the value for "Z1"
csdn_wayne 2002-11-08
  • 打赏
  • 举报
回复
up
csdn_wayne 2002-11-08
  • 打赏
  • 举报
回复
谢谢,第一个问题我已经明白了,但第二个问题还不是很明白。

我本来是这么做的:
private void btnListName_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlDataReader rdrEmployees;

this.connNwind.Open();
rdrEmployees = this.cmdEmployees.ExecuteReader();
while(rdrEmployees.Read())
{
string strName;
strName = rdrEmployees["LastName"] + " " + rdrEmployees["FirstName"];
lbName.Items.Insert(0,strName);
}

rdrEmployees.Close();
this.connNwind.Close();
}

也就是说,我显示在列表框的内容是一个人的全名,两个字段组合到一起的,但每个人都会有一个编号,Number,我需要实现在列表框中显示人名,但当选择到某条记录时,得到的是那个人的编号(编辑4位数但没有连续)。能给我一个比较详细的代码吗?最好能以这个相类似的方法来实现。
QQ好友例表控件 带实例和源码 //1. 属性列表: // SelectionMode 组件条目的选择类型,即多选(Multiple)、单选(Single) // Rows 列表框显示总共多少行 // Selected 检测条目是否被选 // SelectedItem 返回的类型是ListItem,获得列表框被选择的条目 // Count 列表框条目的总数 // SelectedIndex 列表框被选择项的索引值 // Items 泛指列表框的所有项,每一项的类型都是ListItem //2. 取列表框被选的值 // ListBox.SelectedValue //3. 动态的添加列表框的项: // ListBox.Items.Add("所要添加的项"); //4. 移出指定项: // //首先判断列表框的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////移出选择的项 //ListBox.Items.Remove(ListBox.SelectedItem); // } //5. 清空所有项: // //首先判断列表框的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////清空所有项 //ListBox.Items.Clear(); // } //6. 列表框可以一次选择多项: // 只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选 //7. 两个列表框联动,即两级联动菜单 // //判断第一个列表框被选的值 // switch(ListBox1.SelectValue) // { ////如果是"A",第二个列表框就添加这些: //case "A" // ListBox2.Items.Clear(); // ListBox2.Items.Add("A1"); // ListBox2.Items.Add("A2"); // ListBox2.Items.Add("A3"); ////如果是"B",第二个列表框就添加这些: //case "B" // ListBox2.Items.Clear(); // ListBox2.Items.Add("B1"); // ListBox2.Items.Add("B2"); // ListBox2.Items.Add("B3"); // } //8. 实现列表框项的移位 // 即:向上移位、向下移位 // 具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象。 // 如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后 // 把刚才新加入的对象的值,再附给当前选定项的前一项。 // 具体代码为: // //定义一个变量,作移位用 // index = -1; // //将当前条目的文本以及值都保存到一个临时变量里面 // ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue); // //被选的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text; // //被选的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value; // //把被选项的前一条或下一条的值用临时变量的取代 // ListBox.Items[ListBox.SelectedIndex].Test=lt.Test; // //把被选项的前一条或下一条的值用临时变量的取代 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; // //把鼠标指针放到移动后的那项上 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; //9. 移动指针到指定位置: // (1).移至首条 // //将被选项的索引设置为0就OK了 // ListBox.SelectIndex=0; // (2).移至尾条 // //将被选项的索引设置为ListBox.Items.Count-1就OK了 // ListBox.SelectIndex=ListBox.Items.Count-1; // (3).上一条 // //用当前被选的索引去减 1 // ListBox.SelectIndex=ListBox.SelectIndex - 1; // (4).下一条 // //用当前被选的索引去加 1 // ListBox.SelectIndex=ListBox.SelectIndex + 1; //this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后项","")); //this.ListBox1.Items.Insertat(index,ListItem) //ListBox1.Items.Insert(0,new ListItem("text","value"));
6 , chunks.zip<br>This will open a file and read it in "Chunks" of a selected file.<END><br>7 , logging.zip<br>This is a bas that will log installation procedures so the file can be removed later.<END><br>8 , savetree.zip<br>This will save the info in a Tree View. "This technique allows a program to save hierarchical information like the data in a TreeView in a way that is easy to understand."<END><br>11 , OLE.zip<br>Demonstrates the use of OLE.<END><br>12 , gradtxt2.zip<br>"A program for drawing horizontal, rectangular or spherical gradient texts."<END><br>13 , sweepgl.zip<br>This example greatly demonstrates how to use OpenGL in Visual Basic.<END><br>15 , drawdemo.zip<br>This is an excellent example of how to make a paint program with a few extras.<END><br>16 , cube.zip<br>This example demonstrates how to rotate a cube in visual basic.<END><br>17 , sprite1.zip<br>This is an Excellent example on how to use sprites in your program.<END><br>18 , charcreate.zip<br>This is an example of how to assign "characters" to differant pictureboxes. This would be a good starting point for VB game developers.<END><br>19 , breakthrough.zip<br>This demonstrates a simple game in Visual Basic. An excellent example.<END><br>26 , openlib.zip<br>These are the type libs that go with OpenGL. This is used to make 3D text.<END><br>27 , basMath.zip<br>This module contains functions for various math equations. <END><br>28 , calc.zip<br>This is a basic calculator written in Visual Basic.<END><br>29 , stopwatch.zip<br>This shows how to count off time in a Stop Watch format.<END><br>31 , taskhide.zip<br>This will hide your application from the taskbar, Alt+Tab, and Alt+Ctrl+Del.<END><br>32 , newbie.zip<br>This is a nicely done help file for programmers that are new to Visual Basic.<END><br>33 , vbfaq.zip<br>This is AOL's PC Dev Visual Basic FAQ. This is an excellent starting point for begginners.<END><br>34 , Bas.zip<br>it is very good modual for activex<END><br>35, paraviasource.zip<br>This is
1 , vb5dialog.zipThis demonstrates how to subclass the Common Dialog Dialogs and manipulate a specific Dialog.2 , cpnl.zipForm_Taskbar is a control for Visual Basic which, once placed onto a form, makes the form act like the Taskbar (minus the Start Menu).3 , vbo_progbar.zipImplement a common control progress bar with added features that are not accessable using COMCTL32.OCX! 4 , vbo_infolabel.zipThis control adds a great user-friendly interface with and icon and "Hover" ability. Based on a control seen in ICQ. 5 , vbo_checkcombo.zipAdd a checkbox to a combo box and use it to enabled/disable the combo! or whatever you would like to do with it! 6 , vbo_controlframe.zipCreate your own system button such as a Maximize, Minimize, Close, and many others with ease! 7 , vbo_ctextbox.zipThis class makes using the Textbox or Edit class API simple. Easily set properties and access many features not available directly from VB. 8 , taskbar.zipForm_Taskbar is a control for Visual Basic which, once placed onto a form, makes the form act like the Taskbar (minus the Start Menu).9 , NT_Service.zipThis is an OCX that allows you to create an NT service application...add the control to your project and register it as a service!!10 , Scroller.zipThis is a Control Container, it's like a frame control but it lets you scroll the content up and down...11 , TrayArea.zipThis control lets you add your icon to the System Tray Area and handle some events such as MouseMove, MouseDown, MouseUp and DblClick.12 , Resizer.zipThis is a very useful control: It's a container control, you can insert two controls inside and then you'll have a vertical (or horizontal) resizer bar (like the Windows File Explorer). A resizer can contain another resizer... an so on. (you can divide you form in as many sizable sections as you want...).13 , Label3D.zipTh

110,536

社区成员

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

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

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