Click here to Skip to main content
15,881,665 members
Articles / Programming Languages / C#

Using .NET Databinding to Bind an Editable InkPicture Control to a Database

Rate me:
Please Sign up or sign in to vote.
4.22/5 (5 votes)
22 Aug 2004CPOL4 min read 45.4K   167   26  
This article discusses how to create a UserControl that binds to a database to display Tablet PC Ink.
/// Code provided by Rhy A. Mednick of Chicken Scratch Software 
/// http://www.chickenscratchsoftware.com

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Ink;

namespace InkPicture_binding
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox textBox1;
		private System.Data.OleDb.OleDbConnection odbCN;
		private System.Data.OleDb.OleDbDataAdapter odbDA;
		private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
		private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
		private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
		private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
		private InkPicture_binding_problem.MyDS myDS1;
		private InkPicture_binding.InkDataPicture inkDataPicture1;
		private System.Windows.Forms.Button button2;

		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.myDS1 = new InkPicture_binding_problem.MyDS();
			this.odbCN = new System.Data.OleDb.OleDbConnection();
			this.odbDA = new System.Data.OleDb.OleDbDataAdapter();
			this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
			this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
			this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
			this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
			this.inkDataPicture1 = new InkPicture_binding.InkDataPicture();
			this.button2 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.myDS1)).BeginInit();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 128);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "<";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(176, 128);
			this.button3.Name = "button3";
			this.button3.TabIndex = 2;
			this.button3.Text = ">";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox1
			// 
			this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myDS1, "MyTable.TextField"));
			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(248, 20);
			this.textBox1.TabIndex = 4;
			this.textBox1.Text = "textBox1";
			// 
			// myDS1
			// 
			this.myDS1.DataSetName = "MyDS";
			this.myDS1.Locale = new System.Globalization.CultureInfo("en-US");
			// 
			// odbCN
			// 
			this.odbCN.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""M:\Documents and Settings\rhymed\My Documents\Visual Studio Projects\InkPicture_binding_problem\database.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False";
			// 
			// odbDA
			// 
			this.odbDA.DeleteCommand = this.oleDbDeleteCommand1;
			this.odbDA.InsertCommand = this.oleDbInsertCommand1;
			this.odbDA.SelectCommand = this.oleDbSelectCommand1;
			this.odbDA.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																							new System.Data.Common.DataTableMapping("Table", "MyTable", new System.Data.Common.DataColumnMapping[] {
																																																	   new System.Data.Common.DataColumnMapping("InkField", "InkField"),
																																																	   new System.Data.Common.DataColumnMapping("TextField", "TextField")})});
			this.odbDA.UpdateCommand = this.oleDbUpdateCommand1;
			// 
			// oleDbDeleteCommand1
			// 
			this.oleDbDeleteCommand1.CommandText = "DELETE FROM MyTable WHERE (TextField = ?)";
			this.oleDbDeleteCommand1.Connection = this.odbCN;
			this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_TextField", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "TextField", System.Data.DataRowVersion.Original, null));
			// 
			// oleDbInsertCommand1
			// 
			this.oleDbInsertCommand1.CommandText = "INSERT INTO MyTable(InkField, TextField) VALUES (?, ?)";
			this.oleDbInsertCommand1.Connection = this.odbCN;
			this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("InkField", System.Data.OleDb.OleDbType.VarBinary, 0, "InkField"));
			this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("TextField", System.Data.OleDb.OleDbType.VarWChar, 50, "TextField"));
			// 
			// oleDbSelectCommand1
			// 
			this.oleDbSelectCommand1.CommandText = "SELECT InkField, TextField FROM MyTable";
			this.oleDbSelectCommand1.Connection = this.odbCN;
			// 
			// oleDbUpdateCommand1
			// 
			this.oleDbUpdateCommand1.CommandText = "UPDATE MyTable SET InkField = ?, TextField = ? WHERE (TextField = ?)";
			this.oleDbUpdateCommand1.Connection = this.odbCN;
			this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("InkField", System.Data.OleDb.OleDbType.VarBinary, 0, "InkField"));
			this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("TextField", System.Data.OleDb.OleDbType.VarWChar, 50, "TextField"));
			this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_TextField", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "TextField", System.Data.DataRowVersion.Original, null));
			// 
			// inkDataPicture1
			// 
			this.inkDataPicture1.BackColor = System.Drawing.Color.White;
			this.inkDataPicture1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.inkDataPicture1.Location = new System.Drawing.Point(8, 32);
			this.inkDataPicture1.MarginX = -2147483648;
			this.inkDataPicture1.MarginY = -2147483648;
			this.inkDataPicture1.Name = "inkDataPicture1";
			this.inkDataPicture1.Size = new System.Drawing.Size(248, 80);
			this.inkDataPicture1.TabIndex = 5;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(96, 128);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 24);
			this.button2.TabIndex = 6;
			this.button2.Text = "Update";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(264, 166);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.inkDataPicture1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.myDS1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Binding binding = inkDataPicture1.DataBindings.Add("Ink",myDS1,"MyTable.InkField");
			binding.Format += new ConvertEventHandler(binding_Format);
			binding.Parse += new ConvertEventHandler(binding_Parse);

			odbDA.Fill(myDS1);
		}

		private void binding_Format(object sender, ConvertEventArgs e)
		{
			Ink ink = new Ink();
			if (e.DesiredType.Equals(typeof(Ink)))
			{
				if (e.Value.GetType().Equals(typeof(byte[]))) 
				{
					byte[] bytes = (byte[])e.Value;
					if (bytes.Length>0) 
					{
						ink.Load(bytes);
					}
				}
				e.Value = ink;
			}
		}
		private void binding_Parse(object sender, ConvertEventArgs e)
		{
			if (e.DesiredType.Equals(typeof(byte[])) && e.Value.GetType().Equals(typeof(Ink))) 
			{
				Ink ink = (Ink)e.Value;
				e.Value = ink.Save(PersistenceFormat.InkSerializedFormat);
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			CurrencyManager cm = (CurrencyManager)this.textBox1.BindingContext[myDS1, "MyTable"];
			cm.Position++;

		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			CurrencyManager cm = (CurrencyManager)this.textBox1.BindingContext[myDS1, "MyTable"];
			cm.Position--;
	
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			odbDA.Update(myDS1);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Rhy Mednick has been working in the field of software development professionally since 1990. The companies that he has worked for include DAK Industries, 1-800-CARSEARCH, and Microsoft. In 2003 he left Microsoft to start his own software company called Chicken Scratch Software (http://www.chickenscratchsoftware.com) to produce developer tools and end-user applications for the Tablet PC.

In 2005, Rhy returned to Microsoft and is currently working on an advanced technology team determining potential uses for new technologies in development by Microsoft Research. Any articles or comments provided here are completely independent of his relationship with Microsoft so should be treated that way. All views expressed are his personal views and all code samples provided were created independently of Microsoft.

Comments and Discussions