using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using TradeIdeas.TIProData; using TradeIdeas.XML; using System.Xml; using TradeIdeas.TIProData.Configuration; using System.Windows.Forms; namespace TradeIdeas.TIProGUI { public partial class StandardColumnPicker : Form { private const string VERBOSE_COLUMN_HEADER = "Description"; //Start location of Picker window when first brought up... //private static int _posX = 50; //private static int _posY = 50; private const int SPACER = 5; //for fonts and spaces between items... // the DataGridView to which this Picker is attached private DataGridView _dataGrid = null; List _columns = new List(); OverriddenCheckedListBox ochkListBox = new OverriddenCheckedListBox(); //need to do this for ItemHeight private FontManager _fontManager; private const float DEFAULT_FONT_SIZE = 9.75F; public StandardColumnPicker(DataGridView dgv, List Columns) { ochkListBox.Location = new Point(12, 15); ochkListBox.Height = 135; ochkListBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; ochkListBox.ItemCheck +=new ItemCheckEventHandler(ochkListBox_ItemCheck); ochkListBox.CheckOnClick = true; ochkListBox.BorderStyle = BorderStyle.None; ochkListBox.Font = new Font(FontFamily.GenericSansSerif, 10); ochkListBox.BackColor = SystemColors.Control; this.StartPosition = FormStartPosition.CenterParent; InitializeComponent(); this.Controls.Add(ochkListBox); _dataGrid = dgv; _columns = Columns; List phrases = GuiEnvironment.XmlConfig.Node("ALERT_WINDOW").Node("PHRASES"); this.Text = phrases.Node("HIDE_SHOW").PropertyForCulture("TEXT", "***"); loadTheColumns(); Icon = GuiEnvironment.Icon; _fontManager = new FontManager(this, DEFAULT_FONT_SIZE); _fontManager.selectTheFont(); ochkListBox.ItemHeight = Font.Height + SPACER; } private void loadTheColumns() { //Populate the listBox with the checkBox and associated special columns for (int i = 0; i < _dataGrid.Columns.Count; i++) { if (isColumnSpecial(_dataGrid.Columns[i].HeaderText)) { ochkListBox.Items.Add(_dataGrid.Columns[i].HeaderText, _dataGrid.Columns[i].Visible); } } } private bool isColumnSpecial(string textHeader) { /*The IList Columns can hold the list in one order, * but if we re-arrange the positioning of columns, bugs can be introduced where the check boxes might not have a corresponding textual description (that is, the column could be that of a filter and not a "special column"*/ foreach (ColumnInfo c in _columns) { if ((textHeader == c.Description) && c.TextHeader == true) { return true; } } return false; } private int getIndex(string name) { int i = 0; foreach (Column c in _dataGrid.Columns) { if (c.HeaderText == name) { return i; } i++; } return -1; //we shouldn't get here!! } private void ochkListBox_ItemCheck(object sender, ItemCheckEventArgs e) { //When this Picker *first* loads up, this method will be triggered. // when the columns are in the process of being loaded. //In this scenario the checkBox has a "null" selected index so //we must catch that exception. CheckedListBox checkBox = (CheckedListBox)sender; string name; try { name = checkBox.SelectedItem.ToString(); } catch (NullReferenceException) { name = ""; } //When a box is checked/unchecked, that particular column visibility is affected if (e.NewValue == CheckState.Checked) { if (name != "") { _dataGrid.Columns[getIndex(name)].Visible = true; } //For some reason, I had to do the AutoSizeRowsMode to "none" //followed by AllCells in order to have the whole grid(presumably) //adjust itself to the text-wrap feature (not just those rows for the //newest alerts...If I didn't set to "None" first, then the wrap feature //would still only work for the newest alert entries...strange... _dataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; _dataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; } else { if (name != "") { _dataGrid.Columns[getIndex(name)].Visible = false; } } } /* Comment out event so that the window position can be centered over its parent private void StandardColumnPicker_VisibleChanged(object sender, EventArgs e) { //Here we retain the location when this window is created again if (!this.Visible) { _posX = Location.X; _posY = Location.Y; } else { Location = new Point(_posX, _posY); } } * */ } public class OverriddenCheckedListBox : CheckedListBox { //using the standard CheckListBox class, the ItemHeight is hardcoded and cannot be accessed. This also means //that the bottom portions of the "y's" and "g's" can sometimes get cut off from view. // So, had to make an overriden class which derives from the CheckedListBox class in order // to set the ItemHeight(controls spacing between items in a checkedlistbox) public override int ItemHeight { get; set; } } }