让所有列表条目出现在第二个 wpf 应用程序上
Getting all list entries to appear on a second wpf app
我为 uni 编写了一个简单的程序,它在客户对象中保存客户的详细信息,然后将每个存储在列表中。
应用程序本身按预期运行并验证其应有的方式。
但是我有一项高级任务要求我将所有客户及其所有详细信息输出到另一个 wpf 应用程序 window 的列表框中。我试图找到如何去做,但找不到任何像样的教程或任何可以帮助我在任何地方弄清楚的类似内容。我的代码允许我在第一个 window 的消息框中一个接一个地查看每个人,但这没有用。任何人都可以帮助如何做这样的事情吗?
这是应用程序:
这是我的按钮和文本框代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BusinessObjects;
//Euan Sutherland
//Class for button/textbox methods. Allowing data from form to be passed to the list and saved. Or for customers details to be found/deleted.
//29/10/2018
namespace Demo
{
public partial class MainWindow : Window
{
//List to hold details of each customer.
private MailingList store = new MailingList();
//Variable to set starting ID number.
private int ID = 10001;
//Initializing the window to display labels,textboxes,buttons,listbox.
public MainWindow()
{
InitializeComponent();
}
//Button for Adding a customer with a specific assigned ID number.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
//Testing for errors in textboxes with IF statements.
try
{
//Object to hold details for each customer.
Customer cust = new Customer();
//If statements that catches errors within textboxes.
//First name textbox empty, longer than 15 characters or including numbers.
if (txtFName.Text == "" || txtFName.Text.Length > 15 || Regex.IsMatch(txtFName.Text, @"^[0-9]+$"))
{
throw new Exception();
}
//Second name textbox empty, longer than 25 characters or including numbers.
else if (txtSName.Text == "" || txtSName.Text.Length > 25 || Regex.IsMatch(txtSName.Text, @"^[0-9]+$"))
{
throw new Exception();
}
//Email textbox empty and checking whether in email format.
else if (txtEmail.Text == "" || !Regex.IsMatch(txtEmail.Text, @"^(?("")("".+?(?<!\)""@)|(([0-9a-z]((\.(?!\.))|[-!#$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"))
{
throw new Exception();
}
//Skype textbox empty or longer than 32 characters.
else if (txtSkype.Text == "" || txtSkype.Text.Length > 32)
{
throw new Exception();
}
//telephone textbox empty, longer than 11 characters or including any alphabetical characters.
else if (txtTelephone.Text == "" || txtTelephone.Text.Length > 11 || Regex.IsMatch(txtTelephone.Text, @"^[a-zA-Z]+$"))
{
throw new Exception();
}
//Preferred contact texbox empty, or does not match email/skype/tel.
else if (txtPrefContact.Text == "" || !Regex.IsMatch(txtPrefContact.Text, @"^(?:tel|email|skype)+$"))
{
throw new Exception();
}
//Make sure the ID number does not go higher than 50000. If it does, error message is displayed and no entry is added.
try
{
cust.ID = ID;
}
catch (Exception excep)
{
//Display error about ID number going over 50000 and then exit method.
MessageBox.Show(excep.Message);
return;
}
//Stores the details of the textboxes into the customer details for each list entry.
cust.FirstName = txtFName.Text;
cust.SecondName = txtSName.Text;
cust.Email = txtEmail.Text;
cust.SkypeID = txtSkype.Text;
cust.Telephone = txtTelephone.Text;
cust.PrefContact = txtPrefContact.Text;
//Adds the details of the person above to the list and increases the ID number by one for the next entry.
store.add(cust);
ID++;
//Adds the customer to the listbox as an object with their ID number, first name and second name.
lbxCustomers.Items.Add(new ListBoxItem
{
//assigns a tag as an ID number given to it from the customers specific ID number.
Tag = cust.ID,
Content = cust.ID + " " + cust.FirstName + " " + cust.SecondName
});
//Shows user the details of the person added to the list.
MessageBox.Show("Added to list:" +
"\nName: " + cust.FirstName + " " + cust.SecondName +
"\nEmail: " + cust.Email +
"\nSkype: " + cust.SkypeID +
"\nTelephone: " + cust.Telephone +
"\nPreferred Contact: " + cust.PrefContact +
"\nID No: " + cust.ID);
//Clears all textboxes for next entry after adding a customer.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
}
catch
{
//IF statements that displays errors after catching them
//First name textbox empty, longer than 15 characters or including numbers.
if (txtFName.Text == "" || txtFName.Text.Length > 15 || Regex.IsMatch(txtFName.Text, @"^[0-9]+$"))
{
MessageBox.Show("You must enter a first name within 15 characters." +
"\nNo numbers allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Second name textbox empty, longer than 25 characters or including numbers.
else if (txtSName.Text == "" || txtSName.Text.Length > 25 || Regex.IsMatch(txtSName.Text, @"^[0-9]+$"))
{
MessageBox.Show("You must enter a second name within 25 characters." +
"\nNo numbers allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Email textbox empty and checking whether in email format.
else if (txtEmail.Text == "" || !Regex.IsMatch(txtEmail.Text, @"^(?("")("".+?(?<!\)""@)|(([0-9a-z]((\.(?!\.))|[-!#$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"))
{
MessageBox.Show("You haven't entered a valid email address format.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Skype textbox empty or longer than 32 characters.
else if (txtSkype.Text == "" || txtSkype.Text.Length > 32)
{
MessageBox.Show("You haven't entered a valid Skype address." +
"\nMust be within 32 letters and numbers.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//telephone textbox empty, longer than 11 characters or including any alphabetical characters.
else if (txtTelephone.Text == "" || txtTelephone.Text.Length > 11 || Regex.IsMatch(txtTelephone.Text, @"^[a-zA-Z]+$"))
{
MessageBox.Show("You must enter an 11 digit phone number." +
"\nNo Letters allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Preferred contact texbox empty, or does not match email/skype/tel.
else if (txtPrefContact.Text == "" || !Regex.IsMatch(txtPrefContact.Text, @"^(?:tel|email|skype)+$"))
{
MessageBox.Show("Please enter a preferred contact method." +
"\nEither 'email' 'tel' 'phone'");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
}
}
//Button for deleting a specific customer with their specific ID.
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
//Tests whether something was entered to the ID textbox
try
{
//Allows the text in the ID textbox to be changed to an integer to be used for checking the ID.
int id = Int32.Parse(txtID.Text);
/*If the ID number entered is not found in the list,
an error message is displayed to say the customer does not exist.
If the ID does exist, deletes that customer. */
if (store.find(id) == null)
{
//Displays error message to tell user that this customer does not exist.
MessageBox.Show("Invalid ID!" +
"\nNo Customer with this ID exists!");
}
else
{
//Sets a temporary listboxitem.
ListBoxItem itemToDelete = null;
//goes through the items in the listbox and checks the id in the textbox against the listbox entries.
//Once it has found the correct entry, stores it in the temporary listboxitem and breaks from the loop.
foreach (ListBoxItem item in lbxCustomers.Items)
{
if ((int)item.Tag == id)
{
itemToDelete = item;
break;
}
}
//If the temporary list item is not null, delete the entry with id entered to the ID textbox.
if (itemToDelete != null)
{
lbxCustomers.Items.Remove(itemToDelete);
}
//Displays the details of the customer deleted
store.ShowDetails(id);
//Delete the details of customer with specific ID.
store.delete(id);
}
}
catch
{
//Displays error to say nothing was entered to the ID textbox and exits from the function.
MessageBox.Show("You did not enter a correct ID!");
return;
}
}
//Button for Finding a specific customer with their specific ID.
private void btnFind_Click(object sender, RoutedEventArgs e)
{
//Tests whether something was entered to the ID textbox
try
{
//Allows the text in the ID textbox to be changed to an integer to be used for checking the ID.
int id = Int32.Parse(txtID.Text);
/*If the ID number entered is not found in the list,
an error message is displayed to say the user does not exist.
If the ID does exist, shows the user the details of the person with that ID. */
if (store.find(id) == null)
{
//Displays error message to tell user that this customer does not exist.
MessageBox.Show("Invalid ID!" +
"\nNo Customer with this ID exists!");
}
else
{
//Displays the details of customer with specific ID.
store.ShowDetails(id);
}
}
catch
{
//Displays error to say nothing was entered to the ID textbox and exits from the function.
MessageBox.Show("You did not enter a correct ID!");
return;
}
}
//Button for showing all customers and their details on next form.
private void btnShwAll_Click(object sender, RoutedEventArgs e)
{
store.ShowAllDetails();
}
//Allows the details of a customer clicked on in the list to be displayed.
private void lbxCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the currently selected item in the ListBox, convert it to a string and then cut off the name leaving only the id number.
int id = (int)((ListBoxItem)lbxCustomers.SelectedItem).Tag;
//Shows the user the selected customers full details.
store.ShowDetails(id);
}
}
}
这是我的客户class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Euan Sutherland
//Class that gets/sets the specific details variables for each customer in the mailinglist.
//29/10/2018
namespace BusinessObjects
{
public class Customer
{
//Variables to hold each specific detail for each customer for adding to the list.
private int _customerID;
private string _telephone;
private string _firstName;
private string _secondName;
private string _email;
private string _skypeID;
private string _prefContact;
//Get/Set for using ID value.
public int ID
{
get
{
return _customerID;
}
set
{
if(value > 50000)
{
throw new ArgumentException("ID number too large!");
}
_customerID = value;
}
}
//Get/Set for using First Name value.
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
//Get/Set for using Second Name value.
public string SecondName
{
get
{
return _secondName;
}
set
{
_secondName = value;
}
}
//Get/Set for using Skype value.
public string SkypeID
{
get
{
return _skypeID;
}
set
{
_skypeID = value;
}
}
//Get/Set for using Telephone value.
public string Telephone
{
get
{
return _telephone;
}
set
{
_telephone = value;
}
}
//Get/Set for using Email value.
public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
//Get/Set for using preferred contact value.
public string PrefContact
{
get
{
return _prefContact;
}
set
{
_prefContact = value;
}
}
//Creates a string with all the specific details of a customer.
public string PrintDetails()
{
return "Found:" +
"\nName: " + FirstName + " " + SecondName +
"\nEmail: " + Email +
"\nSkype: " + SkypeID +
"\nTelephone: " + Telephone +
"\nPreferred Contact: " + PrefContact +
"\nID No: " + ID;
}
public string printAllDetails()
{
return "Found:" +
"\nName: " + FirstName + " " + SecondName +
"\nEmail: " + Email +
"\nSkype: " + SkypeID +
"\nTelephone: " + Telephone +
"\nPreferred Contact: " + PrefContact +
"\nID No: " + ID;
}
}
}
这是我的邮件列表 class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Euan Sutherland
//Class that holds the functions for adding customers to the list. Or to search through the list with specific ID numbers to find specific customers.
//29/10/2018
namespace BusinessObjects
{
public class MailingList
{
//List for holding each customer.
private List<Customer> _list = new List<Customer>();
//Adds a new customer with their details to the list.
public void add(Customer newCustomer)
{
_list.Add(newCustomer);
}
//Searches through the list of customers and matches the ID given to the function to find that customer and return their details.
public Customer find(int id)
{
//Searches through each customer in list.
foreach (Customer c in _list)
{
//checks each customers ID against the ID given to find.
if (id == c.ID)
{
//Returns the customer object with the correct ID given.
return c;
}
}
return null;
}
//Deletes the customer with specific ID.
public void delete(int id)
{
//Sets up a new customer object to search through the list with ID given.
Customer c = this.find(id);
if (c != null)
{
//Removes the customer with ID given.
_list.Remove(c);
}
}
public List<int> ids
{
get
{
List<int> res = new List<int>();
foreach(Customer p in _list)
res.Add(p.ID);
return res;
}
}
//Searches through the list to find the correct customer details to display with ID given.
public void ShowDetails(int id)
{
//Searches through each customer in list.
foreach (Customer c in _list)
{
//If list item ID matches ID given.
if (c.ID == id)
{
//Display the details of that specific customer.
MessageBox.Show(c.PrintDetails());
}
}
}
public void ShowAllDetails()
{
foreach (Customer c in _list)
{
MessageBox.Show(c.printAllDetails());
}
}
}
}
您可以将它放在用户控件中,而不是在 MainWindow 中使用 code/UI。稍后在新应用程序中,您引用演示项目的 dll 并访问新 wpf 应用程序主 window.
中的用户控件
希望对您有所帮助。
我为 uni 编写了一个简单的程序,它在客户对象中保存客户的详细信息,然后将每个存储在列表中。
应用程序本身按预期运行并验证其应有的方式。
但是我有一项高级任务要求我将所有客户及其所有详细信息输出到另一个 wpf 应用程序 window 的列表框中。我试图找到如何去做,但找不到任何像样的教程或任何可以帮助我在任何地方弄清楚的类似内容。我的代码允许我在第一个 window 的消息框中一个接一个地查看每个人,但这没有用。任何人都可以帮助如何做这样的事情吗?
这是应用程序:
这是我的按钮和文本框代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BusinessObjects;
//Euan Sutherland
//Class for button/textbox methods. Allowing data from form to be passed to the list and saved. Or for customers details to be found/deleted.
//29/10/2018
namespace Demo
{
public partial class MainWindow : Window
{
//List to hold details of each customer.
private MailingList store = new MailingList();
//Variable to set starting ID number.
private int ID = 10001;
//Initializing the window to display labels,textboxes,buttons,listbox.
public MainWindow()
{
InitializeComponent();
}
//Button for Adding a customer with a specific assigned ID number.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
//Testing for errors in textboxes with IF statements.
try
{
//Object to hold details for each customer.
Customer cust = new Customer();
//If statements that catches errors within textboxes.
//First name textbox empty, longer than 15 characters or including numbers.
if (txtFName.Text == "" || txtFName.Text.Length > 15 || Regex.IsMatch(txtFName.Text, @"^[0-9]+$"))
{
throw new Exception();
}
//Second name textbox empty, longer than 25 characters or including numbers.
else if (txtSName.Text == "" || txtSName.Text.Length > 25 || Regex.IsMatch(txtSName.Text, @"^[0-9]+$"))
{
throw new Exception();
}
//Email textbox empty and checking whether in email format.
else if (txtEmail.Text == "" || !Regex.IsMatch(txtEmail.Text, @"^(?("")("".+?(?<!\)""@)|(([0-9a-z]((\.(?!\.))|[-!#$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"))
{
throw new Exception();
}
//Skype textbox empty or longer than 32 characters.
else if (txtSkype.Text == "" || txtSkype.Text.Length > 32)
{
throw new Exception();
}
//telephone textbox empty, longer than 11 characters or including any alphabetical characters.
else if (txtTelephone.Text == "" || txtTelephone.Text.Length > 11 || Regex.IsMatch(txtTelephone.Text, @"^[a-zA-Z]+$"))
{
throw new Exception();
}
//Preferred contact texbox empty, or does not match email/skype/tel.
else if (txtPrefContact.Text == "" || !Regex.IsMatch(txtPrefContact.Text, @"^(?:tel|email|skype)+$"))
{
throw new Exception();
}
//Make sure the ID number does not go higher than 50000. If it does, error message is displayed and no entry is added.
try
{
cust.ID = ID;
}
catch (Exception excep)
{
//Display error about ID number going over 50000 and then exit method.
MessageBox.Show(excep.Message);
return;
}
//Stores the details of the textboxes into the customer details for each list entry.
cust.FirstName = txtFName.Text;
cust.SecondName = txtSName.Text;
cust.Email = txtEmail.Text;
cust.SkypeID = txtSkype.Text;
cust.Telephone = txtTelephone.Text;
cust.PrefContact = txtPrefContact.Text;
//Adds the details of the person above to the list and increases the ID number by one for the next entry.
store.add(cust);
ID++;
//Adds the customer to the listbox as an object with their ID number, first name and second name.
lbxCustomers.Items.Add(new ListBoxItem
{
//assigns a tag as an ID number given to it from the customers specific ID number.
Tag = cust.ID,
Content = cust.ID + " " + cust.FirstName + " " + cust.SecondName
});
//Shows user the details of the person added to the list.
MessageBox.Show("Added to list:" +
"\nName: " + cust.FirstName + " " + cust.SecondName +
"\nEmail: " + cust.Email +
"\nSkype: " + cust.SkypeID +
"\nTelephone: " + cust.Telephone +
"\nPreferred Contact: " + cust.PrefContact +
"\nID No: " + cust.ID);
//Clears all textboxes for next entry after adding a customer.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
}
catch
{
//IF statements that displays errors after catching them
//First name textbox empty, longer than 15 characters or including numbers.
if (txtFName.Text == "" || txtFName.Text.Length > 15 || Regex.IsMatch(txtFName.Text, @"^[0-9]+$"))
{
MessageBox.Show("You must enter a first name within 15 characters." +
"\nNo numbers allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Second name textbox empty, longer than 25 characters or including numbers.
else if (txtSName.Text == "" || txtSName.Text.Length > 25 || Regex.IsMatch(txtSName.Text, @"^[0-9]+$"))
{
MessageBox.Show("You must enter a second name within 25 characters." +
"\nNo numbers allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Email textbox empty and checking whether in email format.
else if (txtEmail.Text == "" || !Regex.IsMatch(txtEmail.Text, @"^(?("")("".+?(?<!\)""@)|(([0-9a-z]((\.(?!\.))|[-!#$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"))
{
MessageBox.Show("You haven't entered a valid email address format.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Skype textbox empty or longer than 32 characters.
else if (txtSkype.Text == "" || txtSkype.Text.Length > 32)
{
MessageBox.Show("You haven't entered a valid Skype address." +
"\nMust be within 32 letters and numbers.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//telephone textbox empty, longer than 11 characters or including any alphabetical characters.
else if (txtTelephone.Text == "" || txtTelephone.Text.Length > 11 || Regex.IsMatch(txtTelephone.Text, @"^[a-zA-Z]+$"))
{
MessageBox.Show("You must enter an 11 digit phone number." +
"\nNo Letters allowed.");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
//Preferred contact texbox empty, or does not match email/skype/tel.
else if (txtPrefContact.Text == "" || !Regex.IsMatch(txtPrefContact.Text, @"^(?:tel|email|skype)+$"))
{
MessageBox.Show("Please enter a preferred contact method." +
"\nEither 'email' 'tel' 'phone'");
//Clears all textboxes for next entry after throwing exception.
txtFName.Clear();
txtSName.Clear();
txtEmail.Clear();
txtSkype.Clear();
txtTelephone.Clear();
txtPrefContact.Clear();
return;
}
}
}
//Button for deleting a specific customer with their specific ID.
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
//Tests whether something was entered to the ID textbox
try
{
//Allows the text in the ID textbox to be changed to an integer to be used for checking the ID.
int id = Int32.Parse(txtID.Text);
/*If the ID number entered is not found in the list,
an error message is displayed to say the customer does not exist.
If the ID does exist, deletes that customer. */
if (store.find(id) == null)
{
//Displays error message to tell user that this customer does not exist.
MessageBox.Show("Invalid ID!" +
"\nNo Customer with this ID exists!");
}
else
{
//Sets a temporary listboxitem.
ListBoxItem itemToDelete = null;
//goes through the items in the listbox and checks the id in the textbox against the listbox entries.
//Once it has found the correct entry, stores it in the temporary listboxitem and breaks from the loop.
foreach (ListBoxItem item in lbxCustomers.Items)
{
if ((int)item.Tag == id)
{
itemToDelete = item;
break;
}
}
//If the temporary list item is not null, delete the entry with id entered to the ID textbox.
if (itemToDelete != null)
{
lbxCustomers.Items.Remove(itemToDelete);
}
//Displays the details of the customer deleted
store.ShowDetails(id);
//Delete the details of customer with specific ID.
store.delete(id);
}
}
catch
{
//Displays error to say nothing was entered to the ID textbox and exits from the function.
MessageBox.Show("You did not enter a correct ID!");
return;
}
}
//Button for Finding a specific customer with their specific ID.
private void btnFind_Click(object sender, RoutedEventArgs e)
{
//Tests whether something was entered to the ID textbox
try
{
//Allows the text in the ID textbox to be changed to an integer to be used for checking the ID.
int id = Int32.Parse(txtID.Text);
/*If the ID number entered is not found in the list,
an error message is displayed to say the user does not exist.
If the ID does exist, shows the user the details of the person with that ID. */
if (store.find(id) == null)
{
//Displays error message to tell user that this customer does not exist.
MessageBox.Show("Invalid ID!" +
"\nNo Customer with this ID exists!");
}
else
{
//Displays the details of customer with specific ID.
store.ShowDetails(id);
}
}
catch
{
//Displays error to say nothing was entered to the ID textbox and exits from the function.
MessageBox.Show("You did not enter a correct ID!");
return;
}
}
//Button for showing all customers and their details on next form.
private void btnShwAll_Click(object sender, RoutedEventArgs e)
{
store.ShowAllDetails();
}
//Allows the details of a customer clicked on in the list to be displayed.
private void lbxCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the currently selected item in the ListBox, convert it to a string and then cut off the name leaving only the id number.
int id = (int)((ListBoxItem)lbxCustomers.SelectedItem).Tag;
//Shows the user the selected customers full details.
store.ShowDetails(id);
}
}
}
这是我的客户class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Euan Sutherland
//Class that gets/sets the specific details variables for each customer in the mailinglist.
//29/10/2018
namespace BusinessObjects
{
public class Customer
{
//Variables to hold each specific detail for each customer for adding to the list.
private int _customerID;
private string _telephone;
private string _firstName;
private string _secondName;
private string _email;
private string _skypeID;
private string _prefContact;
//Get/Set for using ID value.
public int ID
{
get
{
return _customerID;
}
set
{
if(value > 50000)
{
throw new ArgumentException("ID number too large!");
}
_customerID = value;
}
}
//Get/Set for using First Name value.
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
//Get/Set for using Second Name value.
public string SecondName
{
get
{
return _secondName;
}
set
{
_secondName = value;
}
}
//Get/Set for using Skype value.
public string SkypeID
{
get
{
return _skypeID;
}
set
{
_skypeID = value;
}
}
//Get/Set for using Telephone value.
public string Telephone
{
get
{
return _telephone;
}
set
{
_telephone = value;
}
}
//Get/Set for using Email value.
public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
//Get/Set for using preferred contact value.
public string PrefContact
{
get
{
return _prefContact;
}
set
{
_prefContact = value;
}
}
//Creates a string with all the specific details of a customer.
public string PrintDetails()
{
return "Found:" +
"\nName: " + FirstName + " " + SecondName +
"\nEmail: " + Email +
"\nSkype: " + SkypeID +
"\nTelephone: " + Telephone +
"\nPreferred Contact: " + PrefContact +
"\nID No: " + ID;
}
public string printAllDetails()
{
return "Found:" +
"\nName: " + FirstName + " " + SecondName +
"\nEmail: " + Email +
"\nSkype: " + SkypeID +
"\nTelephone: " + Telephone +
"\nPreferred Contact: " + PrefContact +
"\nID No: " + ID;
}
}
}
这是我的邮件列表 class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Euan Sutherland
//Class that holds the functions for adding customers to the list. Or to search through the list with specific ID numbers to find specific customers.
//29/10/2018
namespace BusinessObjects
{
public class MailingList
{
//List for holding each customer.
private List<Customer> _list = new List<Customer>();
//Adds a new customer with their details to the list.
public void add(Customer newCustomer)
{
_list.Add(newCustomer);
}
//Searches through the list of customers and matches the ID given to the function to find that customer and return their details.
public Customer find(int id)
{
//Searches through each customer in list.
foreach (Customer c in _list)
{
//checks each customers ID against the ID given to find.
if (id == c.ID)
{
//Returns the customer object with the correct ID given.
return c;
}
}
return null;
}
//Deletes the customer with specific ID.
public void delete(int id)
{
//Sets up a new customer object to search through the list with ID given.
Customer c = this.find(id);
if (c != null)
{
//Removes the customer with ID given.
_list.Remove(c);
}
}
public List<int> ids
{
get
{
List<int> res = new List<int>();
foreach(Customer p in _list)
res.Add(p.ID);
return res;
}
}
//Searches through the list to find the correct customer details to display with ID given.
public void ShowDetails(int id)
{
//Searches through each customer in list.
foreach (Customer c in _list)
{
//If list item ID matches ID given.
if (c.ID == id)
{
//Display the details of that specific customer.
MessageBox.Show(c.PrintDetails());
}
}
}
public void ShowAllDetails()
{
foreach (Customer c in _list)
{
MessageBox.Show(c.printAllDetails());
}
}
}
}
您可以将它放在用户控件中,而不是在 MainWindow 中使用 code/UI。稍后在新应用程序中,您引用演示项目的 dll 并访问新 wpf 应用程序主 window.
中的用户控件希望对您有所帮助。