这是我的代码。我想基于列表框排序名称&在列表框上显示这些内容。 我使用Hashtable类在Items之间进行搜索。所以我现在不知道如何排序我的列表! 我创建了一个名为Customer的类,并在我的代码中使用它! 非常感谢任何帮助: - )
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Structure_Demo
{
public partial class Form1 : Form
{
//Form level members.
public Hashtable objCustomers = new Hashtable();
public ArrayList objCustomer = new ArrayList();
public Form1()
{
InitializeComponent();
}
public Customer SelectedCustomer
{
get
{
//Return the selected customer
return (Customer)lstCustomers.Items[lstCustomers.SelectedIndex];
}
}
private void DisplayCustomer(Customer objCustomer)
{
//Display the customer detils in the form
txtName.Text = objCustomer.Name;
txtFirstName.Text = objCustomer.FirstName;
txtLastName.Text = objCustomer.LastName;
txtEmail.Text = objCustomer.Email;
}
private void btnTest_Click(object sender, EventArgs e)
{
// Create some customers
CreateCustomer("cuser 1", "customer 1", "cu.customer1@ssamail.com");
CreateCustomer("auser 2", "customer 2", "au.customer2@ssamail.com");
CreateCustomer("buser 3", "customer 3", "bu.customer3@ssamail.com");
CreateCustomer("guser 4", "customer 4", "gu.customer4@ssamail.com");
CreateCustomer("euser 5", "customer 5", "eu.customer5@ssamail.com");
CreateCustomer("huser 6", "customer 6", "hu.customer6@ssamail.com");
CreateCustomer("duser 7", "customer 7", "du.customer7@ssamail.com");
CreateCustomer("fuser 8", "customer 8", "fu.customer8@ssamail.com");
}
public void CreateCustomer(string FirstName, string LastName, string Email)
{
//Declare a customer object.
Customer objNewCustomer;
//Create new customer.
objNewCustomer.FirstName = FirstName;
objNewCustomer.LastName = LastName;
objNewCustomer.Email = Email;
//check if customer isn't currctly in the list
if (objCustomers.Contains(Email.ToLower()) == false)
{
//Add new customer to the list
objCustomers.Add(Email.ToLower(), objNewCustomer);
//Add new customer to the listbox control
lstCustomers.Items.Add(objNewCustomer);
}
else
{
MessageBox.Show("The customer :" + FirstName + LastName + " currently exit", "Structure Demo");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
//if no customer is selected if form list boe then...
if (lstCustomers.SelectedIndex == -1)
{
//Display a message
MessageBox.Show("You must select a customer to delete!","Structure Demo");
//Exit the method
return;
}
//promt the user to delete the selected customer
DialogResult result = MessageBox.Show("Are you sure to delete the " + SelectedCustomer.Name + "?", "Structure Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//Get the customer to be deleted
Customer objCustomerToDelete = SelectedCustomer;
//remove the customer from Arraylist
objCustomers.Remove(txtEmail.Text.ToLower());
//remove the customer from the list box
lstCustomers.Items.Remove(objCustomerToDelete);
}
}
private void btnLookup_Click(object sender, EventArgs e)
{
//if customer found in hashtable
if (objCustomers.Contains(txtEmail.Text.ToLower()) == true)
//Display the customer name
MessageBox.Show("The customer name is: " + ((Customer)objCustomers[txtEmail.Text.ToLower()]).Name, "Structure Demo");
else
//Display an error message
MessageBox.Show("The customer with email address: " + txtEmail.Text + " does not exit!", "Structure Demo");
}
private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
// Display the customer details
DisplayCustomer(SelectedCustomer);
}
private void btnSort_Click(object sender, EventArgs e)
{
//
}
答案 0 :(得分:1)
您无法直接对HashTable进行排序,但您可以使用SortedDictionary作为替代方法,或者维护HashTable键列表中所需的排序顺序。