您的位置:首页 > 编程语言 > C#

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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐