zoukankan      html  css  js  c++  java
  • C#中要使ListBox使用AddRange()时,能够触发SelectedValueChanged事件

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

        基本思路是:

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

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

    2. 例子代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    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;//就是这里,人工选中
            }
        }
    }
    

      

  • 相关阅读:
    常见前端面试题CSS部分
    window.location
    实时时间设置
    常用兼容处理
    背景淡入淡出切换
    常用插件
    PHP文件处理函数
    PHP的数据处理函数二(数组)
    php的数据处理函数一(字符串)
    PHP环境搭建(phpstudy)
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4765216.html
Copyright © 2011-2022 走看看