C# ListView selectedindexchanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9857141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListView selectedindexchanged
提问by TheZozoOwner
I need help to get a response when I click on an "Item" from a list view. Know that there is selectedindexchanged, but when I try to display a MessageBox so nothing happens, have tried lots of other things but have not managed to come up with something.
当我从列表视图中单击“项目”时,我需要帮助才能获得响应。知道有 selectedindexchanged,但是当我尝试显示 MessageBox 所以什么也没发生时,已经尝试了很多其他的东西,但没有设法想出一些东西。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
...
while (reader.Read())
{
string alio = reader["fornamn"].ToString();
string efternamn = reader["efternamn"].ToString();
ListViewItem lvi = new ListViewItem(alio);
listView1.Items.Add(lvi);
lvi.SubItems.Add(efternamn);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
采纳答案by Eddie Paz
Assuming that 81.private void listView1_SelectedIndexChanged is properly linked to the listview, you will need to query the listview to find out what's selected:
假设 81.private void listView1_SelectedIndexChanged 正确链接到列表视图,您将需要查询列表视图以找出选择的内容:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.listView1.SelectedItems.Count == 0)
return;
string namn = this.listView1.SelectedItems[0].Text;
// Create the sql statement to retrieve details for the user
string sql = string.Format("select * from kunder where fornamn = '{0}', namn);
// do the same as you do to create a reader and update the controls.
}
回答by Ε Г И ? И О
Going by the term "when I try to display a MessageBox so nothing happens"\, I assume that you simply put MessageBox.Show("blah");inside the event handler and never got it shown.
按照术语“当我尝试显示 MessageBox 时什么也没有发生”\,我假设您只是将MessageBox.Show("blah");事件处理程序放入事件处理程序中,但从未显示过它。
If that's the case, your event handler is not hooked properly to your form's list view. go back and see the text listView1_SelectedIndexChangedis anywhere to be found inside your Form1.Designer.csfile.
如果是这种情况,则您的事件处理程序没有正确连接到表单的列表视图。返回并查看文本listView1_SelectedIndexChanged在Form1.Designer.cs文件中的任何位置。
If not (or anyway), start over on a new form. That's the easiest way out. :)
如果不是(或无论如何),请重新开始一个新表格。这是最简单的出路。:)
回答by mssphuonglinh
private void lstView_KQ_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstView_KQ.SelectedItems.Count > 0)
{
ListViewItem itiem = stView_KQ.SelectedItems[lstView_KQ.SelectedItems.Count - 1];
if (itiem != null)
foreach (ListViewItem lv in lstView_KQ.SelectedItems)
{
txtMaNV.Text = lv.SubItems[0].Text;
cmbCV.Text = lv.SubItems[1].Text;
txtHoNV.Text = lv.SubItems[2].Text;
txtTenNV.Text = lv.SubItems[3].Text;
txtNgaysinh.Text = lv.SubItems[4].Text;
txtGioiTinh.Text = lv.SubItems[5].Text;
txtDiaChi.Text = lv.SubItems[6].Text;
txtSDT.Text = lv.SubItems[7].Text;
txtCMND.Text = lv.SubItems[8].Text;
}
}
}

