/// <summary> /// 添加一项到列表中 /// </summary> /// <param name="text"></param> public void AddItem(string text, bool ischecked = false) { try { //添加到ToolStripDropDown的Items中 //将ComboBox列表中的项添加到ToolStripDropDown的Items中 if (myToolStripDropDown != null) { myToolStripDropDown.AutoSize = true; myToolStripButton dropDownItem = new myToolStripButton(text); dropDownItem.AutoSize = false; dropDownItem.Image = Resources.icon_normal; dropDownItem.ImageAlign = ContentAlignment.MiddleLeft; dropDownItem.TextAlign = ContentAlignment.MiddleLeft; dropDownItem.Size = new Size(this.pnlTopParent.Width, 32); //插入项"清空地址栏下列表"之前 if (ischecked) { if (PressedItem != null) { PressedItem.IsSelected = false; } dropDownItem.IsSelected = true; PressedItem = dropDownItem; } myToolStripDropDown.Items.Insert(myToolStripDropDown.Items.Count - 1, dropDownItem); myToolStripDropDown.Update(); //myToolStripDropDown.Items.Add(dropDownItem); } } catch (Exception ex) { //Win32API.OutputDebugStringA(string.Format("添加时遇到错误:{0}",ex.Message)); } }
public void AddButtonItem(string btntext) { try { //将清空地址栏列表加入ToolStripDropDown的Items中 myToolStripDropDown.Items.Clear(); //添加下载项 myToolStripButton dropDownItem = new myToolStripButton(btntext); //"清空地址栏下拉列表" //dropDownItem.Text = tmpitem; dropDownItem.AutoSize = false; dropDownItem.Image = Resources.clearItem; dropDownItem.ImageAlign = ContentAlignment.MiddleRight; dropDownItem.TextAlign = ContentAlignment.MiddleRight; dropDownItem.Size = new Size(this.pnlTopParent.Width, 32); myToolStripDropDown.Items.Add(dropDownItem); } catch (Exception ex) { } }
private void MyToolStripDropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e) { try { myDropDownStatus = DropDownStatus.Closing; pboxArrow.Enabled = true; DropDownOpened = false; this.pboxArrow.Image = Properties.Resources.bArrow; //判断当前选择的项 inputurl = ""; } catch (Exception ex) { } //如果弹出了下拉列表框没有选择一项,则直接返回 if (!bolDropDownMouseDown) { //return; } //选择了项,为下一次判断初始化为false bolDropDownMouseDown = false; try { //判断当前选择的项 foreach (myToolStripButton titem in myToolStripDropDown.Items) { //当前项,鼠标悬停,并不是真正意义上的点击选中 //Win32API.OutputDebugStringA(string.Format("所在项的 Pressed 为:{0}", titem.Pressed)); if (titem.Pressed) { inputurl = titem.Text; if (!inputurl.Equals(ClearTips))//"清空地址栏下拉列表" { titem.IsSelected = true; PressedItem = titem; //触发选择了某项事件 if (SelectionChanged != null) { SelectionChanged(this, e); } } else { titem.IsSelected = false; } } else { titem.IsSelected = false; } //Win32API.OutputDebugStringA(string.Format("所在项的IsSelected为:{0}", titem.IsSelected)); } //重新遍历一遍myToolStripDropDown.Items,设置当前地址为选定项 foreach (myToolStripButton titem in myToolStripDropDown.Items) { if (titem == PressedItem) { titem.IsSelected = true; } else { titem.IsSelected = false; } } myToolStripDropDown.Update(); if ((string.IsNullOrEmpty(inputurl) || inputurl.Length <= 0)) { return; } else if (inputurl.Equals(ClearTips)) //"清空地址栏下拉列表" { //Win32API.OutputDebugStringA(string.Format("在 myToolStripDropDown.Items中包含了:{0}个项目.", myToolStripDropDown.Items.Count )); int iloopcount = myToolStripDropDown.Items.Count - 1; for (int i = iloopcount; i >= 0; i--) { myToolStripButton item = myToolStripDropDown.Items[i] as myToolStripButton; if (!item.Text.Equals(ClearTips))//"清空地址栏下拉列表" { myToolStripDropDown.Items.Remove(item); //列表中删除该项 _items.Remove(item.Text); } } myToolStripDropDown.Update(); } else { this.chboxAddress.Text = inputurl; inputurl = ""; } //this.cboxChildbox.DroppedDown = true; } catch (Exception ex) { pboxArrow.Enabled = true; } //触发事件 try { if (DropDownClosed != null) { DropDownClosed(this, e); } } catch (Exception ex) { } //throw new NotImplementedException(); }