toolstripdropdown https://www.e-learn.cn/tag/toolstripdropdown zh-hans Debugger Visualizer Winform ToolStripDropDownMenu ComboBox only shows items when first shown https://www.e-learn.cn/topic/3657643 <span>Debugger Visualizer Winform ToolStripDropDownMenu ComboBox only shows items when first shown</span> <span><span lang="" about="/user/83" typeof="schema:Person" property="schema:name" datatype="">蓝咒</span></span> <span>2020-05-31 03:27:11</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a Visual Studio debugger visualizer project, and when I add a <code>ToolStripComboBox</code> to a <code>ToolStripDropDownMenu</code>, the combobox's items only appear the first time the form is shown.</p> <p>Like this:</p> <p></p> <p>The most basic version of the winform code showing the issue is this:</p> <pre class="lang-cs prettyprint-override"><code>public class MyVisualizerDialog : Form { public MyVisualizerDialog() { var toolStripComboBox = new ToolStripComboBox { Items = { "One", "Two", "Three" } }; var toolStripDownDown = new ToolStripDropDownMenu { Items = { toolStripComboBox } }; var toolStrip = new ToolStrip { Items = { new ToolStripMenuItem("Options") { DropDown = toolStripDownDown } } }; Controls.Add(toolStrip); } } </code></pre> <p>Then the visualizer code is simply:</p> <pre class="lang-cs prettyprint-override"><code>public class MyVisualizer : DialogDebuggerVisualizer { protected override void Show( IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) { windowService.ShowDialog( new MyVisualizerDialog()); } } </code></pre> <p>Some extra details:</p> <ul><li><p>If I add the <code>ToolStripComboBox</code> to <code>ToolStripMenuItem.DropDownItems</code>, it works fine - it seems to specifically be an issue with having a <code>ToolStripComboBox</code> in a <code>ToolStripDropDown</code>.</p></li> <li><p>If I create and open multiple instances of the same form class in a console app, it works fine.</p></li> <li><p>Once the issue occurs, it <em>keeps</em> occurring - even when I revert the code to the version without the <code>ToolStripDropDown</code></p></li> <li><p>If I restart Visual Studio, it works the first time the form is shown, then not afterwards.</p></li> </ul><p>Any ideas?! Anyone know some wrinkle in the way the <code>IDialogVisualizerService</code> disposes controls or something?!</p> <p>Thanks for reading :)</p> <br /><h3>回答1:</h3><br /><p>It appears that, when the debugger visualizer is closed - which is handled in the debugger side, not in the debuggee side - the DropDown is destroyed but the ToolStripManager doesn't know about it and it finds itself with an invalid handle that it doesn't know how to manage. </p> <p>Since the ToolStripManager is also active in design mode, this propagates the problem throughout the designer interface: you may find that some DropDown items still work after the debugger visualizer has been closed, but you may not be able to add other ToolStripComboBox items anywhere.<br /> If you <em>insist</em>, also those that appeared to be working may not work anymore. </p> <p>Note that this misbehavior can translate to ComboBox objects; not directly, but when you try to access their Items collection through the interface.<br /> It may also prevent the Project from compiling. </p> <p>Explicitly disposing of the Form object created in the debugger visualizer side, can partially solve the problem on the debuggee side, but not, as it turns out, on the debugger visualizer side. </p> <p>A simple solution is to avoid setting the DropDown object of a ToolStripMenuItem and use a MenuStrip instead, adding Items to a ToolStripDownDown. </p> <hr /><p>Create custom data visualizers<br /> Visualizer Security Considerations </p> <hr /><p>Sample debugger visualizer (simple Image visualizer) to test the <em>good</em> and <em>bad</em> behavior. </p> <p>► Create a Class Library Project, <code>Target Framework</code> set to <code>.Net Framework</code>, <code>AnyCPU</code> profile. </p> <p>► Add a reference to <code>[Visual Studio install Path]\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.DebuggerVisualizers.dll</code> and <code>System.Windows.Forms</code>. </p> <p>► Compile the .dll as <code>Release</code>. </p> <p>► Copy the .dll to the <code>\Common7\Packages\Debugger\Visualizers</code> directory of your current Visual Studio installation path. </p> <p>► Start a debug session, add a breakpoint where an Image/Bitmap property is set/loaded and use the magnifier tool to open a preview. </p> <pre><code>using System.Diagnostics; using System.Drawing; using System.Windows.Forms; using Microsoft.VisualStudio.DebuggerVisualizers; [assembly: DebuggerVisualizer( typeof(ImageVisualizer.DebuggerSide), typeof(VisualizerObjectSource), Target = typeof(Image), Description = "Test Visualizer")] namespace TestVisualizer { public class DebuggerSide : DialogDebuggerVisualizer { override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) { var image = (Image)objectProvider.GetObject(); var form = new Form(); form.ClientSize = new Size(image.Width, image.Height); form.FormBorderStyle = FormBorderStyle.FixedSingle; form.SuspendLayout(); // ------- WORKING CODE --------------- var menuStrip = new MenuStrip() { }; var tsComboBox = new ToolStripComboBox { Items = { "One", "Two", "Three" } }; var toolStripDownDown = new ToolStripMenuItem() { Text = "Options" }; toolStripDownDown.DropDownItems.AddRange(new ToolStripItem[] { tsComboBox }); menuStrip.Items.AddRange(new ToolStripItem[] { toolStripDownDown }); // ------- WORKING CODE --------------- // ------- BAD CODE --------------- //var toolStripComboBox = new ToolStripComboBox { Items = { "One", "Two", "Three" } }; //var toolStripDownDown = new ToolStripDropDownMenu { Items = { toolStripComboBox } }; //var toolStrip = new ToolStrip { // Items = { new ToolStripMenuItem("Options") { DropDown = toolStripDownDown } } //}; // ------- BAD CODE --------------- var pBox = new PictureBox() { Image = image, Dock = DockStyle.Fill }; //form.Controls.Add(toolStrip); form.Controls.Add(menuStrip); form.Controls.Add(pBox); form.MainMenuStrip = menuStrip; form.ResumeLayout(false); form.PerformLayout(); windowService.ShowDialog(form); form?.Dispose(); } } } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/61675774/debugger-visualizer-winform-toolstripdropdownmenu-combobox-only-shows-items-when</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/combobox" hreflang="zh-hans">combobox</a></div> <div class="field--item"><a href="/tag/debuggervisualizer" hreflang="zh-hans">debuggervisualizer</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Sat, 30 May 2020 19:27:11 +0000 蓝咒 3657643 at https://www.e-learn.cn Converting ToolBar to ToolStrip control and MouseHover not working https://www.e-learn.cn/topic/3308297 <span>Converting ToolBar to ToolStrip control and MouseHover not working</span> <span><span lang="" about="/user/26" typeof="schema:Person" property="schema:name" datatype="">*爱你&永不变心*</span></span> <span>2020-01-25 10:06:25</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a large winform application which we working to modify the appearance. I am replacing System.Windows.Forms.Toolbar to System.Windows.Forms.ToolStrip control. I use a custom renderer to change dropdown arrow color. with default renderer i get mouse hover effects in toolstrip but with my custom rendering it dont seem to work. Here's my code.</p> <p><strong>Tool strip initialization:I removed unnecessary code for reading comfort</strong></p> <pre><code>this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.toolStripDropDownButton1 = new System.Windows.Forms.ToolStripDropDownButton(); this.toolStrip1.ImageList = this.imageList1; this.toolStrip1.ImageScalingSize = new System.Drawing.Size(55, 32); this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripDropDownButton1 }); this.toolStrip1.Renderer = new MyRenderer(); </code></pre> <p><strong>Toolstrip dropdown button:</strong></p> <pre><code> this.toolStripDropDownButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripDropDownButton1.ImageIndex = 0; this.toolStripDropDownButton1.Name = "toolStripDropDownButton1"; </code></pre> <p><strong>CustomRenderer</strong></p> <pre><code> public class MyRenderer : ToolStripRenderer { protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e) { e.ArrowColor = Color.White; base.OnRenderArrow(e); } } </code></pre> <br /><h3>回答1:</h3><br /><p>thanks to @LarsTech for his help. I found this working. I made this below modification in renderer and in code.</p> <p><strong>Added this line in initialization</strong></p> <pre><code>this.Toolstip1.RenderMode = ToolStripRenderMode.Professional; </code></pre> <p><strong>CustomRenderer</strong></p> <pre><code>public class MyRenderer : ToolStripProfessionalRenderer //Professional renderer { protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e) { e.ArrowColor = Color.White; base.OnRenderArrow(e); } } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/25203599/converting-toolbar-to-toolstrip-control-and-mousehover-not-working</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-40" hreflang="zh-hans">c#-4.0</a></div> <div class="field--item"><a href="/tag/toolbar" hreflang="zh-hans">toolbar</a></div> <div class="field--item"><a href="/tag/toolstrip" hreflang="zh-hans">toolstrip</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Sat, 25 Jan 2020 02:06:25 +0000 *爱你&永不变心* 3308297 at https://www.e-learn.cn How to find toolstripmenuItem with name https://www.e-learn.cn/topic/2849655 <span>How to find toolstripmenuItem with name</span> <span><span lang="" about="/user/206" typeof="schema:Person" property="schema:name" datatype="">十年热恋</span></span> <span>2019-12-24 03:31:06</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have set visible property of my menuStrip1 items to false as </p> <pre><code>foreach (ToolStripMenuItem itm in menuStrip1.Items) { itm.Visible = false; } </code></pre> <p>Now I know the Names of <code>toolStripMenuItem and dropDownItem</code> of the menustrip1. How to can I activate the required <code>toolStripMenuItem and dropDownItem</code>.</p> <p>I have </p> <pre><code>string mnItm = "SalesToolStripMenuItem"; string ddItm = "invoiceToolStripMenuItem"; </code></pre> <p>Now I want to set visible true to these two(<code>toolStripMenuItem and dropDownItem</code>) items. How can I do that? I know those names only.</p> <br /><h3>回答1:</h3><br /><p>You should try something like this:</p> <pre><code>string strControlVal ="somecontrol"; //"SalesToolStripMenuItem" or "invoiceToolStripMenuItem" in your case foreach (ToolStripMenuItem item in menuStrip1.Items) { if (strControlVal == item.Name) { item.Visible = false; } } </code></pre> <p>Initialize <code>strControlVal</code> string on your discretion where you need it.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Simply use those names to get the actual <code>item</code> via <code>MenuStrip.Items</code> indexer:</p> <pre><code>ToolStripMenuItem menuItem = menuStrip1.Items[mnItm] as ToolStripMenuItem; ToolStripDropDownMenu ddItem = menuStrip1.Items[ddItm] as ToolStripDropDownMenu; </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>You can use </p> <pre><code>menuStrip1.Items[mnItm].Visible = true; menuStrip1.Items[ddItm].Visible = true; </code></pre> <p>or if you want to set Visible to multiple toolstrip items:</p> <pre><code>string [] visibleItems = new [] {"SalesToolStripMenuItem", "invoiceToolStripMenuItem"}; foreach (ToolStripMenuItem item in menuStrip1.Items) { if (visibleItems.Contains(item.Name)) { item.Visible = false; } } </code></pre> <p>Hope it helps</p> <br /><br /><br /><h3>回答4:</h3><br /><p>You're looking for ToolStripItemCollection.Find method.</p> <pre><code>var items = menustrip.Items.Find("SalesToolStripMenuItem", true); foreach(var item in items) { item.Visible = false; } </code></pre> <p>second parameter says whether or not to search the childrens.</p> <br /><br /><br /><h3>回答5:</h3><br /><p>If i get your question you are trying to disable other than the above two mentioned toolstrip items. Since you know the name of the menu items a slight change in code can get you along</p> <pre><code> foreach (ToolStripMenuItem itm in menuStrip1.Items) { if(itm.Text !="SalesToolStripMenuItem" || itm.Text !="invoiceToolStripMenuItem") { itm.Visible = false; } } </code></pre> <br /><br /><br /><h3>回答6:</h3><br /><pre><code> private void ToolStripMenuItem_Click(object sender, EventArgs e) { string MenuItemName = sender.ToString() } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/18974249/how-to-find-toolstripmenuitem-with-name</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/menustrip" hreflang="zh-hans">menustrip</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> <div class="field--item"><a href="/tag/toolstripmenu" hreflang="zh-hans">toolstripmenu</a></div> </div> </div> Mon, 23 Dec 2019 19:31:06 +0000 十年热恋 2849655 at https://www.e-learn.cn Windows Forms - ToolStripItem Visible property is always set to false https://www.e-learn.cn/topic/2803034 <span>Windows Forms - ToolStripItem Visible property is always set to false</span> <span><span lang="" about="/user/163" typeof="schema:Person" property="schema:name" datatype="">荒凉一梦</span></span> <span>2019-12-23 07:55:36</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm working on a <code>MDI Windows Forms</code> application. My parent form has <code>ToolStrip</code> menu and some <code>ToolStripDropDownButtons</code>. I want to change the <code>Visible</code> property of the <code>ToolStripDropDownButton</code> or to some of the <code>ToolStripItems</code> (sub buttons) that it has accordingly to the permission of the user. </p> <p>Here is the part of the method that I've wrote to manage this:</p> <pre><code>private void SetToolStripDropDownVisibility(ToolStripDropDownButton mainBtn, params ToolStripItem[] item) { mainBtn.Visible = false; foreach (ToolStripItem tempItem in item) { tempItem.Visible = true; } } </code></pre> <p>I'm passing as first argument the <code>ToolStripDropDownButton</code> and all other "sub buttons" as params list. However when I get into debug mode in the part <code>foreach (ToolStripItem tempItem in item)</code> the <code>tempItem</code> Visible property is marked as false. In the designer however this property is set to true. You can see that I even try explicitly to change the value to true - <code>tempItem.Visible = true;</code> but it seems as if this line is doing nothing. The value of <code>Visible</code> remains <code>false</code> and I can't change it. </p> <p>This is just the begining of the method and I can't think of other code that can mess up with the <code>ToolStrip</code> items. I tried to change the value of <code>mainBtn.Visible</code> to true or false thinking that maybe there's any connection but it seems this is not the issues. So any idea why this is happening, why I cant change the <code>Visible</code> value and of course any way to do it. </p> <br /><h3>回答1:</h3><br /><p>The solution is easy and yet not obvious. When we have to work with <code>ToolStripItems</code> which are part of <code>ToolSTripDropDownButton</code> and solve visibility problem the way we used to solve it with ordinary buttons we have to use <code>Available</code> property. It is designed exactly for this purpose. Hope someone gonna spend less time dealing with this problem by reading this!</p> <br /><br /><br /><h3>回答2:</h3><br /><p>The following will go trough all <code>toolstripitems</code> within <code>menuStrip1</code>:</p> <pre><code> List&lt;ToolStripMenuItem&gt; allItems = new List&lt;ToolStripMenuItem&gt;(); foreach (ToolStripMenuItem toolItem in menuStrip1.Items) { allItems.Add(toolItem); //add sub items allItems.AddRange(GetItems(toolItem)); } foreach (ToolStripMenuItem item in allItems) { //make your toolstripMenuItem invisible or whatever you want to do with it. } allItems.Clear(); </code></pre> <p>Change <code>menuStrip1</code> to whatever you call your <code>toolstrip</code>.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/15683820/windows-forms-toolstripitem-visible-property-is-always-set-to-false</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Sun, 22 Dec 2019 23:55:36 +0000 荒凉一梦 2803034 at https://www.e-learn.cn How do I close a toolstripmenuitem that is set to autoclose = false? https://www.e-learn.cn/topic/2353884 <span>How do I close a toolstripmenuitem that is set to autoclose = false?</span> <span><span lang="" about="/user/154" typeof="schema:Person" property="schema:name" datatype="">主宰稳场</span></span> <span>2019-12-12 04:55:25</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a menu of items that the user can toggle. I wanted the menu to stay open so the user can check all the items they want. I set autoclose = false and now that works great. However, I also cannot close the window now lol. I tried clicking off of the menu onto the form, hitting escape, hitting the menu item, hitting the keycombo for the menu, nothing works.</p> <p>Ideally, I'd like the user to be able to just click the form or basically anything but the menu to close it or press escape. How would I accomplish that? I tried creating a gotfocus event on the form and doing item.HideDropDown in there but no dice.</p> <p>Thanks!</p> <br /><h3>回答1:</h3><br /><p>Generate the click event for the form, and then go through and for every control that doesn't have its own click event, set its click event to the one for the form.</p> <p>In the event, include the code to hide the menu: <code>toolStripDropDownButton.HideDropDown();</code></p> <p>Copy the code to any existing click events for other controls.</p> <p>This is how I handled hiding a monthcalendar when you click anywhere on the form.</p> <p>And if you want to also include pressing escape as an option, do the same thing with a KeyDown event, checking if it's the escape key before running the code.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>I had similar problem, and here is my solution. I created common MouseEnter and MouseLeave event handlers and used a timer to delay-close the menu after mouse leaves it.</p> <p>Below is a sample code for the menu of 3 items and 1 separator. In the sample 2 items work with AutoClose and one (the _modeChangingItem) does not close the menu. You can easily customize this for your needs, e.g. make none of the items AutoClose. </p> <pre><code>private Timer _menuTimer = new Timer(); private void MainFrm_Load (object sender, EventArgs e) { _menuTimer.Interval = 200; _menuTimer.Tick += _menuTimer_Tick; _rootMenuItem.MouseEnter += commonMenu_MouseEnter; _rootMenuItem.MouseLeave += commonMenu_MouseLeave; _menuItem1.MouseEnter += commonMenu_MouseEnter; _menuItem1.MouseLeave += commonMenu_MouseLeave; _menuItem2.MouseEnter += commonMenu_MouseEnter; _menuItem2.MouseLeave += commonMenu_MouseLeave; _separator.MouseEnter += commonMenu_MouseEnter; _separator.MouseLeave += commonMenu_MouseLeave; _modeChangingItem.MouseEnter += commonMenu_MouseEnter; _modeChangingItem.MouseLeave += commonMenu_MouseLeave; } private void commonMenu_MouseLeave(object sender, EventArgs e) { _menuTimer.Stop(); // Comment this line out if you want none of the items to AutoClose _rootMenuItem.DropDown.AutoClose = true; ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem != null) menuItem.Tag = null; ToolStripSeparator separator = sender as ToolStripSeparator; if (separator != null) separator.Tag = null; _menuTimer.Start(); } private void commonMenu_MouseEnter(object sender, EventArgs e) { ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem != null) menuItem.Tag = new object(); ToolStripSeparator separator = sender as ToolStripSeparator; if (separator != null) separator.Tag = new object(); } private void _menuTimer_Tick(object sender, EventArgs e) { if (_rootMenuItem.Tag == null &amp;&amp; _menuItem1.Tag == null &amp;&amp; _menuItem2.Tag == null &amp;&amp; _separator.Tag == null &amp;&amp; _modeChangingItem.Tag == null) { _rootMenuItem.DropDown.Close(); } _menuTimer.Stop(); } private void _modeChangingItem_Click(object sender, EventArgs e) { ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if (menuItem == null) return; // Move this line to Form_Load if you want none of the items AutoClose _rootMenuItem.DropDown.AutoClose = false; // Now the menu stays opened [...] } </code></pre> <p>This solution saves extra click for user - the timer closes the menu when you move mouse outside of all the items.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/17979780/how-do-i-close-a-toolstripmenuitem-that-is-set-to-autoclose-false</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> <div class="field--item"><a href="/tag/toolstripmenu" hreflang="zh-hans">toolstripmenu</a></div> </div> </div> Wed, 11 Dec 2019 20:55:25 +0000 主宰稳场 2353884 at https://www.e-learn.cn C#: ToolStripDropDown doesn't Dispose/DestroyHandle https://www.e-learn.cn/topic/2348752 <span>C#: ToolStripDropDown doesn&#039;t Dispose/DestroyHandle</span> <span><span lang="" about="/user/153" typeof="schema:Person" property="schema:name" datatype="">风流意气都作罢</span></span> <span>2019-12-12 04:29:04</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm using the <code>ToolStripDropDown</code> to show up an selectionpopup.</p> <p>The <code>ToolStripDropDown</code> contains a few custom controls with running Threads. The Threads ends with the OnHandleDestroyed event, but for some reason the <code>ToolStripDropDown</code> doesn't dispose/destroy it's handle after closing.</p> <p>Disposing the <code>ToolStripDropDown</code> on the closed-event gives me an exception because anything still accesses the ToolStripDropDown.</p> <p>How do I know if the custom Control is still in use or not to end the thread?</p> <p>Custom-Control:</p> <pre><code>public class CControl : Control { Thread StyleThread; Object lockOBJ = new Object(); bool abortthread = false; public CControl() { this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.Selectable, false); StyleThread = new Thread(new ThreadStart(this.StyleDelegate)); } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); if(!StyleThread.IsAlive) StyleThread.Start(); } protected override void OnHandleDestroyed(EventArgs e) { base.OnHandleDestroyed(e); lock (lockOBJ) { abortthread = true; } if (StyleThread.IsAlive) { StyleThread.Join(100); } } ... }</code></pre> <p>ToolStripDropDown-Control:</p> <pre><code>public class AddPopUp : UserControl { /* ------------------------------------ This Control contains some CControls ------------------------------------ */ public void Show(Control control) { TSDD tsdd = new TSDD(this); Point screenpoint = control.PointToScreen(new Point(0, 0)); tsdd.Show(control,new Point(0, -tsdd.Height )); } class TSDD : ToolStripDropDown { private Control Control { get; set; } public TSDD(Control control) { this.Control = control; this.DropShadowEnabled = false; ToolStripControlHost ch = new ToolStripControlHost(control); this.Items.Add(ch); } protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { base.SetBoundsCore(x, y, Control.Size.Width + 16, Control.Size.Height + 18, specified); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; unchecked { cp.Style |= (int)0x80000000; cp.Style |= 0x40000; } return cp; } } protected override void WndProc(ref Message m) { if (m.Msg == 0x84) { m.Result = (IntPtr)1; return; } base.WndProc(ref m); } } }</code></pre> <h2>EDIT:</h2> <p>After same more testing i wasn't able to dispose the toolstripdropdown. As a workaround i'm just destroying the handle on close of the toolstripdropdown.</p> <pre><code> protected override void OnClosed(ToolStripDropDownClosedEventArgs e) { base.OnClosed(e); this.DestroyHandle(); } </code></pre> <br /><h3>回答1:</h3><br /><p>ToolStrips have a very persistent bug (claimed to be fixed, but I still have problems) where they register an event handler to SystemEvents.UserPreferenceChanged to be able to repaint if the user changes global style settings in the OS. </p> <p>The only workaround I have found is to remove the attached toolstrips by enumerating the handlers in the SystemEvents.UserPreferenceChanged and removing them upon disposal of the control.</p> <p>ToolStrip memory leak</p> <p>http://connect.microsoft.com/VisualStudio/feedback/details/115600/toolstrip-leaks-after-calling-dispose#</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/6826958/c-toolstripdropdown-doesnt-dispose-destroyhandle</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/multithreading" hreflang="zh-hans">multithreading</a></div> <div class="field--item"><a href="/tag/custom-controls" hreflang="zh-hans">custom-controls</a></div> <div class="field--item"><a href="/tag/handle" hreflang="zh-hans">handle</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Wed, 11 Dec 2019 20:29:04 +0000 风流意气都作罢 2348752 at https://www.e-learn.cn ToolStripDropDownButton equivalent in WPF? https://www.e-learn.cn/topic/2164981 <span>ToolStripDropDownButton equivalent in WPF?</span> <span><span lang="" about="/user/12" typeof="schema:Person" property="schema:name" datatype="">倖福魔咒の</span></span> <span>2019-12-11 01:37:49</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm very newbie to the WPF technology. I've been developing in WinForms for around 6 years.</p> <p>Now I would like to advance one big step in my skills by learning WPF. I'm reading the introduction guides in MSDN, while as a personal exercise I'm trying to translate the sructure of a WinForms UI to its WPF UI equivalent.</p> <p>This is a simple mockup of the WinForms controls structure that I would like to reproduce in WPF:</p> <p></p> <p>My problem is trying to reproduce the equivalent control/behavior for the <strong>ToolStripDropDownButton</strong> control.</p> <p>In the Windows Forms Controls and Equivalent WPF Controls article, Microsoft just says that the equivalent control for a <strong>ToolStripDropDownButton</strong> is a <strong>ToolBar</strong>...with composition. I don't understand at all what it means about "with composition". I've been reading and experimenting with the <strong>Toolbar</strong>, but I didn't found the way to add a dropdown button thing inside the toolbar.</p> <p>My question: someone could guide me or show me a brief example of how can I add a drop down button thing inside a Toolbar?.</p> <br /><h3>回答1:</h3><br /><p>You can put a <code>Menu</code> in <code>Toolbar</code>. A <code>Menu</code> contains some <code>MenuItem</code>. Each <code>MenuItem</code> has a <code>Header</code> which is it's content and can be a text or other elements. Also each <code>MenuItem</code> can have some nested or <code>MenuItem</code>s. For example, to have a menu structure like this:</p> <p></p> <p>You can use such code:</p> <pre><code>&lt;ToolBar&gt; &lt;Menu Background="#00000000"&gt; &lt;MenuItem &gt; &lt;MenuItem.Header&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock&gt;Menu 1&lt;/TextBlock&gt; &lt;Path VerticalAlignment="Center" Margin="8,2,0,0" Fill="Black" Data="M 0 0 L 3 3 L 6 0 Z"/&gt; &lt;/StackPanel&gt; &lt;/MenuItem.Header&gt; &lt;MenuItem Header="Menu 1-1" &gt; &lt;MenuItem Header="Menu 1-1-1"/&gt; &lt;MenuItem Header="Menu 1-1-2"/&gt; &lt;/MenuItem&gt; &lt;MenuItem Header="Menu 1-2"/&gt; &lt;MenuItem&gt; &lt;MenuItem.Header&gt;&lt;TextBox Width="100"/&gt;&lt;/MenuItem.Header&gt; &lt;/MenuItem&gt; &lt;MenuItem&gt; &lt;MenuItem.Header&gt;&lt;DatePicker Width="100"/&gt;&lt;/MenuItem.Header&gt; &lt;/MenuItem&gt; &lt;/MenuItem&gt; &lt;MenuItem &gt; &lt;MenuItem.Header&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock&gt;Menu 2&lt;/TextBlock&gt; &lt;Path VerticalAlignment="Center" Margin="8,2,0,0" Fill="Black" Data="M 0 0 L 3 3 L 6 0 Z"/&gt; &lt;/StackPanel&gt; &lt;/MenuItem.Header&gt; &lt;MenuItem Header="Menu 2-1"/&gt; &lt;MenuItem Header="Menu 2-2"/&gt; &lt;MenuItem Header="Menu 2-3"/&gt; &lt;/MenuItem&gt; &lt;MenuItem Header="Menu 3"/&gt; &lt;/Menu&gt; &lt;/ToolBar&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/46250026/toolstripdropdownbutton-equivalent-in-wpf</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/net" hreflang="zh-hans">.net</a></div> <div class="field--item"><a href="/tag/wpf" hreflang="zh-hans">wpf</a></div> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/toolstrip" hreflang="zh-hans">toolstrip</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Tue, 10 Dec 2019 17:37:49 +0000 倖福魔咒の 2164981 at https://www.e-learn.cn How to sort items in ToolStripItemCollection? https://www.e-learn.cn/topic/1885267 <span>How to sort items in ToolStripItemCollection?</span> <span><span lang="" about="/user/216" typeof="schema:Person" property="schema:name" datatype="">假如想象</span></span> <span>2019-12-07 02:15:24</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I add strings (items) dynamically to a ToolStripItemCollection by: </p> <pre><code>Dim onClickHandler As System.EventHandler = New System.EventHandler(AddressOf Symbol_Click) Dim item As New ToolStripMenuItem(newSymbol, Nothing, onClickHandler) SomeToolStripMenuItem.DropDownItems.Add(item) </code></pre> <p>So the items are not added in one go, but one-by-one based on external triggers throughout the program session. I would like to sort the drop-down-list every time I add a new item. What are my options to achieve that? </p> <br /><h3>回答1:</h3><br /><p>Since <code>ToolStripItemCollection</code> has no "Sort"-Function, you have to listen on changes and write your own sort-method:</p> <pre class="lang-vb prettyprint-override"><code>Private Sub ResortToolStripItemCollection(coll As ToolStripItemCollection) Dim oAList As New System.Collections.ArrayList(coll) oAList.Sort(new ToolStripItemComparer()) coll.Clear() For Each oItem As ToolStripItem In oAList coll.Add(oItem) Next End Sub Private Class ToolStripItemComparer Implements System.Collections.IComparer Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare Dim oItem1 As ToolStripItem = DirectCast(x, ToolStripItem) Dim oItem2 As ToolStripItem = DirectCast(y, ToolStripItem) Return String.Compare(oItem1.Text,oItem2.Text,True) End Function End Class </code></pre> <p>You have to use your own comparer (https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort)</p> <br /><br /><br /><h3>回答2:</h3><br /><p>This post was tagged as c# so I converted it based on SpeziFish's answer. Thanks!</p> <pre><code>private void ResortToolStripItemCollection(ToolStripItemCollection coll) { System.Collections.ArrayList oAList = new System.Collections.ArrayList(coll); oAList.Sort(new ToolStripItemComparer()); coll.Clear(); foreach (ToolStripItem oItem in oAList) { coll.Add(oItem); } } public class ToolStripItemComparer : System.Collections.IComparer { public int Compare(object x, object y) { ToolStripItem oItem1 = (ToolStripItem)x; ToolStripItem oItem2 = (ToolStripItem)y; return string.Compare(oItem1.Text, oItem2.Text, true); } } </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>If we need to sort items in <code>ToolStripItemCollection</code>, we can use the following:</p> <pre><code>ItemCollection.OfType&lt;ToolStripItem&gt;().OrderBy(x =&gt; x.Text).ToArray(); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/5102205/how-to-sort-items-in-toolstripitemcollection</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/net" hreflang="zh-hans">.net</a></div> <div class="field--item"><a href="/tag/vbnet" hreflang="zh-hans">vb.net</a></div> <div class="field--item"><a href="/tag/contextmenu" hreflang="zh-hans">contextmenu</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Fri, 06 Dec 2019 18:15:24 +0000 假如想象 1885267 at https://www.e-learn.cn How to sort items in ToolStripItemCollection? https://www.e-learn.cn/topic/1674172 <span>How to sort items in ToolStripItemCollection?</span> <span><span lang="" about="/user/144" typeof="schema:Person" property="schema:name" datatype="">核能气质少年</span></span> <span>2019-12-05 06:21:48</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I add strings (items) dynamically to a ToolStripItemCollection by: </p> <pre><code>Dim onClickHandler As System.EventHandler = New System.EventHandler(AddressOf Symbol_Click) Dim item As New ToolStripMenuItem(newSymbol, Nothing, onClickHandler) SomeToolStripMenuItem.DropDownItems.Add(item) </code></pre> <p>So the items are not added in one go, but one-by-one based on external triggers throughout the program session. I would like to sort the drop-down-list every time I add a new item. What are my options to achieve that? </p> </div><div class="panel panel-info"><div class="panel-heading">SpeziFish</div><div class="panel-body"> <p>Since <code>ToolStripItemCollection</code> has no "Sort"-Function, you have to listen on changes and write your own sort-method:</p> <pre class="lang-vb prettyprint-override"><code>Private Sub ResortToolStripItemCollection(coll As ToolStripItemCollection) Dim oAList As New System.Collections.ArrayList(coll) oAList.Sort(new ToolStripItemComparer()) coll.Clear() For Each oItem As ToolStripItem In oAList coll.Add(oItem) Next End Sub Private Class ToolStripItemComparer Implements System.Collections.IComparer Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare Dim oItem1 As ToolStripItem = DirectCast(x, ToolStripItem) Dim oItem2 As ToolStripItem = DirectCast(y, ToolStripItem) Return String.Compare(oItem1.Text,oItem2.Text,True) End Function End Class </code></pre> <p>You have to use your own comparer (<a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort" rel="nofollow">https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.sort</a>)</p> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>This post was tagged as c# so I converted it based on SpeziFish's answer. Thanks!</p> <pre><code>private void ResortToolStripItemCollection(ToolStripItemCollection coll) { System.Collections.ArrayList oAList = new System.Collections.ArrayList(coll); oAList.Sort(new ToolStripItemComparer()); coll.Clear(); foreach (ToolStripItem oItem in oAList) { coll.Add(oItem); } } public class ToolStripItemComparer : System.Collections.IComparer { public int Compare(object x, object y) { ToolStripItem oItem1 = (ToolStripItem)x; ToolStripItem oItem2 = (ToolStripItem)y; return string.Compare(oItem1.Text, oItem2.Text, true); } } </code></pre> </div></div><div class="panel panel-info"><div class="panel-heading">Sandy</div><div class="panel-body"> <p>If we need to sort items in <code>ToolStripItemCollection</code>, we can use the following:</p> <pre><code>ItemCollection.OfType&lt;ToolStripItem&gt;().OrderBy(x =&gt; x.Text).ToArray(); </code></pre> </div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/5102205/how-to-sort-items-in-toolstripitemcollection</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/net" hreflang="zh-hans">.net</a></div> <div class="field--item"><a href="/tag/vbnet" hreflang="zh-hans">vb.net</a></div> <div class="field--item"><a href="/tag/contextmenu" hreflang="zh-hans">contextmenu</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> </div> </div> Wed, 04 Dec 2019 22:21:48 +0000 核能气质少年 1674172 at https://www.e-learn.cn ToolStripCombobox displays at the top left corner of the screen when DropDownStyle is Simple https://www.e-learn.cn/topic/1521231 <span>ToolStripCombobox displays at the top left corner of the screen when DropDownStyle is Simple</span> <span><span lang="" about="/user/17" typeof="schema:Person" property="schema:name" datatype="">…衆ロ難τιáo~</span></span> <span>2019-12-04 05:47:28</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a <code>ToolStripCombobox</code> that when I set its <code>DropDownStyle</code> to <code>Simple</code>. The first time which I open the menu, it displays at the top left corner of the screen. However, when I select the same item for the second time, it displays in the correct location. </p> <p>Is there a way to prevent the code from showing the list at the top left corner of the screen?</p> <p>Thank you in advance for any help.</p> <p><strong>First Time</strong></p> <p></p><p></p><p></p><img class="b-lazy" data-src="https://www.eimg.top/images/2020/03/15/1bfadc835402a23ef7ee6cbb28ebb444.png" data-original="https://www.eimg.top/images/2020/03/15/1bfadc835402a23ef7ee6cbb28ebb444.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><p></p><p></p> <p><strong>Second Time</strong></p> <p></p><p></p><p></p><img class="b-lazy" data-src="https://www.eimg.top/images/2020/03/15/4057a2d60d48b03666c06ff1136d878c.png" data-original="https://www.eimg.top/images/2020/03/15/4057a2d60d48b03666c06ff1136d878c.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><p></p><p></p> <br /><h3>回答1:</h3><br /><p>To solve the problem put this code in the <code>Load</code> event of form:</p> <pre><code>var item = toolStripComboBox1; var createControl = item.Control.Parent.GetType().GetMethod("CreateControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); createControl.Invoke(item.Control.Parent, new object[] { true }); </code></pre> <p>It's a strange bug and I don't have any idea why the <code>ToolStripComboBox</code> with <code>DisplayStyle</code> set to <code>Simple</code> suffers from this bug but by setting <code>DisplayStyle</code> to <code>DropDown</code> or <code>DropDownList</code> doesn't have this bug. </p> <p>Using above code, I forced the owner <code>ToolStripDropDownMenu</code> be created before being shown.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/40390885/toolstripcombobox-displays-at-the-top-left-corner-of-the-screen-when-dropdownsty</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/winforms" hreflang="zh-hans">winforms</a></div> <div class="field--item"><a href="/tag/toolstrip" hreflang="zh-hans">toolstrip</a></div> <div class="field--item"><a href="/tag/toolstripdropdown" hreflang="zh-hans">toolstripdropdown</a></div> <div class="field--item"><a href="/tag/toolstripcombobox" hreflang="zh-hans">toolstripcombobox</a></div> </div> </div> Tue, 03 Dec 2019 21:47:28 +0000 …衆ロ難τιáo~ 1521231 at https://www.e-learn.cn