📄 configuration.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace Microsoft.Sql.SqlCe.Samples.Cs.IBuySpyDelivery.IBuySpyDevice
{
/// <summary>
/// Summary description for the Configuration control. This custom control is responsible
/// synchronizes with the server data store through either RDA or replication.
/// </summary>
public class Configuration : System.Windows.Forms.Control
{
/// The following enumeration contains the four possible values for configuration status.
/// Each value corresponds to one of the four steps required to configure the device.
///
private enum ConfigStatus
{
/// Specify a database server.
///
ConfigStart,
/// Select a driver.
///
ConfigDriver,
/// Choose the synchronization method (either RDA or replication).
///
ConfigSync,
/// Synchronize to the server database (through RDA or replication). When synchronization is complete, a local database exists on the device.
///
ConfigDone
};
private System.Windows.Forms.PictureBox pboIDegree;
private System.Windows.Forms.Button btnChangeDriver;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.ComboBox cboSyncMethod;
private System.Windows.Forms.Label lblSyncMethod;
private System.Windows.Forms.ComboBox cboDriverID;
private System.Windows.Forms.Label lblDriverID;
private System.Windows.Forms.TextBox txtServerName;
private System.Windows.Forms.Label lblServerName;
private System.Windows.Forms.Button btnNextOrFull;
private System.Windows.Forms.Button btnBackOrQuick;
private FormIBuySpy frmIBuySpy = null;
private IBuySpyData dataIBuySpy = null;
private ConfigStatus configStatus = ConfigStatus.ConfigStart;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Configuration(FormIBuySpy frmIBuySpy)
{
this.frmIBuySpy = frmIBuySpy;
/// This call is required by the Windows.Forms Form Designer.
///
InitializeComponent();
this.pboIDegree.Image = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("IBuySpyDevice.idegree.gif"));
Initialize();
UpdateForm();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component 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.btnChangeDriver = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.cboSyncMethod = new System.Windows.Forms.ComboBox();
this.lblSyncMethod = new System.Windows.Forms.Label();
this.cboDriverID = new System.Windows.Forms.ComboBox();
this.lblDriverID = new System.Windows.Forms.Label();
this.txtServerName = new System.Windows.Forms.TextBox();
this.btnNextOrFull = new System.Windows.Forms.Button();
this.btnBackOrQuick = new System.Windows.Forms.Button();
this.lblServerName = new System.Windows.Forms.Label();
this.pboIDegree = new System.Windows.Forms.PictureBox();
///
/// btnChangeDriver
///
this.btnChangeDriver.Location = new System.Drawing.Point(128, 96);
this.btnChangeDriver.Size = new System.Drawing.Size(104, 24);
this.btnChangeDriver.Text = "Change Driver";
this.btnChangeDriver.Click += new System.EventHandler(this.btnChangeDriver_Click);
///
/// btnReset
///
this.btnReset.Location = new System.Drawing.Point(8, 96);
this.btnReset.Size = new System.Drawing.Size(104, 24);
this.btnReset.Text = "Reset";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
///
/// cboSyncMethod
///
this.cboSyncMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboSyncMethod.Items.Add("Replication");
this.cboSyncMethod.Items.Add("RDA");
this.cboSyncMethod.Location = new System.Drawing.Point(88, 64);
this.cboSyncMethod.Size = new System.Drawing.Size(144, 21);
///
/// lblSyncMethod
///
this.lblSyncMethod.Location = new System.Drawing.Point(8, 67);
this.lblSyncMethod.Size = new System.Drawing.Size(80, 16);
this.lblSyncMethod.Text = "Sync Method:";
///
/// cboDriverID
///
this.cboDriverID.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboDriverID.Location = new System.Drawing.Point(88, 36);
this.cboDriverID.Size = new System.Drawing.Size(144, 21);
this.cboDriverID.SelectedIndexChanged += new System.EventHandler(this.cboDriverID_SelectedIndexChanged);
///
/// lblDriverID
///
this.lblDriverID.Location = new System.Drawing.Point(8, 38);
this.lblDriverID.Size = new System.Drawing.Size(56, 16);
this.lblDriverID.Text = "Driver ID:";
///
/// txtServerName
///
this.txtServerName.Location = new System.Drawing.Point(88, 8);
this.txtServerName.Size = new System.Drawing.Size(144, 20);
this.txtServerName.Text = "a-yiwang1";
///
/// btnNextOrFull
///
this.btnNextOrFull.Location = new System.Drawing.Point(128, 128);
this.btnNextOrFull.Size = new System.Drawing.Size(104, 24);
this.btnNextOrFull.Text = "Full Sync";
this.btnNextOrFull.Click += new System.EventHandler(this.btnNextOrFull_Click);
///
/// btnBackOrQuick
///
this.btnBackOrQuick.Location = new System.Drawing.Point(8, 128);
this.btnBackOrQuick.Size = new System.Drawing.Size(104, 24);
this.btnBackOrQuick.Text = "Quick Sync";
this.btnBackOrQuick.Click += new System.EventHandler(this.btnBackOrQuick_Click);
///
/// lblServerName
///
this.lblServerName.Location = new System.Drawing.Point(8, 8);
this.lblServerName.Size = new System.Drawing.Size(80, 16);
this.lblServerName.Text = "Server Name:";
///
/// pboIDegree
///
this.pboIDegree.Location = new System.Drawing.Point(8, 168);
this.pboIDegree.Size = new System.Drawing.Size(120, 55);
///
/// Configuration
///
this.Controls.Add(this.btnChangeDriver);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.cboSyncMethod);
this.Controls.Add(this.lblSyncMethod);
this.Controls.Add(this.cboDriverID);
this.Controls.Add(this.lblDriverID);
this.Controls.Add(this.txtServerName);
this.Controls.Add(this.btnNextOrFull);
this.Controls.Add(this.btnBackOrQuick);
this.Controls.Add(this.lblServerName);
this.Controls.Add(this.pboIDegree);
this.Size = new System.Drawing.Size(240, 240);
}
#endregion
/// This method initializes the Configuration control.
///
private void Initialize()
{
this.dataIBuySpy = IBuySpyData.GetInstance();
this.txtServerName.Text = this.dataIBuySpy.ServerName;
this.configStatus = ConfigStatus.ConfigStart;
try
{
/// If the database does not exist, no default values can be loaded; therefore, the method returns.
///
if (!File.Exists(this.dataIBuySpy.LocalDatabaseFile))
{
/// This creates the directory (if it does not already exist) on the device where the local database,
/// image files, and executable are stored. The default path is \Program Files\IBuySpyDelivery\.
///
if (!Directory.Exists(this.dataIBuySpy.ProgramPath))
{
Directory.CreateDirectory(this.dataIBuySpy.ProgramPath);
}
return;
}
}
catch(Exception e)
{
MessageBox.Show(e.Message, "IBuySpy Delivery");
return;
}
Cursor.ShowWaitCursor(true);
/// Load default configuration settings from the database.
///
try
{
dataIBuySpy.LoadSettings();
}
catch
{
return;
}
finally
{
Cursor.ShowWaitCursor(false);
}
/// Display the default configuration settings.
///
this.txtServerName.Text = this.dataIBuySpy.ServerName;
this.cboDriverID.Items.Add(this.dataIBuySpy.DriverID.ToString());
this.cboDriverID.Text = this.dataIBuySpy.DriverID.ToString();
this.cboSyncMethod.Text = (this.dataIBuySpy.SyncMethod == SyncType.Rda) ? "RDA" : "Replication";
/// Set the configuration status.
///
this.configStatus = ConfigStatus.ConfigDone;
}
/// This method deletes the local database, forcing the user to repeat the configuration steps
/// before being able to use the application.
///
private void btnReset_Click(object sender, System.EventArgs e)
{
if (DialogResult.OK == MessageBox.Show("You are about to reset all configuration settings. Continuing will discard all changes and delete the local database.",
"IBuySpy Delivery",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1))
{
if (ConnectionState.Open == this.dataIBuySpy.IBuySpyConnection.State)
{
this.dataIBuySpy.IBuySpyConnection.Close();
}
try
{
/// Delete the database.
///
if (File.Exists(this.dataIBuySpy.LocalDatabaseFile))
{
File.Delete(this.dataIBuySpy.LocalDatabaseFile);
}
}
catch(Exception err)
{
MessageBox.Show("Delete database file: " + err.Message, "IBuySpy Delivery");
}
this.configStatus = ConfigStatus.ConfigStart;
/// The user interface is updated.
///
UpdateForm();
}
}
/// The cboDriverID combo box is typically disabled after synchronization. This method enables
/// the combo box, allowing the user to select a different DriverID. The database on the server
/// is filtered using the column DriverID so that, during synchronization, the local database
/// receives the appropriate targeted dataset.
///
private void btnChangeDriver_Click(object sender, System.EventArgs e)
{
if (DialogResult.OK == MessageBox.Show("You are about to change the Driver ID. You must perform a Full Sync after selecting a new Driver ID before the change takes affect.",
"IBuySpy Delivery",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1))
{
Cursor.ShowWaitCursor(true);
try
{
try
{
/// Retrieve all the DriverID records.
///
LoadDriverIDs();
}
finally
{
Cursor.ShowWaitCursor(false);
}
}
catch(SqlCeException err)
{
IBuySpyData.ShowErrors(err);
return;
}
catch (Exception err)
{
MessageBox.Show("Load Driver IDs: " + err.Message, "IBuySpy Delivery");
return;
}
this.cboDriverID.SelectedValue = this.dataIBuySpy.DriverID;
this.cboDriverID.Enabled = true;
}
}
/// This method controls the behavior of the Back button, which also doubles as the Quick Sync button.
/// The Back button is active during configuration and the Quick Sync button is activated after
/// synchronization has occurred.
///
private void btnBackOrQuick_Click(object sender, System.EventArgs e)
{
switch(configStatus)
{
/// Back
///
case ConfigStatus.ConfigDriver:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -