在listbox中选中取值为什么结果却是这个WindowsFormsapplication forms1.MVP

C# ListBox Control
The ListBox control enables you to display a list of items to the user that the user can select by clicking.
In addition to display and selection functionality, the ListBox also provides features that enable you to efficiently add items to the ListBox and to find text within the items of the list. You can use the Add or Insert method to add items to a list box. The Add method adds new items at the end of an unsorted list box.
listBox1.Items.Add("Sunday");
If you want to retrieve a single selected item to a variable , you can code like this
var = listBox1.T
The SelectionMode property determines how many items in the list can be selected at a time. A ListBox control can provide single or multiple selections using the SelectionMode property . If you change the selection mode property to multiple select , then you will retrieve a collection of items from ListBox1.SelectedItems property.
listBox1.SelectionMode = SelectionMode.MultiS
The following C# program initially fill seven days in a week while in the form load event and set the selection mode property to MultiSimple. At the Button click event it will display the selected items.
using System.D
using System.Windows.F
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
listBox1.Items.Add("Sunday");
listBox1.Items.Add("Monday");
listBox1.Items.Add("Tuesday");
listBox1.Items.Add("Wednesday");
listBox1.Items.Add("Thursday");
listBox1.Items.Add("Friday");
listBox1.Items.Add("Saturday");
listBox1.SelectionMode = SelectionMode.MultiS
private void button1_Click(object sender, EventArgs e)
foreach (Object obj in listBox1.SelectedItems )
MessageBox.Show(obj.ToString ());
How to bind a ListBox to a List ?
First you should create a fresh List Object and add items to the List.
List nList = new List();
nList.Add("January");
nList.Add("February");
nList.Add("March");
nList.Add("April");
The next step is to bind this List to the Listbox. In order to do that you should set datasource of the Listbox.
listBox1.DataSource = nL
Full Source code
private void button1_Click(object sender, EventArgs e)
List nList = new List();
nList.Add("January");
nList.Add("February");
nList.Add("March");
nList.Add("April");
listBox1.DataSource = nL
How to bind a listbox to database values ?
First you should create a connection string and fetch data from database to a Dataset.
connetionString = "Data Source=ServerNInitial Catalog=User ID=Password=yourpassword";
sql = "select au_id,au_lname from authors";
After that you should set Listbox datasoure as Dataset.
listBox1.DataSource = ds.Tables[0];
listBox1.ValueMember = "au_id";
listBox1.DisplayMember = "au_lname";
using System.D
using System.Data.SqlC
using System.Windows.F
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
string connetionString =
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql =
//connetionString = "Data Source=ServerNInitial Catalog=User ID=Password=yourpassword";
//sql = "select au_id,au_lname from authors";
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand =
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
listBox1.DataSource = ds.Tables[0];
listBox1.ValueMember = "au_id";
listBox1.DisplayMember = "au_lname";
catch (Exception ex)
MessageBox.Show("Cannot open connection ! ");
How to refresh DataSource of a ListBox
How to clear the Listbox if its already binded with datasource ?
When you want to clear the Listbox, if the ListBox already binded with Datasource, you have to set the Datasource of Listbox as null.
listBox1.DataSource =
How to SelectedIndexChanged event in ListBox ?
This event is fired when the item selection is changed in a ListBox. You can use this event in a situation that you want select an item from your listbox and accodring to this selection you can perform other programming needs.
You can add the event handler using the Properties Window and selecting the Event icon and double-clicking on SelectedIndexChanged as you can see in following image.
The event will fire again when you select a new item. You can write your code within
SelectedIndexChanged event . When you double click on ListBox the code will automatically come in you code editor like the following image.
From the following example you can understand how to fire the SelectedIndexChanged event
First you should drag two listboxes on your Form. First listbox you should set the List as Datasource, the List contents follows:
List nList = new List();
nList.Add("First Quarter");
nList.Add("Second Quarter");
When you load this form you can see the listbox is populated with List and displayed first quarter and second quarter. When you click the "Fist Quarter" the next listbox is populated with first quarter months and when you click "Second Quarter" you can see the second listbox is changed to second quarter months. From the following program you can understand how this happened.
using S
using System.D
using System.Data.SqlC
using System.Windows.F
using System.Collections.G
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
public Form1()
InitializeComponent();
fQ = new List
sQ = new List
private void Form1_Load(object sender, EventArgs e)
fQ.Add("January");
fQ.Add("February");
fQ.Add("March");
sQ.Add("April");
sQ.Add("May");
sQ.Add("June");
nList = new List
nList.Add("First Quarter");
nList.Add("Second Quarter");
listBox1.DataSource = nL
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
if (listBox1.SelectedIndex == 0)
listBox2.DataSource =
listBox2.DataSource = fQ;
else if (listBox1.SelectedIndex == 1)
listBox2.DataSource =
listBox2.DataSource = sQ;
Mail to :&&&&&&
&& Founded by
All Rights Reserved. All other trademarks are property of their respective owners.下次自动登录
现在的位置:
& 综合 & 正文
用C#实现在ListBox中拖动排序
用C#实现在ListBox中拖动排序
现在在C#中处理有关的拖放操作变得比以前方便多了,现在就已一个例子说明,这个例子演示了通过鼠标的拖动在一个ListBox中进行排序操作。
相关源码如下:
using Susing System.Dusing System.Cusing ponentMusing System.Windows.Fusing System.Dnamespace WindowsApplication2...{
public class Form1 : System.Windows.Forms.Form
private System.Windows.Forms.ListBox listBox1;
int//拖动的起始索引
int //拖动的结束索引
private ponentModel.Container components = null;
public Form1()
InitializeComponent();
protected override void Dispose(bool disposing)
if (disposing)
if (components != null)
components.Dispose();
base.Dispose(disposing);
Windows 窗体设计器生成的#region Windows 窗体设计器生成的代码
private void InitializeComponent()
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
// listBox1
this.listBox1.AllowDrop = true;//这个属性一定要设置为
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] ...{
this.listBox1.Location = new System.Drawing.Point(16, 8);
this.listBox1.Name = "listBox1";
this.listBox1.ScrollAlwaysVisible = true;
this.listBox1.Size = new System.Drawing.Size(264, 136);
this.listBox1.TabIndex = 0;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
this.listBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox1_DragOver);
this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
#endregion
[STAThread]
static void Main()
Application.Run(new Form1());
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
indexofsource = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
if (indexofsource != ListBox.NoMatches)
((ListBox)sender).DoDragDrop(((ListBox)sender).Items[indexofsource].ToString(), DragDropEffects.All);
private void listBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
...{ //拖动源和放置的目的地一定是一个ListBox
if (e.Data.GetDataPresent(typeof(System.String)) && ((ListBox)sender).Equals(listBox1))
e.Effect = DragDropEffects.M
e.Effect = DragDropEffects.N
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
ListBox listbox = (ListBox)
indexoftarget = listbox.IndexFromPoint(listbox.PointToClient(new Point(e.X, e.Y)));
if (indexoftarget != ListBox.NoMatches)
string temp = listbox.Items[indexoftarget].ToString();
listbox.Items[indexoftarget] = listbox.Items[indexofsource];
listbox.Items[indexofsource] =
listbox.SelectedIndex =
好了,现在我们的目标达到了。我们还可以通过处理QueryContinueDrag、GiveFeedback事件获得更多的功能。
&&&&推荐文章:
【上篇】【下篇】非静态的字段、方法或属性“WindowsFormsApplication1.Form1.textBox1”要求对象引用???怎么解决?
[问题点数:40分]
非静态的字段、方法或属性“WindowsFormsApplication1.Form1.textBox1”要求对象引用???怎么解决?
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。visual studio 错误 1 无法将文件“obj\Debug\WindowsFormsApplication55.exe”复制到“bin\Debug\Windows_百度知道查看: 4319|回复: 7
如何根据LISTBOX中选中的内容来进行下一步操作
阅读权限30
在线时间 小时
有个LISTBOX控件,里面有两个项,比如说A,B(是工作表的名字),当我选择A,然后按确定后,对A中的数据绘图,显示在A表中!选择B后,对B表中的数据绘图,并显示在B表中!附件有代码,但是只能对A表中的数据绘图,选择B后,不能自动选择B表的数据绘图,这个该如何修改!
第一次进去时,请点击更新!
[ 本帖最后由 linkspeed 于
15:24 编辑 ]
15:23 上传
点击文件名下载附件
12.15 KB, 下载次数: 90
阅读权限100
在线时间 小时
Public nm$
Private Sub CommandButton1_Click()
ListBox1.Clear
For Each sh In Sheets
&&If sh.Visible = -1 Then
& &ListBox1.AddItem sh.Name
&&End If
Next
End Sub
Private Sub CommandButton2_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount
If ListBox1.Selected(i) Then
& & nm = ListBox1.Text
& & Call Chart
End If
Next
End Sub
Private Sub Chart()
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets(nm).Range(&B5:B25&) _
& && && && & , PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).XValues = &='& & nm & &'!R5C1:R25C1&
ActiveChart.Location where:=xlLocationAsObject, Name:=nm
End Sub
复制代码
阅读权限30
在线时间 小时
哦,就是不知道LISTBOX有什么属性,TEXT就是取值吧!明白了,谢谢!
阅读权限30
在线时间 小时
回复 2楼 蓝桥玄霜 的帖子
对了,nm后面的$是什么意思,为什么在过程里面只是nm了,而不是nm$?
阅读权限30
在线时间 小时
是字符串的意思吗?
阅读权限30
在线时间 小时
回复 2楼 蓝桥玄霜 的帖子
ActiveChart.SeriesCollection(1).XValues = &='& & nm & &'!R5C1:R25C1&
请问这句当中,为什么在原来是工作表名的地方,现在却是& & nm & &,而不直接是nm,两边为什么要用&连接符呢?
[ 本帖最后由 linkspeed 于
16:22 编辑 ]
阅读权限95
在线时间 小时
回复 6楼 linkspeed 的帖子
如果不用连接符就没有变量的意思了。
阅读权限30
在线时间 小时
回复 7楼 lb_bn 的帖子
那为什么在name:=nm,却没用呢?而且在sheet(nm)这也没用呢?
玩命加载中,请稍候
玩命加载中,请稍候
Powered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师

我要回帖

更多关于 application forms 的文章

 

随机推荐