CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    12

    Trim ComboBox items

    I want to trim all ComboBox items for example:

    ComboBox items

    Processor:12000
    Ram:5000


    I want to trim string before colon (including 'colon') and send to listbox, so i can calculate all prices for items.
    So plz tell me how can i do that.

    Thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2012
    Posts
    17

    Re: Trim ComboBox items

    Quote Originally Posted by osadalakimdi View Post
    I want to trim all ComboBox items for example:

    ComboBox items

    Processor:12000
    Ram:5000


    I want to trim string before colon (including 'colon') and send to listbox, so i can calculate all prices for items.
    So plz tell me how can i do that.

    Thanks
    use this

    Code:
    String data = "Processor:12000";
    String result = data.substring(data.lastIndexOf(":")+1);
    we substring the String. String.substring([b[This is the Start Index[/b])
    so data.lastIndexOf(":") -> return the index of last : found in the string..
    and return ":12000" but we need only "12000" so +1 for the index

    update:
    sorry i dont see that you want to calculate. so

    try this code instead

    Code:
    int total=0;
    foreach (String text in [your list].Items) {
         total+= int32.Parse(text.Substring(text.LastIndexOf(":")+1));
    }
    Last edited by Neolitz; March 17th, 2012 at 01:13 AM.

  3. #3
    Join Date
    Mar 2012
    Posts
    12

    Re: Trim ComboBox items

    ThankYou so much Neolitz, Your code works

    I use this:

    string substring;
    comboBox1.Items.ToString();
    foreach(string s in comboBox1.Items)
    {

    substring = s.Substring(s.LastIndexOf(":") + 1);
    listBox1.Items.Add(substring);
    }

    so now my problem is when i hit ">" button for add only selected item, all items of combobox trim and tranfer to listbox.
    this code works for all item button(>>) button but not for selected item(">")

    please help me.
    Attached Images Attached Images

  4. #4
    Join Date
    Mar 2012
    Posts
    17

    Re: Trim ComboBox items

    Quote Originally Posted by osadalakimdi View Post
    ThankYou so much Neolitz, Your code works

    I use this:

    string substring;
    comboBox1.Items.ToString();
    foreach(string s in comboBox1.Items)
    {

    substring = s.Substring(s.LastIndexOf(":") + 1);
    listBox1.Items.Add(substring);
    }

    so now my problem is when i hit ">" button for add only selected item, all items of combobox trim and tranfer to listbox.
    this code works for all item button(>>) button but not for selected item(">")

    please help me.
    the foreach will work with >>
    now for the > try to do this

    instead of do it to all data in the combo box we will do to the item selected and insert it to the list

    Code:
    String temp = comboBox1.Text;
    temp = temp.Substring(temp .LastIndexOf(":") + 1);
    listBox1.Items.add(temp);
    your mistake is you use "foreach" which means do to all data in combo box, when you only wanted 1 item to added when > pressed.

    so for >> keep using the old one
    and for > use the new one

    Hope this helps

  5. #5
    Join Date
    Mar 2012
    Posts
    12

    Re: Trim ComboBox items

    Yeeah!! ThankYou so much.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured