0
我似乎無法找到匹配的響應,但我試圖測試我的界面,它迄今爲止工作完美無瑕,除了當我實現滾輪。可拖動標籤自動附加到列表項目的位置,當它被選中時。當滾輪移動時,附帶的標籤隨之移動。但是,選定的項目座標更新會延遲所使用的附加標籤。我嘗試了其他可能觸發即時位置的事件,就像按鈕一樣,但無濟於事。可能任何人有一個想法作爲解決方法嗎?列表項目位置沒有在滾動更新
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");
}
}
}
}