using System; using System.Collections.Generic; using System.Windows.Forms; namespace Sample { public partial class RandomAccessSettingPanel : UserControl { #region Enums private enum RandomDataTypes { /// /// Integer type /// W, /// /// Long integer type /// L, /// /// 4-precision integer type /// Q, /// /// Single precision real type /// F, /// /// Double precision real type /// D, } #endregion #region Consts /// /// Maximum address number at 16 bits /// private const UInt64 SIXTEEN_MAXADDRESSNO = 65535; /// /// Maximum address number at 32 bits /// private const UInt64 THIRTYTWO_MAXADDRESSNO = 4294967295; #endregion #region Feilds private bool isHex = false; #endregion #region Constructor public RandomAccessSettingPanel() { InitializeComponent(); this.UpdateRegisterInfo(Bits.SixteenBit); this.vtDataTypeComboBox.DataSource = Enum.GetValues(typeof(VTDataTypes)); this.dataTypeComboBox.DataSource = Enum.GetValues(typeof(RandomDataTypes)); this.UpdateVTDataType(); foreach (ColumnHeader columnHeader in addedRegistListView.Columns) { columnHeader.Width = -2; } } #endregion #region Event /// /// When the add button is pressed /// /// /// private void addButton_Click(object sender, EventArgs e) { String addressNoString = String.Empty; if (isHex) { addressNoString = String.Format("{0:X}", Convert.ToUInt64(this.addressNoNumericUpDown.Value)); } else { addressNoString = this.addressNoNumericUpDown.Value.ToString(); } String referenceString = String.Format("{0}{1}{2}", this.registerTypeComboBox.SelectedValue.ToString(), this.dataTypeComboBox.SelectedValue.ToString(), addressNoString); String[] item = { referenceString, this.vtDataTypeComboBox.SelectedValue.ToString() }; this.addedRegistListView.Items.Add(new ListViewItem(item)); } private void deleteButton_Click(object sender, EventArgs e) { if (this.addedRegistListView.SelectedItems.Count > 0) { this.addedRegistListView.Items.Remove(this.addedRegistListView.SelectedItems[0]); } else { if (this.addedRegistListView.Items.Count > 0) { this.addedRegistListView.Items.Remove(this.addedRegistListView.Items[this.addedRegistListView.Items.Count - 1]); } } } private void dataTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { this.UpdateVTDataType(); } private void registerTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { this.UpdateHexDecimal(); } #endregion #region Methods public String GetOptionString() { String optionString = String.Empty; String referenceString = String.Empty; String vtDataTypeString = String.Empty; bool isFirst = true; if (this.addedRegistListView.Items.Count > 1) { foreach (ListViewItem item in this.addedRegistListView.Items) { bool isVTDataType = false; if (isFirst) { foreach (ListViewItem.ListViewSubItem subItem in item.SubItems) { if (!isVTDataType) { referenceString += subItem.Text; isVTDataType = true; } else { vtDataTypeString += subItem.Text; } } isFirst = false; } else { foreach (ListViewItem.ListViewSubItem subItem in item.SubItems) { if (!isVTDataType) { referenceString += ":"; referenceString += subItem.Text; isVTDataType = true; } else { vtDataTypeString += ":"; vtDataTypeString += subItem.Text; } } } } } optionString = String.Format("Reference={0},VT={1}", referenceString, vtDataTypeString); return optionString; } /// /// Number of array elements /// /// public UInt64 GetElementCount() { return Convert.ToUInt64(this.addedRegistListView.Items.Count); } /// /// Number of array elements /// /// public List GetVtDataTypeList() { List list = new List(); foreach (ListViewItem item in this.addedRegistListView.Items) { bool isVTDataType = false; foreach (ListViewItem.ListViewSubItem subItem in item.SubItems) { if (!isVTDataType) { isVTDataType = true; } else { list.Add(subItem.Text); } } } return list; } /// /// Update register type according to address capacity /// /// Address capacity public void UpdateRegisterInfo(Bits bits) { if (bits == Bits.SixteenBit) { this.registerTypeComboBox.DataSource = Enum.GetValues(typeof(SixTeenRegisterTypes)); this.addressNoNumericUpDown.Maximum = SIXTEEN_MAXADDRESSNO; } else { this.registerTypeComboBox.DataSource = Enum.GetValues(typeof(ThirtyTwoRegisterTypes)); this.addressNoNumericUpDown.Maximum = THIRTYTWO_MAXADDRESSNO; } } /// /// 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 VT data type according to data type /// private void UpdateVTDataType() { switch (this.dataTypeComboBox.SelectedValue) { case RandomDataTypes.W: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I2); break; case RandomDataTypes.L: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I4); break; case RandomDataTypes.Q: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.I8); break; case RandomDataTypes.F: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.R4); break; case RandomDataTypes.D: this.vtDataTypeComboBox.SelectedIndex = Convert.ToInt32(VTDataTypes.R8); break; } } #endregion } }