Trouble modifying text after copying items between listboxes

Discussion in 'ASP .Net Web Controls' started by AlBruAn, Jul 16, 2007.

  1. AlBruAn

    AlBruAn Guest

    I have an application under development that contains, among other controls,
    two listboxes containing Available Tasks and Assigned Tasks. Let's say one
    of the Available Tasks is "Send Letter" and the user wishes to copy it over
    to the Assigned Tasks listbox; in that case, it's a simple matter of
    executing the following line of code:


    lbAssignedTasks.Items.Add(lbAvailableTasks.Items(lbAvailableTasks.SelectedIndex()))

    Let's further state that a few other tasks are assigned and then the user
    wants to assign sending a follow-up letter by again selecting "Send Letter".
    In this case, I want to modify the first instance of "Send Letter" to read
    "Send Letter - 1" and modify the second instance of "Send Letter" to read
    "Send Letter - 2". I've attempted to set the text for the added instance by
    setting it as follows:

    lbAssignedTasks.Items(lbAssignedTasks.Items.Count - 1).Text =
    lbAssignedTasks.Items(lbAssignedTasks.Count - 1).Text + " - " + CType(2,
    String)

    and I've attempted to set the text for the first instance by setting it as
    follows:

    lbAssignedTasks.Items(i).Text = lbAssignedTasks.Items(i).Text + " - " +
    CType(1, String)

    The value for i in both statements is determined by stepping through the
    list of Assigned Tasks working from the last item to the first until I find
    an item that .Contains("Send Letter").

    As far as I can tell, that part seems to be working correctly, but modifying
    the text value for an item in the Assigned Tasks listbox is somehow being
    "sent back" to the Available Tasks listbox and modifying the text for "Send
    Letter" to also be "Send Letter - 2".

    Why is this happening and is there a workaround solution to my problem?
     
    AlBruAn, Jul 16, 2007
    #1
    1. Advertising

  2. AlBruAn

    AlBruAn Guest

    You're answer worked perfectly. The only other thing I had to do was grab
    the SelectedValue of the Available Task that was selected and reset the Value
    property of the newly-assigned task after assigning it.


    "David R. Longnecker" wrote:

    > If I'm understanding your question, I believe the issue is because your objects
    > are reference types--and when you change one, it changes them all.
    >
    > > lbAssignedTasks.Items.Add(lbAvailableTasks.Items(lbAvailableTasks.SelectedIndex()))

    >
    > Here, your AssignedTasks list box is adding a the reference to the selected
    > ListItem in AvailableTasks; not creating a new item. You could try newing
    > up a ListItem before you add it, creating a new reference. I have the text
    > change (the second line of code) just to verify that it changes only in one
    > place--not both.
    >
    > Dim newItem As New ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedValue)
    > newItem.Text = "Moved to Assigned Tasks - " + newItem.Text
    > DropDownList2.Items.Add(newItem)
    >
    > HTH.
    >
    > -dl
    >
    > --
    > David R. Longnecker
    > http://blog.tiredstudent.com
    >
    > > I have an application under development that contains, among other
    > > controls, two listboxes containing Available Tasks and Assigned Tasks.
    > > Let's say one of the Available Tasks is "Send Letter" and the user
    > > wishes to copy it over to the Assigned Tasks listbox; in that case,
    > > it's a simple matter of executing the following line of code:
    > >
    > > lbAssignedTasks.Items.Add(lbAvailableTasks.Items(lbAvailableTasks.Sele
    > > ctedIndex()))
    > >
    > > Let's further state that a few other tasks are assigned and then the
    > > user wants to assign sending a follow-up letter by again selecting
    > > "Send Letter". In this case, I want to modify the first instance of
    > > "Send Letter" to read "Send Letter - 1" and modify the second instance
    > > of "Send Letter" to read "Send Letter - 2". I've attempted to set the
    > > text for the added instance by setting it as follows:
    > >
    > > lbAssignedTasks.Items(lbAssignedTasks.Items.Count - 1).Text =
    > > lbAssignedTasks.Items(lbAssignedTasks.Count - 1).Text + " - " +
    > > CType(2, String)
    > >
    > > and I've attempted to set the text for the first instance by setting
    > > it as follows:
    > >
    > > lbAssignedTasks.Items(i).Text = lbAssignedTasks.Items(i).Text + "
    > > - " + CType(1, String)
    > >
    > > The value for i in both statements is determined by stepping through
    > > the list of Assigned Tasks working from the last item to the first
    > > until I find an item that .Contains("Send Letter").
    > >
    > > As far as I can tell, that part seems to be working correctly, but
    > > modifying the text value for an item in the Assigned Tasks listbox is
    > > somehow being "sent back" to the Available Tasks listbox and modifying
    > > the text for "Send Letter" to also be "Send Letter - 2".
    > >
    > > Why is this happening and is there a workaround solution to my
    > > problem?
    > >

    >
    >
    >
     
    AlBruAn, Jul 17, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Ternier
    Replies:
    1
    Views:
    562
    John Saunders
    Jul 25, 2004
  2. =?Utf-8?B?U2NvdXRMZWU=?=

    Pass Listitem between Listboxes

    =?Utf-8?B?U2NvdXRMZWU=?=, Sep 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    400
    =?Utf-8?B?U2NvdXRMZWU=?=
    Sep 20, 2004
  3. florin
    Replies:
    2
    Views:
    4,954
    Karl Seguin
    Mar 4, 2005
  4. Replies:
    0
    Views:
    319
  5. ASPirant

    moving items between two listboxes

    ASPirant, Nov 22, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    102
    ASPirant
    Nov 22, 2005
Loading...

Share This Page