Results 1 to 4 of 4

Thread: Adding text to Listbox at specific index

  1. #1
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    0

    Default Adding text to Listbox at specific index

    I have a form with a listbox on it and I want to add a piece of text "AAA" to the listbox when I click a button. When the listbox.selectedindex = -1 (nothing selected) the text adds perfectly to the end of the list but now I want to be able to click an item in the list thus selecting a listbox index and then click the button to add "AAA" to the same index position I have selected. All the samples I've read merely state how to add an item to the listbox:

    Listbox1.Items.Add (Textstring)

    but nothing tells me how to add an item at a specific index in the list. Can someone give me a simple code snipet of how to do this?

  2. #2
    Active Member
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    Login to Give a bone
    0

    Default Re: Adding text to Listbox at specific index


  3. #3
    Active Member
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    Login to Give a bone
    0

    Default Re: Adding text to Listbox at specific index

    Perhaps something like :-
    Code:
    // Codehimbelonga kdub
            private void button1_Click(object sender, EventArgs e)
            {
                string tbVal = (textBox1.Text);
                if (String.IsNullOrEmpty(tbVal.Trim()) == false)
                {
                    if (listBox1.SelectedIndices.Count > 0)
                    {
                        int selectedIndex = listBox1.SelectedIndices[0];
                        // if before selected
                        if (this.radioButton1.Checked)
                        {
                            listBox1.Items.Insert(selectedIndex, tbVal);
                        }
                            // if after selected
                        else if (this.radioButton2.Checked)
                        {
                            listBox1.Items.Insert((selectedIndex + 1), tbVal);
                        }
                    }
                    else
                    {
                        listBox1.Items.Add(tbVal);
                    }
                }
            }

  4. #4
    All AUGI, all the time
    Join Date
    2003-10
    Posts
    706
    Login to Give a bone
    0

    Default Re: Adding text to Listbox at specific index

    Ahhh, so it's INSERT instead of ADD. That's why it's so difficult to learn VB.NET because in VBA you can specify the index with the ADD method. I searched everywhere on the internet about how to ADD a piece of text to a listbox at a certain index and came up with nothing... and now I know why. Thanks for the code and the clues. It does what I want now.

Similar Threads

  1. Adding an empty row in Sheet List index
    By sovby254640 in forum AutoCAD Customization
    Replies: 7
    Last Post: 2016-05-17, 06:34 PM
  2. question about adding decal to a wall in a specific place
    By yassineboukhabza276887 in forum Revit - API
    Replies: 0
    Last Post: 2011-07-28, 09:02 PM
  3. question about adding decal to a wall in a specific place
    By yassineboukhabza276887 in forum Revit Architecture - General
    Replies: 0
    Last Post: 2011-07-28, 08:32 PM
  4. Dim Text on Specific Layer instead of having Specific Color
    By Masroor Javid in forum ACA Wish List
    Replies: 0
    Last Post: 2009-02-23, 09:34 AM
  5. Replies: 7
    Last Post: 2006-10-21, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •