List Box Help

Discussion in 'Programming & Scripting' started by Nothinbeter2do, Jan 12, 2009 with 16 replies and 243 views.

  1. Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Alright, lets say I have a list box on my form that has the text Se7ensins, Halo Modding, and GoW modding. I also have this code:


    Code:
     richTextBox1.Text = richTextBox1.Text + " " + listBox1.SelectedIndex;
    Now if I already had the text Se7ensins in the text box, once I click my button it will say Se7ensins-1. Then if I select another index it will add -1 again to my rich text box. But what I want it to add is Se7ensins or GoW modding.

    How can I do this?
     
  2. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    uhh

    listBox1.Items.Add("add string here");

    ?
     
  3. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    In the item collection I have Se7ensins, Halo Modding and GoW Modding. But when I use the above code it just adds "-1" to my text box.
     
  4. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    try

    richTextBox1.Text += richTextBox1.Text + " " + listBox1.SelectedIndex;

    or

    richTextBox1.Text = richTextBox1.Text += " " + listBox1.SelectedIndex;

    or something like

    string str;
    str = richTextBox1.Text;

    richTextBox1.Text = str+" " + listBox1.SelectedIndex.ToString();
     
  5. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Use selectedItem rather than selectedIndex
     
  6. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269

    Ah, that is what he wants, I didn't read clearly, listen to him, xD.


    What's this for, anyway?
     
  7. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    Have you even checked your code superaison? That's not the way to go....

    listBox1.SelectedIndex is an integer that returns which index is currently selected. What he's looking for is the text that is currently selected.

    Here is some code that will work:
    EDIT: oops, had this page open for a bit before I answered. Didn't notice you guys already solved it for him.
     
  8. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    As I just said, I didn't read exactly what he wanted, so not my code, but what he wanted :tongue:
     
  9. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Using the code:

    Code:
    richTextBox1.Text = richTextBox1.Text + " " + listBox1.SelectedItem;
    doesn't work. When I click my button nothing happens.
     
  10. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    Do


    richTextBox1.Text = richTextBox1.Text + " " + listBox1.SelectedItem.ToString();


    or

    richTextBox1.Text += richTextBox1.Text + " " + listBox1.SelectedItem.ToString();
     
  11. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    When using those I get

    Code:
    Object reference not set to an instance of an object.
     
  12. superaison

    superaison Banned

    Messages:
    1,404
    Ratings:
    269
    grr


    i'll open C# now.


    richTextBox1.Text += " " + listBox1.SelectedItem.ToString();
     
  13. OP
    Nothinbeter2do

    Nothinbeter2do Banned

    Messages:
    2,425
    Ratings:
    526
    Alright, this just isn't working for me. :frown:
     
  14. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
  15. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    k

    first of all, dont do this:

    Code:
    richTextBox1.Text = richTextBox1.Text + " " + listBox1.SelectedIndex; 
    it should be like this

    Code:
    richTextBox1.Text += (" " + listBox1.SelectedIndex.ToString());
    i used () to make it look better.

    second of all, if your trying to add what the user selected and the index it was then try:

    Code:
    string item = listBox1.SelectedItem.ToString();
    int index = listBox1.SelectedIndex.ToString();
    
    richTextBox1.Text += (" " + (item + ("[" + index + "]") ) );
    
    once again I used () to make it more orginized. Now this is all top of my head. The code should work fine. But you may have to change somthing, idk.

    but if im correct it should add somthing that looks like

    example
    Code:
    GoW Modding[2]
     
  16. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    Edit: Nvm I was thinking of comboBoxes
     
  17. zadesa

    zadesa Enthusiast

    Messages:
    75
    Ratings:
    43
    Cause listbox doesnt have that feature >_<