我想在列表框中选择多行c#

我想在列表框中选择多行c#

本文介绍了我想在列表框中选择多行c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void loadTable()
{
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            listBox3.Items.Clear();
            listBox4.Items.Clear();
            listBox5.Items.Clear();
            listBox6.Items.Clear();
            listBox7.Items.Clear();
            listBox8.Items.Clear();

            string query = "SELECT * FROM total ORDER BY id DESC";


            connection.Open();
            cmd.CommandText = query;
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {

                    listBox1.Items.Add(dr[1].ToString());
                    listBox2.Items.Add(dr[2].ToString());
                    listBox3.Items.Add(dr[3].ToString());
                    listBox4.Items.Add(dr[4].ToString());
                    listBox5.Items.Add(dr[5].ToString());
                    listBox6.Items.Add(dr[6].ToString());
                    listBox7.Items.Add(dr[7].ToString());
                    listBox8.Items.Add(dr[8].ToString());

                }
               }
            connection.Close();
        }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lsb = sender as ListBox;


               if (lsb.SelectedIndex != -1)
            {


                listBox1.SelectedIndex = lsb.SelectedIndex;
                listBox2.SelectedIndex = lsb.SelectedIndex;
                listBox3.SelectedIndex = lsb.SelectedIndex;
                listBox4.SelectedIndex = lsb.SelectedIndex;
                listBox5.SelectedIndex = lsb.SelectedIndex;
                listBox6.SelectedIndex = lsb.SelectedIndex;
                listBox7.SelectedIndex = lsb.SelectedIndex;
                listBox8.SelectedIndex = lsb.SelectedIndex;



            }

        }

推荐答案

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListBox lsb = sender as ListBox;
    if (lsb.SelectedIndex != -1)
    {
        //listBox1.SelectedIndex = lsb.SelectedIndex;
        listBox2.SelectedIndex = lsb.SelectedIndex;
        listBox3.SelectedIndex = lsb.SelectedIndex;
        listBox4.SelectedIndex = lsb.SelectedIndex;
        listBox5.SelectedIndex = lsb.SelectedIndex;
        listBox6.SelectedIndex = lsb.SelectedIndex;
        listBox7.SelectedIndex = lsb.SelectedIndex;
        listBox8.SelectedIndex = lsb.SelectedIndex;
    }
}


这篇关于我想在列表框中选择多行c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 10:26