简体   繁体   English

如何使用按钮从VB6 ListView中删除行(项)?

[英]How to remove a row (item) from a VB6 ListView using a button?

How do I delete a row in a ListView. 如何删除ListView中的行。 I need to select the row to be deleted and a command button will delete it with a alert message if you want to delete the row. 我需要选择要删除的行,如果要删除该行,命令按钮将使用警告消息将其删除。 What will be the code for that? 那个代码是什么?

Assuming you already created ListView (ListView1) and a Click event for the button (let's call it button1), by double-clicking on it, the could would go something like this: 假设你已经创建了ListView(ListView1)和按钮的Click事件(让我们称之为button1),通过双击它,可能会是这样的:

So the code would go something like this: 所以代码会是这样的:

private sub Button1_Click()
    if ListView1.SelectedItem is nothing then exit sub

    if MsgBox("Do you really want to delete?", "Question", vbYesNo) = vbYes then
        ListView1.ListItems.Remove ListView1.SelectedItem.Index
    end if
end sub

What controls have you already used and what code have you already written to make this happen? 您已经使用了哪些控件以及您已经编写了哪些代码才能实现此目的? (You can add this information to your question by editing it). (您可以通过编辑将此信息添加到您的问题中)。

Anyway, I assume you at least have a ListView control (eg ListView1) and a Button control (eg DeleteRow), and that you know about that button's click event, DeleteRow_Click (if not, double-click the button in the form designer, and you'll see what I mean). 无论如何,我假设您至少有一个ListView控件(例如ListView1)和一个Button控件(例如DeleteRow),并且您知道该按钮的click事件,DeleteRow_Click(如果没有,请双击表单设计器中的按钮,以及你会明白我的意思。

Now, you'll need to put some code in the DeleteRow_Click event. 现在,您需要在DeleteRow_Click事件中放入一些代码。 Some hints: 一些提示:

  • The currently selected row (item) in the ListView is ListView1.SelectedItem. ListView中当前选定的行(项)是ListView1.SelectedItem。 SelectedItem is an object with some useful properties: you can explore these using VB's Object Explorer and/or Intellisense in the editor. SelectedItem是一个具有一些有用属性的对象:您可以在编辑器中使用VB的对象资源管理器和/或智能感知来探索这些属性。 Also, consider what happens when NO item is selected in the ListView: you can also check this by putting a breakpoint on a line that assigns SelectedItem to a variable, and then use the debugger to inspect it after you run your app and click the button without first selecting an item in the listbox (in an actual application, you would usually disable the button until an item had been selected, but let's not get ahead of ourselves here...) 另外,考虑在ListView中选择NO项时会发生什么:您还可以通过在将SelectedItem分配给变量的行上放置断点来检查这一点,然后在运行应用程序并单击按钮后使用调试器进行检查没有先选择列表框中的项目(在实际应用程序中,您通常会禁用该按钮,直到选择了一个项目,但让我们不要超越自己......)

  • ListView1 also has a collection that represents all items in it: it's called ListItems, and has several useful properties and methods (such as .Remove...) as well, ready for you to explore using F2 or Intellisense ListView1还有一个表示其中所有项目的集合:它被称为ListItems,并且还有几个有用的属性和方法(例如.Remove ...),随时可供您使用F2或Intellisense进行探索

  • To ask the user if he/she is really sure about the whole removal thing, look into the MessageBox function: this function is a bit tricky, since it maps pretty directly to the underlying Windows API call, but the general idea is that you pass in some flag values (by adding them together) to indicate what kind of message box you want (icon- and button-wise). 要询问用户他/她是否真的确定整个删除操作,请查看MessageBox函数:这个函数有点棘手,因为它直接映射到底层的Windows API调用,但一般的想法是你通过在一些标志值中(通过将它们加在一起)来指示您想要的消息框类型(图标和按钮方式)。 You then check the return value to see which button the user selected. 然后检查返回值以查看用户选择的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM