📄 frmaddnewbook.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bussiness;
namespace UI
{
public partial class frmAddNewBook : Form
{
private static frmAddNewBook objfrmAddNewBook;
SelectInfo objSelectInfo = new SelectInfo();
Books objBooks = new Books();
DataTable objDataTable;
public frmAddNewBook()
{
InitializeComponent();
}
public static frmAddNewBook getfrmAddNewBook()
{
objfrmAddNewBook = new frmAddNewBook();
return objfrmAddNewBook;
}
/// <summary>
/// 此方法用于检测输入的字符串是否全为数字
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private bool IsDigit(string input)
{
int count = 0;
foreach (char ch in input)
{
if (char.IsDigit(ch))
{
count++;
}
}
if (input.Length == count)
return true;
return false;
}
/// <summary>
/// 验证字符串是否为空
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private bool IsNull(string input)
{
if (input == "")
{
return true;
}
return false;
}
/// <summary>
/// 验证数字是否小于零
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private bool IsBiggerZero(string input)
{
if (Convert.ToInt32(input) < 0)
{
return true;
}
return false;
}
/// <summary>
/// 验证用户输入数据是否正确
/// </summary>
/// <returns></returns>
private bool checkIN()
{
if(IsNull(txtBookName.Text))
{
MessageBox.Show("书籍ID不能为空!");
txtBookName.Focus();
return false;
}
if (IsDigit(txtBookName.Text.Trim()) || txtBookName.Text.Trim().IndexOf('B') != 0)
{
MessageBox.Show("书籍ID必须以B开头后跟数字");
txtBookName.Focus();
return false;
}
else if (txtBookName.Text.Length != 5)
{
MessageBox.Show("书籍ID必须为五位");
txtBookName.Focus();
return false;
}
else if (IsNull(txtBookISBN.Text))
{
MessageBox.Show("书籍名称不能为空.");
txtBookISBN.Focus();
return false;
}
else if(IsNull(mtxtBookPrice.Text))
{
MessageBox.Show("书籍价格不能为空");
mtxtBookPrice.Focus();
return false;
}
else if (IsNull(txtBookAuthor.Text))
{
MessageBox.Show("数据作者不能为空");
txtBookAuthor.Focus();
return false;
}
return true;
}
//判断用户添加的信息有了就提示已有纪录!
//没有就加到数据库中
private void btnOk_Click(object sender, EventArgs e)
{
if (checkIN())
{
DateTime Stroragetime = dtpStorageDate.Value;
if (objBooks.SelectBooks(txtBookName.Text, txtBookISBN.Text))
{
try
{
int i;
if (Rshi.Checked)
{
i = 1;
}
else
{
i = 0;
}
objBooks.InsertBooks(txtBookName.Text, txtBookISBN.Text, txtBookType.Text, txtBookAuthor.Text, comChu.SelectedItem.ToString(), Convert.ToDouble(mtxtBookPrice.Text),
Convert.ToDouble(txtBorrowMoney.Text), Convert.ToInt32(nudStorageNum.Value.ToString()), Convert.ToInt32(nudStorageNum.Value.ToString()), dtpStorageDate.Value.ToString(),
txtOp.Text, txtBookSynopsis.Text, Convert.ToInt32(nudBorrowNum.Value.ToString()), i);
MessageBox.Show("添加新书成功");
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
if (MessageBox.Show("该书籍已经存在?", "是否更新数量",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
objBooks.UpdateBooks(txtBookName.Text, txtBookISBN.Text, Convert.ToInt32(nudStorageNum.Value.ToString()), Convert.ToInt32(nudStorageNum.Value.ToString()));
MessageBox.Show("添加图书信息成功", "提示");
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
private void frmAddNewBook_Activated(object sender, EventArgs e)
{
this.txtBookName.Focus();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtBookName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtBookISBN.Focus();
}
}
private void txtBookISBN_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
mtxtBookPrice.Focus();
}
}
private void mtxtBookPrice_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtBookAuthor.Focus();
}
}
private void txtBookAuthor_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void txtBookConcern_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
nudStorageNum.Focus();
}
}
private void nudStorageNum_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btnOk.Focus();
}
}
private void lblBookName_Click(object sender, EventArgs e)
{
}
private void frmAddNewBook_Load(object sender, EventArgs e)
{
objDataTable=objSelectInfo.GridViewBookConcernnfoTable();
foreach (DataRow row in objDataTable.Rows)
{
this.comChu.Items.Add(row[0].ToString());
}
}
private void btnXuan_Click(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -