using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BWP.WinFormControl { public class UComboBox : ComboBox { ToolTip itemTip = null; public UComboBox() { this.DropDown += (sender, de) => { CRefreshItems(); //if (string.IsNullOrEmpty(this.Text)) // return; //if (_enableTopItem && Items.Count > 1) // this.SelectedIndex = 1; //else if (!_enableTopItem && Items.Count > 0) // this.SelectedIndex = 0; }; this.DrawMode = DrawMode.OwnerDrawFixed; this.DrawItem += new DrawItemEventHandler(cb_DrawItem); this.DropDownClosed += new EventHandler(cb_DropDownClosed); itemTip = new ToolTip(); itemTip.SetToolTip(this, ""); } void cb_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // 绘制背景 e.DrawBackground(); //绘制列表项目 e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, System.Drawing.Brushes.Black, e.Bounds); //将高亮的列表项目的文字传递到toolTip1(之前建立ToolTip的一个实例) if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) itemTip.Show(this.Items[e.Index].ToString(), this, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height); e.DrawFocusRectangle(); } void cb_DropDownClosed(object sender, EventArgs e) { itemTip.Hide(this); } public string DefaultServerPath = "/MainSystem/B3ClientService/Rpcs/"; public string CodeArgs { get; set; } private bool _enableTopItem = true; public bool EnableTopItem { get { return _enableTopItem; } set { _enableTopItem = value; } } private int range = 10; public int Range { get { return range; } set { range = value; } } private string rpcPath; public void Init(string _rpcPath) { rpcPath = _rpcPath; if (string.IsNullOrEmpty(rpcPath)) return; rpcPath = string.Format("{0}{1}", DefaultServerPath, rpcPath); } protected override void OnKeyUp(KeyEventArgs e) { base.OnKeyUp(e); if (e.KeyCode == System.Windows.Forms.Keys.Enter) { if (string.IsNullOrEmpty(this.Text) || this.DisplayValue != this.Text || string.IsNullOrEmpty(this.SelectedText)) { if (!this.DroppedDown) this.DroppedDown = true; } } } private new WordPair SelectedValue { get { if (this.SelectedItem == null) return null; var result = this.SelectedItem as WordPair; if (string.IsNullOrEmpty(result.PhyName) && string.IsNullOrEmpty(result.DisplayName)) return null; return result; } } void CRefreshItems() { var inputArgs = string.Empty; if (this.SelectedValue == null) inputArgs = this.Text; var list = ComboBoxHelper.GetList(rpcPath, inputArgs, CodeArgs, Range); if (_enableTopItem) list.Insert(0, new WordPair(string.Empty)); this.Items.Clear(); foreach (var item in list) this.Items.Add(item); this.DisplayMember = "DisplayName"; this.ValueMember = "PhyName"; } public void Fill(string displayValue, string phyValue) { this.SelectedItem = new WordPair(displayValue, phyValue); } public bool IsEmpty { get { return SelectedValue == null; } } public string DisplayValue { get { if (SelectedValue == null) return string.Empty; return SelectedValue.DisplayName; } } public string Value { get { if (SelectedValue == null) return string.Empty; return SelectedValue.PhyName; } } public long? LongValue { get { if (SelectedValue == null) return null; long v; if (long.TryParse(SelectedValue.PhyName, out v)) return v; return null; } } } }