WPF 面向对象 实现简单图书管理系统
程序员文章站
2022-05-13 20:24:28
...
XAML界面
<Window x:Class="图书管理系统.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:图书管理系统"
mc:Ignorable="d"
Title="图书管理系统" Height="450" Width="800" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" Icon="img/03.jpg">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="45"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition Width="140"></ColumnDefinition>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Menu Grid.Row="0" Grid.Column="0">
<MenuItem Height="45" Width="112" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
<MenuItem.Icon>
<Image Source="img/56.png"></Image>
</MenuItem.Icon>
<MenuItem.Header>
<Label Content="图书管理" Margin="-20,8,0,0"></Label>
</MenuItem.Header>
<MenuItem Header="添加图书" Margin="0,0,-36,0" x:Name="AddBook" Click="AddBook_Click" Tag="Add">
<MenuItem.Icon>
<Image Source="img/61.png" Width="20" Height="20"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="移除图书" Margin="0,0,-36,0" Tag="Remove" Click="AddBook_Click">
<MenuItem.Icon>
<Image Source="img/156.png" Width="20" Height="20"></Image>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>
<Menu Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Background="SkyBlue"></Menu>
<Menu Grid.Row="0" Grid.Column="1">
<MenuItem HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Click="MenuItem_Click_1">
<MenuItem.Icon>
<Image Source="img/212.png"></Image>
</MenuItem.Icon>
<MenuItem.Header>
<Label Content="查看所有书籍" Margin="-27,8,0,0"></Label>
</MenuItem.Header>
</MenuItem>
</Menu>
<Menu Grid.Row="0" Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.ColumnSpan="2">
<MenuItem Width="58">
<MenuItem.Icon>
<Image Source="img/211.png" Width="30"></Image>
</MenuItem.Icon>
</MenuItem>
<TextBox Width="74" Margin="-35,10,0,0" x:Name="Textss">
<TextBox.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Center">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="搜索"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Menu>
<DataGrid Grid.ColumnSpan="3" HorizontalAlignment="Left" Height="341" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="358" Name="Databook"/>
<Grid Grid.Row="1" Grid.Column="3" Background="Gold" x:Name="add" Visibility="Hidden">
<Label Content="添加书籍" FontSize="20" Margin="160,10,136,321"></Label>
<Label Content="书名:" FontSize="20" Margin="70,69,279,254"/>
<Label Content="作者:" FontSize="20" Margin="70,126,279,198"/>
<Label Content="类型:" FontSize="20" Margin="70,182,284,142"/>
<Label Content="价格:" FontSize="20" Margin="70,238,279,87"/>
<Button Content="添加" HorizontalAlignment="Left" Margin="160,301,0,0" VerticalAlignment="Top" Width="75" Height="30" Click="Button_Click"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="133,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="name"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="133,137,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="write"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="133,193,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="tp"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="133,249,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="mey" PreviewTextInput="rimitnumber" InputMethod.IsInputMethodEnabled="False"/>
</Grid>
<Menu Grid.Row="0" Grid.Column="3">
<Button Content="搜索" Margin="-15,8,0,0" Width="50" Height="30" Background="Purple" Click="Button_Click_1"></Button>
</Menu>
</Grid>
</Window>
cs界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 System.IO;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace 图书管理系统
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon ico = new System.Windows.Forms.NotifyIcon();
ico.Icon = new System.Drawing.Icon("../../img/1.ico");
ico.Text = "图标管理系统";
ico.Visible = true;
ico.BalloonTipTitle = "图标管理系统";
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
exit.Click += Exit_Click;
System.Windows.Forms.MenuItem[] menus = new System.Windows.Forms.MenuItem[] { exit };
ico.ContextMenu = new System.Windows.Forms.ContextMenu(menus);
}
private void Exit_Click(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
IList<Books> books;
string[] str;
string bookpath = "../../图书.txt";
public void rimitnumber(object sender, TextCompositionEventArgs e)
{
Regex re = new Regex("[^0-9]+");
e.Handled = re.IsMatch(e.Text);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
books = new BindingList<Books>();
Find();
}
//书籍写入集合
private void Find()
{
books.Clear();
StreamReader reader = new StreamReader(bookpath, Encoding.Default);
string txts = reader.ReadToEnd();
str = txts.Split(new char[] { ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < str.Length; i++)
{
string[] BooksAll = str[i].Split('|');
Books book = new Books();
book.Bid = int.Parse(BooksAll[0]);
book.Bname = BooksAll[1];
book.Writer = BooksAll[2];
book.Money = Convert.ToDouble(BooksAll[3]);
book.Types = BooksAll[4];
books.Add(book);
}
}
//查看所有书籍
private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
Find();
Databook.ItemsSource = books;
}
//添加书籍按钮
private void Button_Click(object sender, RoutedEventArgs e)
{
int index = books[books.Count-1].Bid;
index++;
Books book = new Books();
book.Bid =Convert.ToInt32("00")+index;
book.Bname = "《" + name.Text + "》";
book.Writer = write.Text;
book.Money = int.Parse(mey.Text);
book.Types = tp.Text;
books.Add(book);
Databook.ItemsSource = books;
WriteBooks(index,name.Text,write.Text,mey.Text,tp.Text);
add.Visibility = Visibility.Hidden;
}
//书籍写入文本
private void WriteBooks(int id, string name, string writ, string money, string type)
{
StreamWriter writer = new StreamWriter(bookpath, true, Encoding.Default);
string str1 = string.Format("00{0}|《{1}》|{2}|{3}|{4},", id, name, writ, money, type);
//"\r\n" + "00" + id + "|" + "《" + name + "》" + "|" + writ + "|" + money + "|", type + "," + "\r\n"
writer.WriteLine(str1);
writer.Close();
}
//添加书籍和删除书籍
private void AddBook_Click(object sender, RoutedEventArgs e)
{
MenuItem menu = sender as MenuItem;
if (menu.Tag.ToString() == "Add")
{
add.Visibility = Visibility.Visible;
}
else
{
int x = Databook.SelectedIndex;
books.RemoveAt(x);
StreamWriter writer = new StreamWriter(bookpath, false, Encoding.Default);
for (int i = 0; i < books.Count; i++)
{
string str = string.Format("00{0}|{1}|{2}|{3}|{4},", books[i].Bid, books[i].Bname, books[i].Writer, books[i].Money, books[i].Types);
writer.WriteLine(str);
}
writer.Close();
}
}
//查找书籍
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string str2 = Textss.Text;
var chazhao = books.Where(xxx => xxx.Writer == str2).ToList();
Databook.ItemsSource = chazhao;
}
}
}
创建Books类界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 图书管理系统
{
//public enum Bookstype
// {
// 历史,小说,诗经,财经
// }
class Books
{
/// <summary>
/// 书名
/// </summary>
public string Bname { get; set; }
/// <summary>
/// 书号
/// </summary>
public int Bid { get; set; }
/// <summary>
/// 作者
/// </summary>
public string Writer { get; set; }
/// <summary>
/// 价钱
/// </summary>
public double Money { get; set; }
/// <summary>
/// 类型
/// </summary>
public string Types { get; set; }
}
}