using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace ClabGui { /// /// Partial GUI class for drawing a variable and it's domains. /// public partial class VarParaGui : UserControl { private MainGui mainGui; private int id; private int selectedIndex = -1; private Hashtable valueToComboIndex; private StringComparer stringComparer; private VarParameters varParameters; private Boolean allowIndexChangedEvent = true; /// /// Initializes a new instance of the class. /// /// The . /// The variable id. /// Name of the variable. /// The domain values. public VarParaGui(MainGui mainGui, int id, String varName, List domainV) { stringComparer = StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true); valueToComboIndex = new Hashtable(stringComparer); this.mainGui = mainGui; this.id = id; varParameters = new VarParameters(domainV); InitializeComponent(); this.varName.Text = varName; domainVCombo.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.domainVCombo_DrawItem); for (int i = 0; i < domainV.Count; i++) { domainVCombo.Items.Add(domainV[i]); valueToComboIndex[domainV[i]] = i; } } private void domainVCombo_SelectedIndexChanged(object sender, EventArgs e) { if (allowIndexChangedEvent && domainVCombo.SelectedIndex != -1) { String selected = (String)domainVCombo.SelectedItem; if ( varParameters.GetStatus(selected) == ParameterBitmapSingleton.AVAILABLE) { Boolean clabNotFinnished = mainGui.RunValueChosenSearch(id, selected); if (!clabNotFinnished) { varParameters.UserSelectedVal(selected); selectedIndex = domainVCombo.SelectedIndex; allowIndexChangedEvent = false; } else { domainVCombo.SelectedIndex = -1; } } } if (selectedIndex != -1 && domainVCombo.SelectedIndex != selectedIndex) { domainVCombo.SelectedIndex = selectedIndex; } } /// /// Updates the domain values with the new valid domains. /// /// The valid domain values. public void UpdateDomainValues(List validDomainValues) { varParameters.ValidDomainVals(validDomainValues); if (validDomainValues.Count == 1) { String val = validDomainValues[0]; int index = (int)valueToComboIndex[val]; selectedIndex = index; domainVCombo.SelectedIndex = index; } } private void domainVCombo_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Graphics g = e.Graphics ; Font fn = null ; if (e.Index >= 0) { int offset = 1; fn = (Font)new Font("Arial", 10, FontStyle.Regular); string s = (string)domainVCombo.Items[e.Index]; // Set the string format options StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; // Draw the rectangle RectangleF itemRect = new RectangleF((float)(e.Bounds.X + 16 + offset), (float)(e.Bounds.Y), (float)(e.Bounds.Width - 16 - offset), (float)(e.Bounds.Height)); if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { // if the item is not selected draw it with a different color e.Graphics.FillRectangle(new SolidBrush(Color.White), itemRect); e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), itemRect, sf); e.DrawFocusRectangle(); } else { //Bitmap ImageToDraw = new Bitmap(test, e.Bounds.Height - 1, e.Bounds.Height - 1); Bitmap statusBitmap = varParameters.GetDomainValBitmap(s); Bitmap ImageToDraw = new Bitmap(statusBitmap, e.Bounds.Height - 1, e.Bounds.Height - 1); e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), itemRect, sf); Rectangle imageRect = new Rectangle(e.Bounds.X + offset, e.Bounds.Y, 16, 16); e.Graphics.DrawImage(ImageToDraw, imageRect); // if the item is selected draw it with a different color //e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r); //e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf); e.DrawFocusRectangle(); } } } } }