using System; using System.Windows.Forms; namespace Sample { public partial class NonRandomAccessSettingPanel : UserControl { #region Enums /// /// VT data type /// public enum BitVTDataTypes { /// /// VT_BOOL /// BOOL, /// /// VT_I1 /// I1, /// /// VT_UI1 /// UI1, /// /// VT_I2 /// I2, /// /// VT_UI2 /// UI2, /// /// VT_I4 /// I4, /// /// VT_UI4 /// UI4, /// /// VT_I8 /// I8, /// /// VT_UI8 /// UI8, /// /// VT_R4 /// R4, /// /// VT_R8 /// R8, } #endregion #region Consts /// /// Maximum address number when offset is selected at 16 bits /// private const UInt64 SIXTEEN_OFFSET_MAXADDRESSNO = 4095; /// /// Maximum address number at 16 bits /// private const UInt64 SIXTEEN_MAXADDRESSNO = 65535; /// /// Maximum address number when offset is selected at 32 bits /// private const UInt64 THIRTYTWO_OFFSET_MAXADDRESSNO = 268435455; /// /// Maximum address number at 32 bits /// private const UInt64 THIRTYTWO_MAXADDRESSNO = 4294967295; #endregion #region Feilds private bool isOffset = true; private bool isHex = false; private Bits currentBit = Bits.SixteenBit; #endregion #region Constructor /// /// Constructor /// public NonRandomAccessSettingPanel() { InitializeComponent(); this.dataTypeComboBox.DataSource = Enum.GetValues(typeof(DataTypes)); this.vtDataTypeComboBox.DataSource = Enum.GetValues(typeof(BitVTDataTypes)); this.UpdateRegisterInfo(Bits.SixteenBit); this.UpdateHexDecimal(); this.UpdateVTDataType(); this.UpdateCodePageVisible(); } #endregion #region Event /// /// When the data type selection is changed /// /// /// private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (((DataTypes)this.dataTypeComboBox.SelectedValue) == DataTypes.B) { this.vtDataTypeComboBox.DataSource = Enum.GetValues(typeof(BitVTDataTypes)); } else { this.vtDataTypeComboBox.DataSource = Enum.GetValues(typeof(VTDataTypes)); } this.UpdateOffset(); this.UpdateMaxAddressNo(); this.UpdateVTDataType(); } /// /// When the register type selection is changed /// /// /// private void registerTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { this.UpdateHexDecimal(); } /// /// When the number of elements is changed /// /// /// private void elemNumericUpDown_ValueChanged(object sender, EventArgs e) { if (this.elemNumericUpDown.Value > 1) { this.arrayCheckBox.Checked = true; this.arrayCheckBox.Enabled = false; } else { this.arrayCheckBox.Checked = false; this.arrayCheckBox.Enabled = true; } } /// /// When the VT data type is changed /// /// /// private void vtDataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { this.UpdateCodePageVisible(); } #endregion #region Methods /// /// Get option string /// /// Option string public String GetOptionString() { String optionString = String.Empty; String addressNoString = String.Empty; String referenceString = String.Empty; if (isHex) { addressNoString = String.Format("{0:X}" , Convert.ToUInt64(this.addressNoNumericUpDown.Value)); } else { addressNoString = this.addressNoNumericUpDown.Value.ToString(); } if (isOffset) { referenceString = String.Format("Reference={0}{1}{2}{3:X}", this.registerTypeComboBox.SelectedValue.ToString(), this.dataTypeComboBox.SelectedValue.ToString(), addressNoString, Convert.ToInt16(this.offsetNumericUpDown.Value)); } else { referenceString = String.Format("Reference={0}{1}{2}", this.registerTypeComboBox.SelectedValue.ToString(), this.dataTypeComboBox.SelectedValue.ToString(), addressNoString); } optionString = String.Format("{0},Elem={1},VT={2},Array={3},Codepage={4}", referenceString, this.elemNumericUpDown.Value, this.vtDataTypeComboBox.SelectedValue.ToString(), this.arrayCheckBox.Checked, Convert.ToUInt32(this.codePageNumericUpDown.Value)); return optionString; } /// /// Number of array elements /// /// public UInt64 GetElementCount() { return Convert.ToUInt64(this.elemNumericUpDown.Value); } /// /// Update register type according to address capacity /// /// Address capacity public void UpdateRegisterInfo(Bits bit) { this.currentBit = bit; if (this.currentBit == Bits.SixteenBit) { this.registerTypeComboBox.DataSource = Enum.GetValues(typeof(SixTeenRegisterTypes)); this.UpdateMaxAddressNo(); } else { this.registerTypeComboBox.DataSource = Enum.GetValues(typeof(ThirtyTwoRegisterTypes)); this.UpdateMaxAddressNo(); } } /// /// Update maximum address number /// /// private void UpdateMaxAddressNo() { if (this.currentBit == Bits.SixteenBit) { switch (this.dataTypeComboBox.SelectedValue) { case DataTypes.B: this.addressNoNumericUpDown.Maximum = SIXTEEN_OFFSET_MAXADDRESSNO; break; default: this.addressNoNumericUpDown.Maximum = SIXTEEN_MAXADDRESSNO; break; } } else { switch (this.dataTypeComboBox.SelectedValue) { case DataTypes.B: this.addressNoNumericUpDown.Maximum = THIRTYTWO_OFFSET_MAXADDRESSNO; break; default: this.addressNoNumericUpDown.Maximum = THIRTYTWO_MAXADDRESSNO; break; } } } /// /// Update VT data type according to data type /// private void UpdateVTDataType() { switch (this.dataTypeComboBox.SelectedValue) { case DataTypes.B: case DataTypes.W: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I2); break; case DataTypes.L: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I4); break; case DataTypes.Q: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I8); break; case DataTypes.F: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.R4); break; case DataTypes.D: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.R8); break; } } /// /// Update VT data type according to data type /// private void UpdateOffset() { switch (this.dataTypeComboBox.SelectedValue) { case DataTypes.B: this.isOffset = true; this.offsetLabel.Visible = isOffset; this.offsetNumericUpDown.Visible = isOffset; break; default: this.isOffset = false; this.offsetLabel.Visible = isOffset; this.offsetNumericUpDown.Visible = isOffset; break; } } /// /// Update hexadecimal number of address number /// private void UpdateHexDecimal() { if (this.registerTypeComboBox.SelectedValue.GetType() == typeof(SixTeenRegisterTypes)) { switch (this.registerTypeComboBox.SelectedValue) { case SixTeenRegisterTypes.M: this.isHex = false; this.addressNoNumericUpDown.Hexadecimal = this.isHex; break; case SixTeenRegisterTypes.I: this.isHex = true; this.addressNoNumericUpDown.Hexadecimal = this.isHex; break; } } else { switch (this.registerTypeComboBox.SelectedValue) { case ThirtyTwoRegisterTypes.S: case ThirtyTwoRegisterTypes.M: case ThirtyTwoRegisterTypes.G: this.isHex = false; this.addressNoNumericUpDown.Hexadecimal = this.isHex; break; case ThirtyTwoRegisterTypes.I: case ThirtyTwoRegisterTypes.O: this.isHex = true; this.addressNoNumericUpDown.Hexadecimal = this.isHex; break; } } } /// /// Update code page display /// private void UpdateCodePageVisible() { if (this.vtDataTypeComboBox.DataSource.GetType() == typeof(VTDataTypes[])) { switch (this.vtDataTypeComboBox.SelectedValue) { case VTDataTypes.BSTR: this.codePageLabel.Visible = true; this.codePageNumericUpDown.Visible = true; break; default: this.codePageLabel.Visible = false; this.codePageNumericUpDown.Visible = false; break; } } else { this.codePageLabel.Visible = false; this.codePageNumericUpDown.Visible = false; } } /// /// Get number of elements /// /// public int GetElemCount() { return Convert.ToInt32(this.elemNumericUpDown.Value); } /// /// Get VT data Types /// /// public VTDataTypes GetVTDataType() { return (VTDataTypes)this.vtDataTypeComboBox.SelectedValue; } /// /// Get whether the data is an array /// /// public bool GetIsArray() { return this.arrayCheckBox.Checked; } #endregion } }