C# 列表项位置未在滚动上更新
我似乎找不到与此匹配的响应,但我正在尝试测试我的界面,目前为止它工作正常,除了在实现滚轮时。选中列表项时,可拖动标签会自动附着到列表项的位置。当滚轮移动时,附着的标签也随之移动。但是,要使用的附加标签的选定项目坐标更新会延迟。我尝试了其他可以像按钮一样触发即时位置的事件,但没有效果。有没有人能想出一个解决办法C# 列表项位置未在滚动上更新,c#,listview,C#,Listview,我似乎找不到与此匹配的响应,但我正在尝试测试我的界面,目前为止它工作正常,除了在实现滚轮时。选中列表项时,可拖动标签会自动附着到列表项的位置。当滚轮移动时,附着的标签也随之移动。但是,要使用的附加标签的选定项目坐标更新会延迟。我尝试了其他可以像按钮一样触发即时位置的事件,但没有效果。有没有人能想出一个解决办法 using System; using System.Drawing; using System.Windows.Forms; namespace matchMoving { p
using System;
using System.Drawing;
using System.Windows.Forms;
namespace matchMoving
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.MouseDown += new MouseEventHandler(Label_Mouse_Down);
label1.MouseMove += new MouseEventHandler(Label_Mouse_Move);
listBox1.MouseWheel += new MouseEventHandler(List_Mouse_Scroll);
listBox1.MouseMove += new MouseEventHandler(List_Mouse_Move);
listBox1.MouseDown += new MouseEventHandler(List_Mouse_Down);
}
Rectangle IndexLocation;
private Point MouseDownLocation;
public void List_Function()
{
label1.Text = listBox1.SelectedIndex.ToString();
IndexLocation = listBox1.GetItemRectangle(listBox1.SelectedIndex);
int nx = Convert.ToInt32(IndexLocation.X);
int ny = Convert.ToInt32(IndexLocation.Y);
label1.Location = new Point(nx, ny);
textBox1.Text = listBox1.GetItemRectangle(listBox1.SelectedIndex).ToString();
}
public void List_Mouse_Move(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
List_Function();
}
}
public void List_Mouse_Scroll(object sender, MouseEventArgs e)
{
List_Function();
}
private void List_Mouse_Down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
List_Function();
}
}
private void Label_Mouse_Down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void Label_Mouse_Move(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point start = Point.Empty;
int nx = Math.Min(Math.Max(label1.Left - MouseDownLocation.X + (e.X - start.X), 0), label1.Parent.Width - label1.Width);
int ny = Math.Min(Math.Max(label1.Top - MouseDownLocation.Y + (e.Y - start.Y), 0), label1.Parent.Height - label1.Height);
label1.Location = new Point(nx, ny);
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < 40; x++)
{
listBox1.Items.Add("This is new text");
}
}
}
}
使用系统;
使用系统图;
使用System.Windows.Forms;
命名空间匹配移动
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
label1.MouseDown+=新的MouseEventHandler(Label\u Mouse\u Down);
label1.MouseMove+=新的MouseEventHandler(Label\u Mouse\u Move);
listBox1.MouseWheel+=新的鼠标指针(列表鼠标滚动);
listBox1.MouseMove+=新的MouseEventHandler(List\u Mouse\u Move);
listBox1.MouseDown+=新的MouseEventHandler(列出鼠标向下);
}
矩形索引;
私人点鼠标下移位置;
公共无效列表_函数()
{
label1.Text=listBox1.SelectedIndex.ToString();
IndexLocation=listBox1.GetItemRectangle(listBox1.SelectedIndex);
int nx=转换为32(IndexLocation.X);
int ny=转换为32(索引位置Y);
标签1.位置=新点(nx,ny);
textBox1.Text=listBox1.GetItemRectangle(listBox1.SelectedIndex.ToString();
}
公共无效列表\鼠标\移动(对象发送者,鼠标目标)
{
if(MouseButtons==MouseButtons.Left)
{
List_函数();
}
}
公共无效列表\鼠标\滚动(对象发送器,鼠标目标)
{
List_函数();
}
私有无效列表鼠标按下(对象发送者,鼠标目标)
{
if(e.Button==MouseButtons.Left)
{
List_函数();
}
}
专用无效标签\鼠标\向下(对象发送器,鼠标目标e)
{
if(e.Button==MouseButtons.Left)
{
MouseDownLocation=e.位置;
}
}
专用无效标签\u鼠标\u移动(对象发送器,鼠标目标e)
{
if(e.Button==MouseButtons.Left)
{
点开始=点。空;
int nx=Math.Min(Math.Max(label1.Left-MouseDownLocation.X+(e.X-start.X),0),label1.Parent.Width-label1.Width);
int ny=Math.Min(Math.Max(label1.Top-MouseDownLocation.Y+(e.Y-start.Y),0),label1.Parent.Height-label1.Height);
标签1.位置=新点(nx,ny);
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
对于(int x=0;x<40;x++)
{
listBox1.Items.Add(“这是新文本”);
}
}
}
}
这是我发现的有效方法。此代码将创建一个标签,显示每个列表框项目的索引。这个标签是可拖动的。选择列表框项目时,标签将附着到列表框项目的位置。滚动滚轮或鼠标选择新的列表框项目时,标签跟随列表框项目的位置。要实现可点击控制的滚动,可能需要自定义
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
namespace MouseMatchMoving
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// These are the event handling functions
label1.MouseDown += new MouseEventHandler(Label_Mouse_Down);
label1.MouseMove += new MouseEventHandler(Label_Mouse_Move);
label1.MouseUp += new MouseEventHandler(Label_Mouse_Up);
listBox1.MouseWheel += new MouseEventHandler(List_Mouse_Scroll);
listBox1.MouseMove += new MouseEventHandler(List_Mouse_Move);
listBox1.MouseDown += new MouseEventHandler(List_Mouse_Down);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(BProgress);
backgroundWorker1.DoWork += new DoWorkEventHandler(BDowork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BWCompleted);
backgroundWorker1.WorkerReportsProgress = true;
// Start the backgroundworker
backgroundWorker1.RunWorkerAsync();
label1.BringToFront();
}
Rectangle IndexLocation;
private Point MouseDownLocation;
// Form loading
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < 40; x++)
{
listBox1.Items.Add("This is new text");
}
listBox1.SelectedIndex = 0;
}
// This backgroundworker invokes the GUI thread
private void BDowork(object sender, DoWorkEventArgs e)
{
if (InvokeRequired)
{
this.Invoke(new Action(() => List_Function()));
return;
}
}
private void BProgress(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
Console.WriteLine(e.ProgressPercentage.ToString());
Console.WriteLine(e.UserState);
}
private void BWCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine(e.Cancelled);
}
// This begins the mouse ListBox Focus functions
private void List_Mouse_Down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
List_Function();
}
}
public void List_Mouse_Move(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
List_Function();
}
}
// This is the mouse scrolling function which calls the backgroundworker
public void List_Mouse_Scroll(object sender, MouseEventArgs e)
{
listBox1.SetSelected(listBox1.SelectedIndex, true);
listBox1.GetSelected(listBox1.SelectedIndex);
backgroundWorker1.RunWorkerAsync();
}
// This is invokes the draggable label mouse functions
private void Label_Mouse_Down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void Label_Mouse_Move(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point start = Point.Empty;
int nx = Math.Min(Math.Max(label1.Left - MouseDownLocation.X + (e.X - start.X), 0), label1.Parent.Width - label1.Width);
int ny = Math.Min(Math.Max(label1.Top - MouseDownLocation.Y + (e.Y - start.Y), 0), label1.Parent.Height - label1.Height);
label1.Location = new Point(nx, ny);
}
}
private void Label_Mouse_Up(object sender, MouseEventArgs e)
{
MouseDownLocation = e.Location;
}
// This is the mouse list tracking function
public void List_Function()
{
label1.Text = listBox1.SelectedIndex.ToString();
IndexLocation = listBox1.GetItemRectangle(listBox1.SelectedIndex);
int nx = Convert.ToInt32(IndexLocation.X);
int ny = Convert.ToInt32(IndexLocation.Y);
label1.Location = new Point(nx, ny);
textBox1.Text = listBox1.GetItemRectangle(listBox1.SelectedIndex).ToString();
Console.WriteLine(listBox1.GetItemRectangle(listBox1.SelectedIndex));
}
}
}
使用系统;
使用系统图;
使用System.Windows.Forms;
使用系统线程;
使用系统组件模型;
名称空间鼠标移动
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
//这些是事件处理函数
label1.MouseDown+=新的MouseEventHandler(Label\u Mouse\u Down);
label1.MouseMove+=新的MouseEventHandler(Label\u Mouse\u Move);
label1.MouseUp+=新的MouseEventHandler(Label\u Mouse\u Up);
listBox1.MouseWheel+=新的鼠标指针(列表鼠标滚动);
listBox1.MouseMove+=新的MouseEventHandler(List\u Mouse\u Move);
listBox1.MouseDown+=新的MouseEventHandler(列出鼠标向下);
backgroundWorker1.ProgressChanged+=新的ProgressChangedEventHandler(BProgress);
backgroundWorker1.DoWork+=新的DoworkerVenthandler(B工作);
backgroundWorker1.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(已完成);
backgroundWorker1.WorkerReportsProgress=true;
//启动后台工作人员
backgroundWorker1.RunWorkerAsync();
标签1.BringToFront();
}
矩形索引;
私人点鼠标下移位置;
//模板加载
私有void Form1\u加载(对象发送方、事件参数e)
{
对于(int x=0;x<40;x++)
{
listBox1.Items.Add(“这是新文本”);
}
listBox1.SelectedIndex=0;
}
//此backgroundworker调用GUI线程
私有void BDowork(对象发送方,DoWorkEventArgs e)
{
如果(需要调用)
{
调用(新操作(()=>List_Function());
返回;
}
}
私有void BProgress(对象发送方,ProgressChangedEventArgs e)
{
label1.Text=e.ProgressPercentage.ToString();
Console.WriteLine(例如ProgressPercentage.ToString());
Console.WriteLine(如UserState);
}
私有void BWCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
控制台写入线(如取消);
}
//这将从鼠标列表框Foc开始