3 Replies - 1072 Views - Last Post: 17 February 2015 - 07:25 PM

#1 fucode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-September 14

sort objects in ListBox

Posted 16 February 2015 - 10:24 PM

Hi Guys!, this is my second hw doing C# and I'm trying to sort my objects I have in the ListBoxes,when I remove them from the ListBox on the right then get added to the original ListBox(because when i click add it removes the item and sends them to the ListBox on the right),but when i add them the go at the end of the list and they order should matter,how can i sort the list after the were added back?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//Patricio Vargas
public partial class _default : System.Web.UI.Page
{
    int hours = 0;
    int cost = 0;
    int limit = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        buildAvailableCourseList().ToArray<ListItem>();
        if (!Page.IsPostBack)        //For initial page creation
        {
            ListItem[] availableCourses = buildAvailableCourseList();
            lbxAvailableClasses.DataSource = availableCourses;
            lbxAvailableClasses.DataTextField = "Text";
            lbxAvailableClasses.DataValueField = "Value";
            lbxAvailableClasses.DataBind();
        }
        

    }
    private ListItem[] buildAvailableCourseList()
    {
        ListItem[] tempList = { new ListItem("CS 1301-4", "CS 1301-4"),
                                new ListItem("CS 1302-4", "CS 1302-4"),
                                new ListItem("CS 1303-4", "CS 1303-4"),
                                new ListItem("CS 2202-2", "CS 2202-2"),
                                new ListItem("CS 2224-2", "CS 2224-2"),
                                new ListItem("CS 3300-3", "CS 3300-3"),
                                new ListItem("CS 3301-1", "CS 3301-1"),
                                new ListItem("CS 3302-1", "CS 3302-1"),
                                new ListItem("CS 3340-3", "CS 3340-3"),
                                new ListItem("CS 4321-3", "CS 4321-3"),
                                new ListItem("CS 4322-3", "CS 4322-3")
                              };
        return tempList;
    }

    protected void addBtn_Click(object sender, EventArgs e)
    {
        String[] textD = new String[3];
        String[] text = new String[3];
        String[] temp1 = new String[3];
        var selected = new List<ListItem>();
        foreach (ListItem item in lbxAvailableClasses.Items)
        {
            if (item.Selected && limit<= 19)
            {
                selected.Add(item);
                lbxRegisterClasses.Items.Add(item);
            }
            
        }
        foreach (ListItem item in selected)
        {
            lbxAvailableClasses.Items.Remove(item);
        }
        foreach (ListItem item in lbxRegisterClasses.Items)
        {
            textD = item.ToString().Split(' ');
            temp1 = textD[1].Split('-');
            hours += int.Parse(temp1[1]);
            cost = hours * 1000;
            limit += hours;
        }
        lblHours.Text = hours.ToString();
        lblCost.Text = cost.ToString();
    }
   
    protected void removetbtn_Click(object sender, EventArgs e)
    {
        ArrayList q = new ArrayList();
        var selected = new List<ListItem>();
        
        foreach (ListItem item in lbxRegisterClasses.Items){
        
            if (item.Selected)
            {
                
                selected.Add(item);
                lbxAvailableClasses.Items.Add(item);
            }
        }                       
        foreach (ListItem item in selected)
        {
            lbxRegisterClasses.Items.Remove(item);
        }
        foreach (ListItem item in lbxRegisterClasses.Items)
        {
            String[] textD = new String[3];
            String[] text = new String[3];
            String[] temp1 = new String[3];
            textD = item.ToString().Split(' ');
            temp1 = textD[1].Split('-');
            hours += int.Parse(temp1[1]);
            cost = hours * 1000;
            limit += hours;
        }
        lblHours.Text = hours.ToString();
        lblCost.Text = cost.ToString();
    }
    protected void resetbtn_Click(object sender, EventArgs e)
    {
        Session["ViewState"] = null;
        Response.Redirect("~/default.aspx"); 
        

    
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: sort objects in ListBox

#2 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: sort objects in ListBox

Posted 17 February 2015 - 03:00 AM

There is a built in sorted property.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,265
  • Joined: 05-May 12

Re: sort objects in ListBox

Posted 17 February 2015 - 06:59 AM

Moving to ASP.NET...
Was This Post Helpful? 0
  • +
  • -

#4 fucode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-September 14

Re: sort objects in ListBox

Posted 17 February 2015 - 07:25 PM

View PostMichael26, on 17 February 2015 - 03:00 AM, said:

There is a built in sorted property.

lbxAvailableClasses.Items.Sort()?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1