首页 > 其他分享 >LIstBox实现下拉列表

LIstBox实现下拉列表

时间:2022-11-30 18:46:38浏览次数:43  
标签:name 实现 LIstBox 列表 dr1 Add dt listBox id

1、初始化下拉列表

 DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            DataRow dr1 = dt.NewRow();
            dr1["id"] = "01";
            dr1["name"] = "光1";
            dt.Rows.Add(dr1);

            dr1 = dt.NewRow();
            dr1["id"] = "02";
            dr1["name"] = "光2";
            dt.Rows.Add(dr1);

            dr1 = dt.NewRow();
            dr1["id"] = "31";
            dr1["name"] = "瓷1";
            dt.Rows.Add(dr1);

            dr1 = dt.NewRow();
            dr1["id"] = "33";
            dr1["name"] = "瓷2";
            dt.Rows.Add(dr1);

            dr1 = dt.NewRow();
            dr1["id"] = csTypeId;
            dr1["name"] = "瓷3";
            dt.Rows.Add(dr1);

            listBox.DataSource = dt;
            listBox.DisplayMember = "name";
            listBox.ValueMember = "id";

 

2、显示位置

private void pbOrderTypeSelect_Click(object sender, EventArgs e)
{
listBox.Left = panel1.Left + txtCHOrderTypeID.Left;
listBox.Width = txtCHOrderTypeID.Width;
listBox.Top = panel1.Top + txtCHOrderTypeID.Top + txtCHOrderTypeID.Height;
listBox.Visible = true;
listBox.BringToFront();
}

3、设置事件

 

 

 private void lbCHOrderType_MouseLeave(object sender, EventArgs e)
        {
            string un = string.Empty;
            chorderTypeIdList.Clear();
            DataRow dr;
            for (Int32 i = 0; i <= listBox.SelectedItems.Count - 1; i++)
            {
                dr = ((DataRowView)listBox.SelectedItems[i]).Row;
                //chorderTypeStr+=$"{dr["id"].ToString()},";
                string id = $"{dr["id"].ToString()}";
                chorderTypeIdList.Add(id);
                un = un + (dr["name"].ToString() + ",");
            }
            if (un.Length > 1)
            {
                un = un.Remove(un.Length - 1, 1);
            }
            this.txtCHOrderTypeID.Text = un;
            this.listBox.Visible = false;
        }

 

标签:name,实现,LIstBox,列表,dr1,Add,dt,listBox,id
From: https://www.cnblogs.com/yuanshuo/p/16939404.html

相关文章