C#如何用单击鼠标右键选择列表框项目? [英] C# How to select ListBox item with a RightClick?
本文介绍了C#如何用单击鼠标右键选择列表框项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经尝试了很多这种方法和研究DONE小时,但它只是似乎永远不会为我工作。
这是我当前的代码,并我不知道为什么它不应该工作。
私人无效listBox1_MouseDown(对象发件人,MouseEventArgs E)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(例如,EY);
如果(e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show();
}
}
此外,我不关心上下文菜单可以去掉,我只是在寻找一种方法,使鼠标右键选择我点击的项目。
任何想法?
< DIV CLASS =h2_lin>解决方案
您比较接近,你只是忘记了选择的项目。修复:
私人无效listBox1_MouseUp(对象发件人,MouseEventArgs E){
如果(e.Button == MouseButtons。右){
VAR项目= listBox1.IndexFromPoint(e.Location);
如果(项目> = 0){
listBox1.SelectedIndex =项目;
contextMenuStrip1.Show(listBox1中,e.Location);
}
}
}
I have tried a lot of methods for this and done hours of research, but it just never seems to work for me.
This is my current code, and I don't know why it shouldn't work.
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show();
}
}
Also I don't care about the context menu that can be removed I am just looking for a way to make the right mouse button select the item I click on.
Any Ideas?
解决方案
You are close, you just forgot to select the item. Fix:
private void listBox1_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var item = listBox1.IndexFromPoint(e.Location);
if (item >= 0) {
listBox1.SelectedIndex = item;
contextMenuStrip1.Show(listBox1, e.Location);
}
}
}
这篇关于C#如何用单击鼠标右键选择列表框项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文