Thread: Delete Item

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    14

    Delete Item

    Please help! It's part of my assignment to delete a string from a list box when the string is selected and a button is clicked, but the darned textbook does not give an example of this! The project is 99% done except for this little piece. I figured out how to delete an item, but it wants a specific index location. Well, the item I want to delete may not always be at a specific location. Can you point me to a piece of code using delete item or delete string ASAP? Thank you!

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When the button is clicked send a LB_GETCURSEL message to your listbox. The SendMessage() function will return an index of the currently selected string which you can use as the WPARAM when you send a LB_DELETESTRING message to your listbox using SendMessage().
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    I just tried this:
    m_Orders.DeleteString(SendMessage(m_Orders.GetCurS el()));

    and it only deletes the string above the one that is selected.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Then do this...

    m_Orders.DeleteString(1 + SendMessage(m_Orders.GetCurSel()));

    That should fix the problem Hehe. An easy solution. If it always deletes the item above the selection then add one to the number returned to delete the selected item! Hope it's the right solution. I don't program in C++ I do C so I hope that is the right syntax for that. But if it isn't I think you can get the right idea from it.
    cerion
    Use the code Luke.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What happens if the first Item (or Last at +1) is clicked?

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you're using MFC then I don't think you'll have to use SendMesage(). Try -

    m_Orders.DeleteString(m_Orders.GetCurSel());
    zen

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    novacain,

    By what he explained the SendMessage is always returning the (SelectedItemNum - 1) so if the first is clicked it would most likely return 0, thus adding 1 will get you the first element. And if the last item is clicked it will most likely return (LastItemNum - 1) thus again, adding 1 to the returned number will get you the last item in the list.
    cerion
    Use the code Luke.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Cerion,
    Your solution will fix the problem. Not the cause.

    From my experience with Get_Cursel ect it returns the zero based index for the items in a ctrl.

    I was pointing out that there is probably a clash in the array indexing.

    The ctrl is using a zero based index and the list (programmer) a one based index. This is WHY it deletes the item below on the ctrl. If this is not the case then something more serious is going wrong.

    As this is homework, it is better to get the basics right, understand what is going wrong, not 'fudge' to fix the problem.

    PS EmilyH sounds feminine(?) or am I missing something

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    I did get it to work eventually. Thanks.

    Yes, I'm a female programming student. What's wrong with that? My GPA is 3.8, I'm a couple of months away from finishing an Associate's in C Programming. I also have a 4-year degree in an unrelated field.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868

    Nothing, great to see, Did not mean to offend

    Only Cerion used

    'he'

    [I work in an office with 100% male, would prefer 100% female. (maybe)]

    If you declare an array
    ie int MyArray[100]
    it has elements from 0 to 99

    if you index it 1 to 100 you will cause problems as the element indexed as 100 is outside the arrays memory.

    In debug mode this will not be obvious as you will probably only be over-writting debug info from the compiler. In release mode this will probably crash the program or worse.

    (an eye for detail stops many crashes before they happen)
    Last edited by novacain; 10-03-2001 at 09:30 PM.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    14
    No Problem. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM