public void SetTextBox(TextBoxDropDown txt) { this.txt = txt; this.txt.DropDownControl = this.chkBox; this.otherValues.Clear(); //Find values do not exist in the CheckedListBox.Items Dictionary<string, int> items = new Dictionary<string, int>(); for (int i=0; i<this.chkBox.Items.Count ; i++) { object obj = this.chkBox.Items[i]; items.Add(obj.ToString().Trim(), i); this.chkBox.SetItemChecked(i, false); } if (!string.IsNullOrEmpty(this.txt.Text.Trim())) { string[] values = this.txt.Text.Split(new char[] { ',' }); foreach (string str in values) { if (!string.IsNullOrEmpty(str.Trim())) { if (!items.ContainsKey(str.Trim())) { this.otherValues.Add(str.Trim()); } else //有比對到,則 ckeck this item { this.chkBox.SetItemChecked(items[str.Trim()], true); } } } // end foreach } // end if }
/// <summary> /// Constructor /// </summary> public DataGridViewTextBoxDropDownColumn() { CellTemplate = new DataGridViewTextBoxDropDownCell(); _TextBoxDropDown = new TextBoxDropDown(); _TextBoxDropDown.BackgroundStyle.Class = ElementStyleClassKeys.DataGridViewIpAddressBorderKey; }
protected virtual Control CreateNodeEditor() { bool isReadOnly = GetEffectiveReadOnly(); //PropertyValueEditor valueEditor = _Property.Attributes[typeof(PropertyValueEditor)] as PropertyValueEditor; //if (valueEditor != null) //{ // IPropertyValueEditor editorCustom = valueEditor.CreateEditor(_Property, _TargetComponent); // editorCustom.EditValueChanged += ValueEditorEditValueChanged; // return (Control)editorCustom; //} TextBoxDropDown editor = new TextBoxDropDown(); if (BarUtilities.UseTextRenderer) editor.BackgroundStyle.MarginLeft = 3; editor.Size = new Size(32, editor.PreferredHeight); editor.Visible = true; editor.TextBox.KeyDown += new KeyEventHandler(EditorKeyDown); editor.TextBox.TextChanged += new EventHandler(EditorTextChanged); editor.TextBox.ReadOnly = isReadOnly; editor.TextBox.PreventEnterBeep = true; UITypeEditor typeEditor = GetTypeEditor(); UITypeEditorEditStyle editStyle = UITypeEditorEditStyle.None; if (typeEditor != null) { editStyle = typeEditor.GetEditStyle(this); if (editStyle == UITypeEditorEditStyle.Modal) { editor.ButtonCustom.Visible = true; editor.ButtonCustomClick += new EventHandler(EditorButtonCustomClick); } else if (editStyle == UITypeEditorEditStyle.DropDown) { editor.ButtonDropDown.Visible = true; editor.ButtonDropDownClick += new CancelEventHandler(EditorButtonDropDownClick); } if (!IsReadOnly) { PropertyDescriptor propertyDescriptor = GetPropertyDescriptor(); if (propertyDescriptor != null && !propertyDescriptor.PropertyType.IsAssignableFrom(typeof(string)) && propertyDescriptor.Converter != null && !propertyDescriptor.Converter.CanConvertFrom(typeof(string))) editor.TextBox.ReadOnly = true; } } if (typeEditor == null || editStyle == UITypeEditorEditStyle.None) { if (!isReadOnly) { SetupEditorAutoCompleteList(editor); if (!SetupEditorPopup(editor)) editor.ButtonDropDown.Visible = false; else { editor.ButtonDropDown.Visible = true; } } } if (AfterEditorCreated != null) AfterEditorCreated(this, editor); return editor; }
protected virtual void SetupEditorAutoCompleteList(TextBoxDropDown editor) { if (this.PropertyType == null) return; TextBox textBox = editor.TextBox; TypeConverter converter = this.TypeConverter; //if (converter != null && converter.GetStandardValuesExclusive(this)) // editor.InternalReadOnly = true; //else // editor.InternalReadOnly = false; string[] items = GetPopupListContent(); if (items != null && items.Length > 0) { textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; textBox.AutoCompleteCustomSource.Clear(); textBox.AutoCompleteCustomSource.AddRange(items); } }
protected virtual bool SetupEditorPopup(TextBoxDropDown editor) { if (this.PropertyType == null) return false; string[] items = GetPopupListContent(); if (items == null || items.Length == 0) return false; TextBoxDropDown textBox = editor; ListBox list = new ListBox(); list.SelectedIndexChanged += new EventHandler(PopupListSelectedIndexChanged); list.BorderStyle = BorderStyle.None; list.Items.AddRange(items); if (!string.IsNullOrEmpty(editor.Text)) list.SelectedItem = editor.Text; textBox.DropDownControl = list; list.Size = GetPreferredListBoxSize(ref items); return true; }
private Control createTextBoxDropDown(Question q) { int innerPanelLength = 0; FlowLayoutPanel pnlInner = new FlowLayoutPanel(); pnlInner.FlowDirection = FlowDirection.LeftToRight; pnlInner.Width = 120; //pnl.BackColor = System.Drawing.Color.Green; Label lbl = this.createLabel(q.GetQuestionLabel()); pnlInner.Controls.Add(lbl); innerPanelLength += lbl.Width + 6; TextBoxDropDown txt = new TextBoxDropDown(); txt.Enabled = false; txt.Name = q.GetQuestionName(); txt.DropDownControl = this.getDropDownControl(q); txt.ButtonDropDown.Text = "..."; txt.ButtonDropDown.Visible = true; txt.Width = 150; txt.Height = 20; txt.TextAlign = HorizontalAlignment.Left; txt.Tag = q; txt.TextChanged += new EventHandler(txt_TextChanged); txt.ButtonDropDownClick += new System.ComponentModel.CancelEventHandler(txt_ButtonDropDownClick); //調整寬度 pnlInner.Width = innerPanelLength + txt.Width + 6; if (!string.IsNullOrEmpty(q.GetWidth())) { if (q.GetWidth().ToUpper() == "FILL") { pnlInner.Width = this.contentPanel.Width - 12; txt.Width = pnlInner.Width - innerPanelLength - 12; } else { int width = 0; bool isnum = int.TryParse(q.GetWidth(), out width); if (isnum) { txt.Width = width; pnlInner.Width = innerPanelLength + txt.Width + 6; } } } //txt.Text = q.GetQuestionName(); pnlInner.Controls.Add(txt); this.allQControls.Add(txt.Name, txt); //pnlInner.Width = innerPanelLength + txt.Width + 6; pnlInner.Height = txt.Height + 6; //adjust height int periodCount = 4; this.contentPanel.Height = 6 + 20 * ((this.questionGroup.GetQuestions().Count % periodCount == 0) ? (this.questionGroup.GetQuestions().Count / periodCount + 1) : (this.questionGroup.GetListItems().Count / periodCount + 2)); this.pnlQGroup.Height = this.contentPanel.Height + 6; return pnlInner; }
/// <summary> /// DrawText /// </summary> /// <param name="di"></param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="cellStyle"></param> private void DrawText(TextBoxDropDown di, DataGridViewCellStyle cellStyle, Rectangle r, Graphics g) { r.Inflate(-2, 0); eTextFormat tf = eTextFormat.Default | eTextFormat.NoPrefix; switch (di.TextAlign) { case HorizontalAlignment.Center: tf |= eTextFormat.HorizontalCenter; break; case HorizontalAlignment.Right: tf |= eTextFormat.Right; break; } if (cellStyle.WrapMode == DataGridViewTriState.True) tf |= eTextFormat.WordBreak; switch (cellStyle.Alignment) { case DataGridViewContentAlignment.TopLeft: case DataGridViewContentAlignment.TopCenter: case DataGridViewContentAlignment.TopRight: tf |= eTextFormat.Top; break; case DataGridViewContentAlignment.BottomLeft: case DataGridViewContentAlignment.BottomCenter: case DataGridViewContentAlignment.BottomRight: tf |= eTextFormat.Bottom; break; default: tf |= eTextFormat.VerticalCenter; break; } TextDrawing.DrawString(g, di.Text, di.Font, di.ForeColor, r, tf); }
/// <summary> /// DrawControl /// </summary> /// <param name="di"></param> /// <param name="cellStyle"></param> /// <param name="r"></param> /// <param name="bm"></param> /// <param name="g"></param> private void DrawControl(TextBoxDropDown di, DataGridViewCellStyle cellStyle, Rectangle r, Bitmap bm, Graphics g) { if (di.ButtonGroup.Items.Count > 0) { using (Bitmap bm2 = new Bitmap(bm)) { di.Bounds = r; di.DrawToBitmap(bm2, r); foreach (VisualItem item in di.ButtonGroup.Items) { if (item.Visible == true) { Rectangle t = item.RenderBounds; t.X += r.X; t.Y += r.Y; g.DrawImage(bm2, t, t, GraphicsUnit.Pixel); if (t.Left < r.Right) r.Width -= (r.Right - t.Left - 1); } } } } DrawText(di, cellStyle, r, g); }