C#使用Binding事件完成超越内置类型转换的功能
2008-12-29 20:00
477 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BindingEvents.ExchangeRatesDataSetTableAdapters;
using System.IO;
using System.Drawing.Imaging;
namespace BindingEvents
{
public partial class CurrencyExchangeForm : Form
{
public CurrencyExchangeForm()
{
InitializeComponent();
// Get the data
CountriesTableAdapter countriesAdapter = new CountriesTableAdapter();
ExchangeRatesTableAdapter exchangeRatesAdapter = new ExchangeRatesTableAdapter();
m_ExchangeRatesDataSet = new ExchangeRatesDataSet();
countriesAdapter.Fill(m_ExchangeRatesDataSet.Countries);
exchangeRatesAdapter.Fill(m_ExchangeRatesDataSet.ExchangeRates);
m_ExchangeRatesBindingSource.DataSource = m_ExchangeRatesDataSet;
m_ExchangeRatesBindingSource.DataMember = m_ExchangeRatesDataSet.ExchangeRates.TableName;
CreateBindings();
BuildAutoCompleteList();
m_CountryFromTextBox.Validated += OnCountryFromValidated;
m_CountryToTextBox.Validated += OnCountryToValidated;
}
private void CreateBindings()
{
Binding countryFromBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryFromID", true);
countryFromBinding.Format += new ConvertEventHandler(OnCountryFromFormat);
countryFromBinding.Parse += new ConvertEventHandler(OnCountryFromParse);
m_CountryFromTextBox.DataBindings.Add(countryFromBinding);
Binding countryToBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryToID", true);
countryToBinding.Format += new ConvertEventHandler(OnCountryToFormat);
countryToBinding.Parse += new ConvertEventHandler(OnCountryToParse);
m_CountryToTextBox.DataBindings.Add(countryToBinding);
Binding currencyFromBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryFromID", true);
currencyFromBinding.Format += new ConvertEventHandler(OnCurrencyFromFormat);
m_CurrencyTypeFromTextBox.DataBindings.Add(currencyFromBinding);
Binding currencyToBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryToID", true);
currencyToBinding.Format += new ConvertEventHandler(OnCurrencyToFormat);
m_CurrencyTypeToTextBox.DataBindings.Add(currencyToBinding);
Binding exchangeRateBinding = new Binding("Text", m_ExchangeRatesBindingSource, "ExchangeRate", true, DataSourceUpdateMode.OnValidation, "1.0000", "#.0000");
m_ExchangeRateTextBox.DataBindings.Add(exchangeRateBinding);
Binding exchangeRateDateBinding = new Binding("Value", m_ExchangeRatesBindingSource, "ExchangeRateDate", true);
m_ExchangeRateDateTimePicker.DataBindings.Add(exchangeRateDateBinding);
}
private void OnSaveItem(object sender, EventArgs e)
{
if (this.Validate())
{
m_ExchangeRatesBindingSource.EndEdit();
ExchangeRatesTableAdapter exchangeRatesAdapter = new ExchangeRatesTableAdapter();
CountriesTableAdapter countriesAdapter = new CountriesTableAdapter();
countriesAdapter.Update(m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet);
exchangeRatesAdapter.Update(m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet);
}
else
{
System.Windows.Forms.MessageBox.Show(this, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
}
void OnCountryFromFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
m_FlagFromPictureBox.Image = null;
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CountryName;
// Now set other control properties based on this same binding
ImageConverter converter = new ImageConverter();
m_FlagFromPictureBox.Image = converter.ConvertFrom(countryRow.Flag) as Image;
}
void OnCountryFromParse(object sender, ConvertEventArgs e)
{
// Need to look up the Country information for the country name
ExchangeRatesDataSet.CountriesRow row = GetCountryRow(e.Value.ToString());
if (row == null)
{
string error = "Country not found";
m_ErrorProvider.SetError(m_CountryFromTextBox, error);
m_CountryFromTextBox.Focus();
throw new ArgumentException(error);
}
e.Value = row.CountryID;
}
void OnCountryToFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
m_FlagToPictureBox.Image = null;
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CountryName;
// Now set other control properties based on this same binding
ImageConverter converter = new ImageConverter();
m_FlagToPictureBox.Image = converter.ConvertFrom(countryRow.Flag) as Image;
}
void OnCountryToParse(object sender, ConvertEventArgs e)
{
// Need to look up the Country information for the country name
ExchangeRatesDataSet.CountriesRow row = GetCountryRow(e.Value.ToString());
if (row == null)
{
string error = "Country not found";
m_ErrorProvider.SetError(m_CountryToTextBox, error);
m_CountryToTextBox.Focus();
throw new ArgumentException(error);
}
e.Value = row.CountryID;
}
void OnCurrencyFromFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CurrencyType;
}
void OnCurrencyToFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CurrencyType;
}
ExchangeRatesDataSet.CountriesRow GetCountryRow(int countryID)
{
ExchangeRatesDataSet ds = m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet;
return ds.Countries.Select("CountryID = " + countryID.ToString())[0]
as ExchangeRatesDataSet.CountriesRow;
}
ExchangeRatesDataSet.CountriesRow GetCountryRow(string countryName)
{
ExchangeRatesDataSet ds = m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet;
DataRow[] rows = ds.Countries.Select("CountryName = '" + countryName + "'");
if (rows.Length > 0)
{
return rows[0] as ExchangeRatesDataSet.CountriesRow;
}
else
{
return null;
}
}
private static byte[] BitmapToByteArray(Bitmap bmp)
{
using (MemoryStream stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Gif);
return stream.ToArray();
}
}
private static Bitmap BitmapFromByteArray(byte[] bits)
{
MemoryStream stream = new MemoryStream(bits);
return (Bitmap)Image.FromStream(stream);
}
private void BuildAutoCompleteList()
{
AutoCompleteStringCollection filterVals = new AutoCompleteStringCollection();
foreach (ExchangeRatesDataSet.CountriesRow countryRow in m_ExchangeRatesDataSet.Countries)
{
filterVals.Add(countryRow.CountryName);
}
m_CountryFromTextBox.AutoCompleteCustomSource = filterVals;
m_CountryToTextBox.AutoCompleteCustomSource = filterVals;
}
// Have to handle the Validated events on the textboxes to force the changes made through parse
// to be completed in the data row affected. Otherwise, the other related bound controls -
// specifically the currency textboxes, will not reflect the change just made in the
// Country From/To textbox until you page off the row
private void OnCountryFromValidated(object sender, EventArgs e)
{
m_ExchangeRatesBindingSource.EndEdit();
}
private void OnCountryToValidated(object sender, EventArgs e)
{
m_ExchangeRatesBindingSource.EndEdit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BindingEvents.ExchangeRatesDataSetTableAdapters;
using System.IO;
using System.Drawing.Imaging;
namespace BindingEvents
{
public partial class CurrencyExchangeForm : Form
{
public CurrencyExchangeForm()
{
InitializeComponent();
// Get the data
CountriesTableAdapter countriesAdapter = new CountriesTableAdapter();
ExchangeRatesTableAdapter exchangeRatesAdapter = new ExchangeRatesTableAdapter();
m_ExchangeRatesDataSet = new ExchangeRatesDataSet();
countriesAdapter.Fill(m_ExchangeRatesDataSet.Countries);
exchangeRatesAdapter.Fill(m_ExchangeRatesDataSet.ExchangeRates);
m_ExchangeRatesBindingSource.DataSource = m_ExchangeRatesDataSet;
m_ExchangeRatesBindingSource.DataMember = m_ExchangeRatesDataSet.ExchangeRates.TableName;
CreateBindings();
BuildAutoCompleteList();
m_CountryFromTextBox.Validated += OnCountryFromValidated;
m_CountryToTextBox.Validated += OnCountryToValidated;
}
private void CreateBindings()
{
Binding countryFromBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryFromID", true);
countryFromBinding.Format += new ConvertEventHandler(OnCountryFromFormat);
countryFromBinding.Parse += new ConvertEventHandler(OnCountryFromParse);
m_CountryFromTextBox.DataBindings.Add(countryFromBinding);
Binding countryToBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryToID", true);
countryToBinding.Format += new ConvertEventHandler(OnCountryToFormat);
countryToBinding.Parse += new ConvertEventHandler(OnCountryToParse);
m_CountryToTextBox.DataBindings.Add(countryToBinding);
Binding currencyFromBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryFromID", true);
currencyFromBinding.Format += new ConvertEventHandler(OnCurrencyFromFormat);
m_CurrencyTypeFromTextBox.DataBindings.Add(currencyFromBinding);
Binding currencyToBinding = new Binding("Text", m_ExchangeRatesBindingSource, "CountryToID", true);
currencyToBinding.Format += new ConvertEventHandler(OnCurrencyToFormat);
m_CurrencyTypeToTextBox.DataBindings.Add(currencyToBinding);
Binding exchangeRateBinding = new Binding("Text", m_ExchangeRatesBindingSource, "ExchangeRate", true, DataSourceUpdateMode.OnValidation, "1.0000", "#.0000");
m_ExchangeRateTextBox.DataBindings.Add(exchangeRateBinding);
Binding exchangeRateDateBinding = new Binding("Value", m_ExchangeRatesBindingSource, "ExchangeRateDate", true);
m_ExchangeRateDateTimePicker.DataBindings.Add(exchangeRateDateBinding);
}
private void OnSaveItem(object sender, EventArgs e)
{
if (this.Validate())
{
m_ExchangeRatesBindingSource.EndEdit();
ExchangeRatesTableAdapter exchangeRatesAdapter = new ExchangeRatesTableAdapter();
CountriesTableAdapter countriesAdapter = new CountriesTableAdapter();
countriesAdapter.Update(m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet);
exchangeRatesAdapter.Update(m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet);
}
else
{
System.Windows.Forms.MessageBox.Show(this, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
}
void OnCountryFromFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
m_FlagFromPictureBox.Image = null;
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CountryName;
// Now set other control properties based on this same binding
ImageConverter converter = new ImageConverter();
m_FlagFromPictureBox.Image = converter.ConvertFrom(countryRow.Flag) as Image;
}
void OnCountryFromParse(object sender, ConvertEventArgs e)
{
// Need to look up the Country information for the country name
ExchangeRatesDataSet.CountriesRow row = GetCountryRow(e.Value.ToString());
if (row == null)
{
string error = "Country not found";
m_ErrorProvider.SetError(m_CountryFromTextBox, error);
m_CountryFromTextBox.Focus();
throw new ArgumentException(error);
}
e.Value = row.CountryID;
}
void OnCountryToFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
m_FlagToPictureBox.Image = null;
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CountryName;
// Now set other control properties based on this same binding
ImageConverter converter = new ImageConverter();
m_FlagToPictureBox.Image = converter.ConvertFrom(countryRow.Flag) as Image;
}
void OnCountryToParse(object sender, ConvertEventArgs e)
{
// Need to look up the Country information for the country name
ExchangeRatesDataSet.CountriesRow row = GetCountryRow(e.Value.ToString());
if (row == null)
{
string error = "Country not found";
m_ErrorProvider.SetError(m_CountryToTextBox, error);
m_CountryToTextBox.Focus();
throw new ArgumentException(error);
}
e.Value = row.CountryID;
}
void OnCurrencyFromFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CurrencyType;
}
void OnCurrencyToFormat(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
{
return;
}
ExchangeRatesDataSet.CountriesRow countryRow = GetCountryRow((int)e.Value);
e.Value = countryRow.CurrencyType;
}
ExchangeRatesDataSet.CountriesRow GetCountryRow(int countryID)
{
ExchangeRatesDataSet ds = m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet;
return ds.Countries.Select("CountryID = " + countryID.ToString())[0]
as ExchangeRatesDataSet.CountriesRow;
}
ExchangeRatesDataSet.CountriesRow GetCountryRow(string countryName)
{
ExchangeRatesDataSet ds = m_ExchangeRatesBindingSource.DataSource as ExchangeRatesDataSet;
DataRow[] rows = ds.Countries.Select("CountryName = '" + countryName + "'");
if (rows.Length > 0)
{
return rows[0] as ExchangeRatesDataSet.CountriesRow;
}
else
{
return null;
}
}
private static byte[] BitmapToByteArray(Bitmap bmp)
{
using (MemoryStream stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Gif);
return stream.ToArray();
}
}
private static Bitmap BitmapFromByteArray(byte[] bits)
{
MemoryStream stream = new MemoryStream(bits);
return (Bitmap)Image.FromStream(stream);
}
private void BuildAutoCompleteList()
{
AutoCompleteStringCollection filterVals = new AutoCompleteStringCollection();
foreach (ExchangeRatesDataSet.CountriesRow countryRow in m_ExchangeRatesDataSet.Countries)
{
filterVals.Add(countryRow.CountryName);
}
m_CountryFromTextBox.AutoCompleteCustomSource = filterVals;
m_CountryToTextBox.AutoCompleteCustomSource = filterVals;
}
// Have to handle the Validated events on the textboxes to force the changes made through parse
// to be completed in the data row affected. Otherwise, the other related bound controls -
// specifically the currency textboxes, will not reflect the change just made in the
// Country From/To textbox until you page off the row
private void OnCountryFromValidated(object sender, EventArgs e)
{
m_ExchangeRatesBindingSource.EndEdit();
}
private void OnCountryToValidated(object sender, EventArgs e)
{
m_ExchangeRatesBindingSource.EndEdit();
}
}
}
相关文章推荐
- C#中使用指针转换数据类型[C#/unsafe]
- Qt5事件处理机制:“函数样式转换”:位于“.”运算符右边时非法/不允许使用此类型名
- C#实现String类型和json之间的相互转换功能示例
- C#引用类型转换,到底使用is,as还是显式强转?
- c#中与vb中CType相同功能的函数(强类型转换)
- C#类型的转换:Converter<TInput, TOutput> 委托的使用
- 使用type类型和存储过程来完成oracle的行列转换
- C实现分割字符串(使用strtok_s函数,完成C#的Split函数功能)
- C# 使用DateTime.TryParseExact将自定义日期类型转换成日期
- C#新手入门代码 使用readline时不输入而是敲击回车,之后用convert转换类型出现错误的解决代码
- (原创)c#学习笔记05--变量的更多内容01--类型转换03--使用Convert命令进行显式转焕
- C++ stringstream的使用 类型转换以及字符分离(功能类似split)
- java中把字符串类型转换为int型,不允许使用java内置方法,如parseInt等
- C#命名空间的使用、添加命名空间、引用类型和值类型、字符串与char数组转换
- C#中使用System.Convert类将一种基础数据类型转换为其他基础数据类型
- C#.NET开发Winform使用MySQL数据库 无法将“System.Byte[]”的对象强制转换为类型“System.IConvertible”
- C#中使用强制类型实现字符串和ASCII码之间的转换
- 如何使用C#Windows窗体完成文件浏览功能:
- Effective C++:规定24:如果所有的单位都需要的参数类型转换,使用请做到这一点non-member功能
- C#中使用as进行类型转换