失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C#中要使ListBox使用AddRange()时 能够触发SelectedValueChanged事件

C#中要使ListBox使用AddRange()时 能够触发SelectedValueChanged事件

时间:2018-09-12 19:54:25

相关推荐

C#中要使ListBox使用AddRange()时 能够触发SelectedValueChanged事件

1. 要触发 SelectedValueChanged事件,必须要当ListBox所选中的值发生改变

基本思路是:

当AddRange()后,就马上指定ListBox的SelectedIndex,这样就能够触发SelectedValueChanged事件了

相当于人工在代码中选中了一个选项

2. 例子代码:

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace ListBoxAddRangeTest{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void listBox1_SelectedValueChanged(object sender, EventArgs e){if (listBox1.SelectedItems.Count > 0){System.Windows.Forms.MessageBox.Show(listBox1.SelectedItems[0].ToString());}}private void button1_Click(object sender, EventArgs e){string[] testArray = new string[] { "me", "you" };listBox1.Items.AddRange(testArray);listBox1.SelectedIndex = 0;//就是这里,人工选中}}}

如果觉得《C#中要使ListBox使用AddRange()时 能够触发SelectedValueChanged事件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。