列表框问题 [英] Listbox problems

查看:60
本文介绍了列表框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[最初发布到mpdlvwindowsforms.controls,但似乎是那里的低流量
v,所以如果我在这里重新发布,希望没有人介意。]


我仍然在努力寻找一种方法来重新排序

相同单个列表框中的项目,并进行拖放操作。我想我已经得到了阻力

工作,但这是我无法弄清楚的掉落代码。我有什么

目前是(将列表框设置为AllowDrop):


Sub MyListBox_MouseDown(args等...)

DoDragDrop(MyListBox.SelectedItem,DragDropEffects.Move)

End Sub


Sub MyListBox_DragEnter(args etc ...)

e.effect = DragDropEffects.Move

结束sub


Sub MyListBox.DragDrop(args etc ...)

''这就是我被困的地方!大概我需要:

MyListBox.Items.Insert(???,MyListBox.SelectedItem)

结束子


显然我需要一些代码在最后一个子目录中插入当前选中的项目(即由Drag捕获的项目),然后在鼠标当前项目之前立即

徘徊。但是如何获得这个项目的索引然后放下所选项目。

可能/可能我需要明确删除源项目?


感谢您的帮助。

JGD

解决方案

您只是有效地改变索引源项目,即。这是集合中的
位置。要执行此操作,您应该删除源项目。它实际上不会被删除,因为当然你的拖放仍然有一个

引用它。为了找到正确的放置位置,你需要使用IndexFromPoint方法,传入鼠标坐标。


有一个使用拖动的MSDN示例/ drop at

http://msdn.microsoft.com/library/de...pointtopic.asp


" John Dann" ; <是ne ** @ prodata.co.uk>在消息中写道

新闻:n8 ******************************** @ 4ax.com ...

[最初发布到mpdlvwindowsforms.controls,但似乎是那里的交通流量很低,所以如果我转发这里,希望没有人介意。]
相同单个列表框中的项目。我想我已经有了拖累工作,但这是我无法弄清楚的掉落代码。我目前是什么(将列表框设置为AllowDrop):

Sub MyListBox_MouseDown(args等...)
DoDragDrop(MyListBox.SelectedItem,DragDropEffects.Move)<结束Sub

子MyListBox_DragEnter(args等...)
e.effect = DragDropEffects.Move
结束子

子MyListBox。 DragDrop(args等......)
''这里'我被卡住了!大概我需要:
MyListBox.Items.Insert(???,MyListBox.SelectedItem)
结束子

显然我需要在最后一个子代码中插入一些代码
当前所选项目(即由Drag捕获的项目)立即在鼠标当前悬停的项目之前
。但是如何才能获得该项目的索引然后删除所选项目。
可能/可能我需要明确删除源项目?

感谢您的帮助。
JGD



好的,非常感谢 - 我想我正在取得一些进展,但它不是

还在工作。我似乎能够拖动一个项目(或至少微弱的

灰色拖动矩形显示为光标),但执行插入的

的最后一步是'还没有。这是我目前的代码

有:

Private Sub lbxSelFields_MouseDown(ByVal发送者为对象,ByVal e As

System.Windows.Forms.MouseEventArgs)处理lbxSelFields.MouseDown

Dim lb作为ListBox = CType(发件人,ListBox)

Dim pt作为新点(eX,eY)

Dim index As Integer = lb.IndexFromPoint(pt)

如果index> = 0那么

lb.DoDragDrop(lb.Items(index) ).ToString(),

DragDropEffects.Move)

结束如果

结束子


Private Sub lbxSelFields_DragEnter(ByVal sender As Object,ByVal e

As System.Windows.Forms.DragEventArgs)处理lbxSelFields.DragEnter

如果e.Data.GetDataPresent(GetType(String) ))然后

e.Effect = DragDropEffects.Move

Else

e.Effect = DragDropEffects.None

结束如果

End Sub


Private Sub lbxSelFields_DragDrop(ByVal sender As Object,ByVal e

As System.Windows。 Forms.DragEventArgs)处理lbxSelFields.DragDrop

Dim lb作为ListBox = CType(发件人,ListBox)

Dim pt作为新点(eX,eY)

Dim index As Integer = lb.IndexFromPoint(pt)

lbxSelFields.Items.Insert(index,

e.Data.GetData(" System.string" ,真).ToString())

结束子


这是从2或3个例子拼凑而成的所以也许我是

错误地混合了一些参数/变量。我不知道为什么

例子我应该做出声明:


Dim lb作为ListBox = CType(发件人,ListBox)


当控件本身是一个列表框时。我可以在例如MouseDown程序的第3行内部说明:


Dim index As Integer = lbxSelFields.IndexFromPoint(pt)


但最重要的是为什么DragDrop事件中的lbxSelFields.Items.Insert(...)

行不起作用?


谢谢

JGD


检查索引值以确保它首先有效。虽然我确定

你会得到一个indexoutofrange异常,如果它是-1。

另外,你设置了AllowDrop吗?列表框的属性?


你不需要写,CType(发件人,ListBox),它只是另一种方式

方式看着事情。如果您在该方法中使用
,则可以明确使用您的列表框名称。

" John Dann" <是ne ** @ prodata.co.uk>在消息中写道

新闻:ua ******************************** @ 4ax.com ...

好的,非常感谢 - 我想我正在取得一些进展,但它还没有工作。我似乎能够拖动一个项目(或者至少将微弱的灰色拖动矩形显示为光标),但是执行插入的最后一步还没有。这是我目前所拥有的代码:

Private Sub lbxSelFields_MouseDown(ByVal sender As Object,ByVal e As
System.Windows.Forms.MouseEventArgs)处理lbxSelFields.MouseDown
Dim lb作为ListBox = CType(发送者,ListBox)
Dim pt作为新点(eX,eY)
Dim index As Integer = lb.IndexFromPoint(pt)
如果索引> = 0然后
lb.DoDragDrop(lb.Items(index).ToString(),
DragDropEffects.Move)
结束如果
End Sub

Private Sub lbxSelFields_DragEnter(ByVal sender As Object,ByVal e
As System.Windows.Forms.DragEventArgs)处理lbxSelFields.DragEnter
如果e.Data.GetDataPresent(GetType(String))则
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
结束如果
结束Sub

私有Sub lbxSelFields_DragDrop(ByVal发送者)作为Object,ByVal e
As System.Windows.Forms.D ragEventArgs)处理lbxSelFields.DragDrop
Dim lb作为ListBox = CType(发送者,ListBox)
Dim pt作为新点(eX,eY)
Dim index As Integer = lb.IndexFromPoint(pt)
lbxSelFields.Items.Insert(index,
End Sub

这是从2或3个例子拼凑而成的,所以也许我不正确地混合了一些参数/变量。我不确定为什么我要做声明的例子:

Dim lb作为ListBox = CType(发送者,ListBox)

当控件本身是一个列表框。我可以在例如MouseDown程序的第3行内部说:

Dim index As Integer = lbxSelFields.IndexFromPoint(pt)

但最重要的是为什么不能DragDrop事件中的lbxSelFields.Items.Insert(...)
行是否正常工作?

感谢
JGD



[Originally posted to m.p.d.l.v.windowsforms.controls but seems to be
v low traffic there, so hope no-one minds if I repost here.]

I''m still struggling to find a way of reordering the items within the
same single listbox with drag and drop. I think I''ve got the drag
working but it''s the drop code I can''t figure out. What I have
currently is (with the listbox set to AllowDrop):

Sub MyListBox_MouseDown(args etc...)
DoDragDrop(MyListBox.SelectedItem, DragDropEffects.Move)
End Sub

Sub MyListBox_DragEnter(args etc...)
e.effect=DragDropEffects.Move
End sub

Sub MyListBox.DragDrop(args etc...)
''Here''s where I''m stuck! Presumably I need:
MyListBox.Items.Insert(???, MyListBox.SelectedItem)
End Sub

Obviously I need some code in the last sub to insert the
currently-selected item (ie the one picked up by the Drag) immediately
before the item over which the mouse is currently hovering. But how
can I get the index of this item and then drop the selected item.
Possibly/probably I need to explicitly delete the source item?

Thanks for any help.
JGD

解决方案

You are just effecitvely changing the index of the source item, ie. it''s
location in the set. To do this yes you should delete the source item. It
won''t actually be "deleted", because of course your drag/drop still holds a
reference to it. In order to discover the correct placement position, you
need to use the IndexFromPoint method, passing in the mouse coords.

There is an MSDN example using drag/drop here:

http://msdn.microsoft.com/library/de...pointtopic.asp

"John Dann" <ne**@prodata.co.uk> wrote in message
news:n8********************************@4ax.com...

[Originally posted to m.p.d.l.v.windowsforms.controls but seems to be
v low traffic there, so hope no-one minds if I repost here.]

I''m still struggling to find a way of reordering the items within the
same single listbox with drag and drop. I think I''ve got the drag
working but it''s the drop code I can''t figure out. What I have
currently is (with the listbox set to AllowDrop):

Sub MyListBox_MouseDown(args etc...)
DoDragDrop(MyListBox.SelectedItem, DragDropEffects.Move)
End Sub

Sub MyListBox_DragEnter(args etc...)
e.effect=DragDropEffects.Move
End sub

Sub MyListBox.DragDrop(args etc...)
''Here''s where I''m stuck! Presumably I need:
MyListBox.Items.Insert(???, MyListBox.SelectedItem)
End Sub

Obviously I need some code in the last sub to insert the
currently-selected item (ie the one picked up by the Drag) immediately
before the item over which the mouse is currently hovering. But how
can I get the index of this item and then drop the selected item.
Possibly/probably I need to explicitly delete the source item?

Thanks for any help.
JGD



OK, many thanks - I''m making some progress I think, but it''s not
working yet. I seem to be able to drag an item (or at least the faint
grey dragging rectangle appears as the cursor), but the final step of
performing the insert isn''t there yet. Here''s the code I currently
have:

Private Sub lbxSelFields_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbxSelFields.MouseDown
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
Dim index As Integer = lb.IndexFromPoint(pt)
If index >= 0 Then
lb.DoDragDrop(lb.Items(index).ToString(),
DragDropEffects.Move)
End If
End Sub

Private Sub lbxSelFields_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles lbxSelFields.DragEnter
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub lbxSelFields_DragDrop(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles lbxSelFields.DragDrop
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
Dim index As Integer = lb.IndexFromPoint(pt)
lbxSelFields.Items.Insert(index,
e.Data.GetData("System.string", True).ToString())
End Sub

This is cobbled together from 2 or 3 examples and so maybe I''m
incorrectly mixing some parameters/variables. I''m not sure why for
example I should make the declaration:

Dim lb As ListBox = CType(sender, ListBox)

when the control itself is a listbox. Could I just say in eg internal
line 3 of the MouseDown procedure:

Dim index As Integer = lbxSelFields.IndexFromPoint(pt)

But most importantly why wouldn''t the lbxSelFields.Items.Insert(...)
line in the DragDrop event be working?

Thanks
JGD


Check the index value to make sure it is valid firstly. Although I''m sure
you would get an indexoutofrange exception if it was something like -1.
Also, did you set the "AllowDrop" property of the list box?

You don''t need to write, CType(sender, ListBox), it''s just an alternative
way of looking at things. You can explicitly use your list box name if you
like in that method.
"John Dann" <ne**@prodata.co.uk> wrote in message
news:ua********************************@4ax.com...

OK, many thanks - I''m making some progress I think, but it''s not
working yet. I seem to be able to drag an item (or at least the faint
grey dragging rectangle appears as the cursor), but the final step of
performing the insert isn''t there yet. Here''s the code I currently
have:

Private Sub lbxSelFields_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lbxSelFields.MouseDown
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
Dim index As Integer = lb.IndexFromPoint(pt)
If index >= 0 Then
lb.DoDragDrop(lb.Items(index).ToString(),
DragDropEffects.Move)
End If
End Sub

Private Sub lbxSelFields_DragEnter(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles lbxSelFields.DragEnter
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub lbxSelFields_DragDrop(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles lbxSelFields.DragDrop
Dim lb As ListBox = CType(sender, ListBox)
Dim pt As New Point(e.X, e.Y)
Dim index As Integer = lb.IndexFromPoint(pt)
lbxSelFields.Items.Insert(index,
e.Data.GetData("System.string", True).ToString())
End Sub

This is cobbled together from 2 or 3 examples and so maybe I''m
incorrectly mixing some parameters/variables. I''m not sure why for
example I should make the declaration:

Dim lb As ListBox = CType(sender, ListBox)

when the control itself is a listbox. Could I just say in eg internal
line 3 of the MouseDown procedure:

Dim index As Integer = lbxSelFields.IndexFromPoint(pt)

But most importantly why wouldn''t the lbxSelFields.Items.Insert(...)
line in the DragDrop event be working?

Thanks
JGD



这篇关于列表框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆