using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
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.Shapes ;
using GalaSoft.MvvmLight ;
using formula_manage.SQLModels ;
using System.Data.SqlClient ;
using System.Data ;
using System.Windows.Forms ;
using static System . Windows . Forms . VisualStyles . VisualStyleElement . Button ;
using Xceed.Wpf.AvalonDock.Themes ;
using static System . Net . Mime . MediaTypeNames ;
namespace formula_manage.Windows
{
/// <summary>
/// color.xaml 的交互逻辑
/// </summary>
public partial class color : Window
{
public string INIPath = Convert . ToString ( System . AppDomain . CurrentDomain . BaseDirectory ) + "formula.ini" ; //配置文件路径
DataTable dataTable = new DataTable ( ) ; //建立缓存
int Button_ID = 0 ;
string TEXT_SQLIP ;
string TEXT_SQLNAME ;
string TEXT_SQMOD ;
string TEXT_SQLUSER ;
string TEXT_SQLPASWOR ;
string Connstr_SC ;
public color ( )
{
WindowStartupLocation = WindowStartupLocation . CenterScreen ;
InitializeComponent ( ) ;
}
private async void color_Loaded ( object sender , RoutedEventArgs e ) //打开页面执行
{
UserClass . IniFile . IniFiles Configini = new UserClass . IniFile . IniFiles ( INIPath ) ; //生效配置读取
this . DataGridcolor . LoadingRow + = new EventHandler < DataGridRowEventArgs > ( this . DataGridEquipment_LoadingRow ) ; //生成序列号
TEXT_SQLIP = Configini . IniReadvalue ( "SQL_SERVER" , "SQL1" ) ; //读配置文件
TEXT_SQLNAME = Configini . IniReadvalue ( "SQL_SERVER" , "SQL2" ) ;
TEXT_SQMOD = Configini . IniReadvalue ( "SQL_SERVER" , "SQL3" ) ;
TEXT_SQLUSER = Configini . IniReadvalue ( "SQL_SERVER" , "SQL4" ) ;
TEXT_SQLPASWOR = Configini . IniReadvalue ( "SQL_SERVER" , "SQL5" ) ;
if ( TEXT_SQMOD = = "0" ) //判断连接方式
{
Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";Trusted_Connection=SSPI" ;
}
else
{
Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";User ID=" + TEXT_SQLUSER + ";Password=" + TEXT_SQLPASWOR ;
}
string color_sql = "SELECT COLOR_CODE ,COLOR_NAME , COLOR_NO , COLOR_Remark FROM [dbo].[COLOR]" ; //查询语句
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
try
{
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlDataAdapter color_data = new SqlDataAdapter ( color_sql , Connstr_SC ) ; //查询
color_data . Fill ( dataTable ) ; //查询结果存入缓存
conn_SC . Close ( ) ; //关闭连接
DataGridcolor . ItemsSource = dataTable . DefaultView ; //数据加入表格
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求信息失败,检查连接" ) ;
return ;
}
}
/// <summary>
/// 生成序列号的方法
/// </summary>
private void DataGridEquipment_LoadingRow ( object sender , DataGridRowEventArgs e )
{
e . Row . Header = e . Row . GetIndex ( ) + 1 ;
}
/// <summary>
/// 查询数据库的方法
/// </summary>
private async void GridSql ( )
{
string color_sql = "SELECT COLOR_CODE ,COLOR_NAME , COLOR_NO , COLOR_Remark FROM [dbo].[COLOR]" ; //查询语句
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
try
{
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlDataAdapter color_data = new SqlDataAdapter ( color_sql , Connstr_SC ) ; //查询
dataTable . Clear ( ) ;
color_data . Fill ( dataTable ) ; //查询结果存入缓存
conn_SC . Close ( ) ; //关闭连接
DataGridcolor . ItemsSource = dataTable . DefaultView ; //数据加入表格
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求信息失败,检查连接" ) ;
return ;
}
}
private async void Button_Preservation ( object sender , RoutedEventArgs e ) //保存按钮事件
{
Regex re_number = new Regex ( @"^[0-9]+(.[0-9]{1,4})?$" ) ; //校验用正则表达式有1~2位小数的正实数
string color_sql ;
int ColorCode_SQL ;
string colorValue = color_color . Background . ToString ( ) ; //获取色彩框背景色"#FFC1C2C3"
string StuffColor_R = string . Format ( "{0:X2}" , colorValue . Substring ( 3 , 2 ) ) ; //获取红色参数C1
string StuffColor_G = string . Format ( "{0:X2}" , colorValue . Substring ( 5 , 2 ) ) ; //获取绿色参数C2
string StuffColor_B = string . Format ( "{0:X2}" , colorValue . Substring ( 7 , 2 ) ) ; //获取蓝色参数C3
string StuffColor = StuffColor_B + StuffColor_G + StuffColor_R ; //合并16进制rgb参数
ColorCode_SQL = Convert . ToInt32 ( StuffColor , 1 6 ) ; //16进制转10进制
if ( ColorCode_SQL < 0 ) ColorCode_SQL = 1 6 7 7 7 2 1 5 ; //判断色彩数据是否有效
if ( ( re_number . IsMatch ( this . color_color . Text ) = = false ) & & ( color_color . Text ! = "" ) )
System . Windows . MessageBox . Show ( "ERR:单位码重错误" , "错误" ) ; //检查价格输入信息
else
if ( string . IsNullOrEmpty ( this . color_code . Text ) )
System . Windows . MessageBox . Show ( "ERR:无效的原料信息" , "错误" ) ; //检查原料代码
else
if ( string . IsNullOrEmpty ( this . color_name . Text ) )
System . Windows . MessageBox . Show ( "ERR:无效的原料名称" , "错误" ) ; //检查原料名
else
{
try
{
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
color_sql = "SELECT count(*) FROM [dbo].[COLOR] WHERE COLOR_CODE = '" + color_code . Text + " '" ; //查询语句
await conn_SC . OpenAsync ( ) ; //打开数据连
SqlCommand cmd = new SqlCommand ( color_sql , conn_SC ) ; //查询记录数
int count = Convert . ToInt32 ( cmd . ExecuteScalar ( ) ) ; //显示记录数
conn_SC . Close ( ) ; //关闭连接
if ( count = = 0 )
{
color_sql = string . Format ( "INSERT INTO [dbo].[COLOR](COLOR_CODE ,COLOR_NAME , COLOR_NO , COLOR_Remark) " +
"VALUES ('" + color_code . Text + "','" + color_name . Text + "','" + ColorCode_SQL + "','" + color_Remark . Text + "')" ) ;
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand ( color_sql , conn_SC ) ;
int sql_in = INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
conn_SC . Close ( ) ; //关闭连接
if ( sql_in = = 0 )
{
System . Windows . MessageBox . Show ( "添加失败" ) ;
}
else
{
GridSql ( ) ;
System . Windows . MessageBox . Show ( "添加完成" ) ;
}
}
else
{
color_sql = string . Format ( "UPDATE [dbo].[COLOR] SET COLOR_NAME='" + color_name . Text + "',COLOR_NO='" + ColorCode_SQL + "',COLOR_Remark='" + color_Remark . Text + "' Where COLOR_CODE='" + color_code . Text + "'" ) ;
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand ( color_sql , conn_SC ) ;
int sql_in = INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
conn_SC . Close ( ) ; //关闭连接
if ( sql_in = = 0 )
{
System . Windows . MessageBox . Show ( "C0110-1:修改失败" ) ;
}
else
{
GridSql ( ) ;
System . Windows . MessageBox . Show ( "修改完成" ) ;
}
}
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求失败,检查连接" ) ;
return ;
}
}
}
private async void Button_Delete ( object sender , RoutedEventArgs e ) //删除按钮事件
{
string ShowProductName = "是否删除【" + this . color_name . Text + "】" + this . color_code . Text ; //获取信息并拼接提示字符串
MessageBoxResult showProductName = System . Windows . MessageBox . Show ( ShowProductName , "提示" , MessageBoxButton . YesNo , MessageBoxImage . Warning , MessageBoxResult . Yes ) ; //弹窗提示是否删除目标原料
if ( showProductName = = MessageBoxResult . Yes ) //判断是否删除
{
string Stuff_sql = string . Format ( "DELETE FROM [dbo].[COLOR] WHERE COLOR_CODE ='" + color_code . Text . ToString ( ) + "'" ) ;
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
try
{
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand ( Stuff_sql , conn_SC ) ;
int sql_in = INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
conn_SC . Close ( ) ; //关闭连接
if ( sql_in = = 0 )
{
System . Windows . MessageBox . Show ( "C0110-2:删除失败" , "错误" ) ; //判断执行是否成功
}
else
{
GridSql ( ) ;
}
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求失败,检查连接" ) ;
return ;
}
}
}
private void DataGridcolor_MouseDoubleClick ( object sender , MouseButtonEventArgs e ) //数据表双击事件
{
int rownum = DataGridcolor . SelectedIndex ; //获取鼠标选中行并定义变量
if ( rownum ! = - 1 ) //判断鼠标定位是否有效
{
/*定位选中行及指定列单元格文本信息*/
color_code . Text = ( DataGridcolor . Columns [ 1 ] . GetCellContent ( DataGridcolor . Items [ rownum ] ) as TextBlock ) . Text . TrimEnd ( ) ; //定位第0列,代码
color_name . Text = ( DataGridcolor . Columns [ 2 ] . GetCellContent ( DataGridcolor . Items [ rownum ] ) as TextBlock ) . Text . TrimEnd ( ) ; //定位第1列,名称
color_Remark . Text = ( DataGridcolor . Columns [ 4 ] . GetCellContent ( DataGridcolor . Items [ rownum ] ) as TextBlock ) . Text . TrimEnd ( ) ; //定位,
if ( ( DataGridcolor . Columns [ 3 ] . GetCellContent ( DataGridcolor . Items [ rownum ] ) as TextBlock ) . Background ! = null ) //定位选中行单元格,色彩,判断参数是否有效
{
color_color . Background = ( DataGridcolor . Columns [ 3 ] . GetCellContent ( DataGridcolor . Items [ rownum ] ) as TextBlock ) . Background ; //定位选中行单元格,色彩,有效背景色传递至色彩框背景色
}
else
{
color_color . Background = new SolidColorBrush ( System . Windows . Media . Color . FromArgb ( 2 5 5 , 2 5 5 , 2 5 5 , 2 5 5 ) ) ; //白色传递至色彩框背景色
}
}
}
private void Color_MouseDoubleClick ( object sender , MouseButtonEventArgs e ) //色彩框双击事件
{
System . Windows . Forms . ColorDialog colorDialog = new System . Windows . Forms . ColorDialog ( ) ; //使用调色盘控件ColorDialog
if ( colorDialog . ShowDialog ( ) = = System . Windows . Forms . DialogResult . OK ) //打开调色盘
{
System . Drawing . Color DColor = colorDialog . Color ; ////获取选中色彩信息
//System.Windows.Media.Color MColor = new System.Windows.Media.Color();//转换
System . Windows . Media . Color MColor = System . Windows . Media . Color . FromArgb ( DColor . A , DColor . R , DColor . G , DColor . B ) ; //转换并配置ARGB参数
color_color . Background = new SolidColorBrush ( MColor ) ; //ARGB参数输出至Color的背景色
//string colorCode = string.Format("{0:X2}", DColor.B) + string.Format("{0:X2}", DColor.G) + string.Format("{0:X2}", DColor.R);//反向十六进制RGB
//string colorCode = string.Format("{0:X2}", DColor.R) + string.Format("{0:X2}", DColor.G) + string.Format("{0:X2}", DColor.B);//正向十六进制RGB
//ColorCode_SQL = Convert.ToInt32(colorCode, 16);//十六进制RGB转存储数值
}
}
}
}