开发者

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

How do I delete a row in a ListView. I need to select the row to be deleted and a command button will delete it with a alert 开发者_C百科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:

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 (e.g. ListView1) and a Button control (e.g. 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).

Now, you'll need to put some code in the DeleteRow_Click event. Some hints:

  • The currently selected row (item) in the ListView is 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. 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...)

  • 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

  • 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). You then check the return value to see which button the user selected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜