0
這是我的代碼。我想對列表框進行排序名稱&在列表框上顯示它們。 我使用Hashtable類來搜索Items.So現在我不Konw如何排序我的列表! 我創建了一個名爲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", "[email protected]");
CreateCustomer("auser 2", "customer 2", "[email protected]");
CreateCustomer("buser 3", "customer 3", "[email protected]");
CreateCustomer("guser 4", "customer 4", "[email protected]");
CreateCustomer("euser 5", "customer 5", "[email protected]");
CreateCustomer("huser 6", "customer 6", "[email protected]");
CreateCustomer("duser 7", "customer 7", "[email protected]");
CreateCustomer("fuser 8", "customer 8", "[email protected]");
}
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)
{
//
}
我不相信你的代碼甚至編譯。 'lstCustomers'是什麼?它在哪裏宣佈? –
這是一個來自Winforms應用程序的部分類 - UI控件可能在另一個文件的生成代碼中聲明。 –