using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using RM.Interop.ReportMachine;

namespace RMDemo_CSharpe
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class FormStream : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Button button1;
		private System.Data.OleDb.OleDbConnection oleDbConnection1;
		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 System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
		private RMReportEngine.RMReport rmReport1;
		private System.Windows.Forms.Button button2;
        private System.Data.DataSet dataSet11;
		private System.Windows.Forms.Button button3;
        private Button button4;
		private System.ComponentModel.IContainer components;

		public FormStream()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormStream));
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
            this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
            this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
            this.button2 = new System.Windows.Forms.Button();
            this.dataSet11 = new System.Data.DataSet();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.rmReport1 = new RMReportEngine.RMReport();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(24, 138);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(128, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Design Report";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // oleDbConnection1
            // 
            this.oleDbConnection1.ConnectionString = resources.GetString("oleDbConnection1.ConnectionString");
            // 
            // oleDbSelectCommand1
            // 
            this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
            // 
            // oleDbDataAdapter1
            // 
            this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
            this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
            this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
            this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(53, 12);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(128, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "Load From Stream";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // dataSet11
            // 
            this.dataSet11.DataSetName = "DataSet1";
            this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(53, 41);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(128, 23);
            this.button3.TabIndex = 4;
            this.button3.Text = "Save to Stream";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(158, 138);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(128, 23);
            this.button4.TabIndex = 5;
            this.button4.Text = "Show Report";
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // rmReport1
            // 
            this.rmReport1.OnDesignerSaveReport += new RMReportEngine.RMReport.TRMOnSaveReportEvent(this.rmReport1_OnDesginerSaveReport);
            // 
            // FormStream
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(304, 189);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "FormStream";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Stream Demo";
            this.Load += new System.EventHandler(this.FormStream_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			
				rmReport1.DesignReport();
							
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			FileStream fs;
			Byte[] lBytes;

            fs = new FileStream(@"reports\StreamDemo.rmf", FileMode.Open, FileAccess.Read);
			lBytes = new Byte[fs.Length];
			fs.Read(lBytes, 0, (int)fs.Length);

			rmReport1.Init(this, TxRMReportType.rmrtReport);
			rmReport1.AddDataSet(this.dataSet11.Tables["Customer"], "db1");
			rmReport1.ReportData = lBytes;

			fs.Close();
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			FileStream fs;
			Byte[] lBytes;

			fs = new FileStream(@"reports\StreamDemo.rmf", FileMode.Create);
            lBytes = new Byte[rmReport1.ReportData.Length];
			lBytes = rmReport1.ReportData;
			
			//rmReport1.SaveReportToStream(lWriter.BaseStream);
			fs.Write(lBytes, 0, (int)lBytes.Length);

			fs.Close();
		}

        private void button4_Click(object sender, EventArgs e)
        {
            rmReport1.ShowReport();
        }

        private void rmReport1_OnDesginerSaveReport(object sender, AxRM.Interop.ReportMachine.IRMReportEngineEvents_OnDesignerSaveReportEvent e)
        {
            
            FileStream fs;
            Byte[] lBytes;

            fs = new FileStream(@"reports\StreamDemo.rmf", FileMode.Create);
            lBytes = new Byte[rmReport1.ReportData.Length];
            lBytes = rmReport1.ReportData;

            //rmReport1.SaveReportToStream(lWriter.BaseStream);
            fs.Write(lBytes, 0, (int)lBytes.Length);

            fs.Close();

            e.aSaved = true;
        }

        private void FormStream_Load(object sender, EventArgs e)
        {
            this.dataSet11.Reset();

            this.oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * from customer";
            this.oleDbDataAdapter1.Fill(this.dataSet11, "Customer");

            rmReport1.Init(this, TxRMReportType.rmrtReport);
            rmReport1.ShowProgress = false;

        }
	}
}
