How to add already existing ToolStrip Buttons to a ToolStrip Dynamically - winforms

I have a Winform Applicaion useing an MDI Form. On the MDI Form I have a ToolStrip with buttons (Buttons have images) on it acting as the main menu buttons for the application. So when user clicks on a button on the toolstrip the mdichild form for that button opens the child form.
So I have six buttons with images already created and in the project. But I want the user to select the buttons they want to appear on the toolstrip.
So the user opens the application and there is only one button on the toolstrip. The user clicks on that button and a child screen opens showing all the available existing buttons that could be on the toolstrip. The user selects the buttons they want to appear on the toolstrip then clicks that save button on the child screen.
What I want is that as soon as the user clicks that save button the buttons that the user selected should automatically appear on the toolstrip.
Right now I have to have the user close the applicaiton then reopen it for the buttons they selected to appear on the toolstrip.
How so I get the buttons to appear automatically?

Just create all of the ToolStripButtons, and set each one's Visible property to false. When the user picks them to be shown, change the Visible property of the ToolStripButton to true. They'll automatically appear on the ToolStrip.
I tested using VS2010 with Oxygene from RemObjects (formerly AKA Delphi Prism).
Start a new WinForms application
Drop a ToolStrip on the window. Right-click it and choose Insert standard items.
Double-click the New button (newToolStripButton, the one on the left end), and add the following code to the newToolStripButton_Click handler:
// Oxygene version: helpToolStripButton.Visible := not helpToolStripButton.Visible;
helpToolStripButton.Visible != helpToolStripButton.Visible;
Run the application, and click the newTooStripButton repeatedly, and watch the right-most ToolStripButton (the Help button) appear and disappear from the ToolStrip.

You can create any ToolStrip and add it to a MenuStrip.DropDownItems.Add.
The click EventHandler must be a (s,e) function.
ToolStripMenuItem ts = new ToolStripMenuItem();
ts.Name = $"MyMenuStrip";
ts.Text = "New MenuStrip";
ts.Click += new EventHandler(this.ToolStripMenuItem_Click);
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
Trace.WriteLine($"Clicked: {clickedMenuItem.Text}");
}

Related

C# how to make a child form topmost only inside the application?

I have 5 child forms in my c# application. one of the child forms named childFormopens another form subForm. What i want is, when subForm is open, user cannot click or do anything inside the application without closing subForm. But this should be contained in the application only. i.e When user wants to switch to another application without closing subForm he/she wont see subForm anymore, again if user switches to the c# application the subForm form must be on top and rest of the control must be disabled.
to create and show the subForm from within the childForm i wrote a button_click event
Imagine your form contains an editbox, a Button and a close Button (x).
This may help if we have the same SDK.
Button.click += delegate{
Subform();
}
Public void Subform(){
Button closebutton = Resource.Id...;
Button Enter = Resource.id...;
Edittext Edit = Resource.Id...;
Enter.enabled = false;
Edit.editable = false;
Closebutton.click += delegate{
Mainform();
}
}

WPF window and element focus

I've got a WPF form inside a window and a short series of events like this:
1) 1st form has series of selection buttons
2) Clicking a button brings up a progress bar window over the existing window
3) Progress window closes and 1st form switches to a new 2nd form using page navigation
The problem is that the 2nd form (and the entire window) no longer has any focus and what's really killing me is that the window is no longer getting OnKeyDown() calls (it's still the top winow). If I hit the tab key the first menu option is highlighted and the following key stroke will fire a OnKeyDown(). Also, if I alt-tab to another app and then alt-tab back to my window it will begin receiving OnKeyDown() again.
How do I figure out where the focus is after the dialog?
If you are using navigation then the focus will have switched to the Page that you have navigated to, assuming that it is Focusable.
You can check that by setting up a breakpoint in a Focused event handle for the Page.
Alternatively you can use an explicit control.SetFocus() in the page.Navigated handler.

WinForms: can a menu click handler determine which edit control on the form had focus immediately preceding the click?

WinForms.
Let's say we have a grid on the form with some cell editors (checkboxes, radio button groups). The form has a file menu. File -> Save. User visits a few cells in the grid, and clicks a checkbox in a cell, and, without touching anything else with the mouse and without touching the Tab key, clicks on the File menu and then clicks Save.
Inside the Save menuitem click handler, is it possible to determine which checkbox in the grid had focus immediately prior? That checkbox won't have lostfocus yet because a menu choice doesn't cause its lostfocus event to fire.

Displaying a modal dialog (ChildWindow) from a user control

I have a user control that has a Grid as a main container. This control needs to display a modal dialog. However, when I show the dialog (implements ChildWindow) from the control, nothing happens, no errors and no dialog.
MyDialog dialog = new MyDialog();
dialog.Show();
If ChildWindow is something that can only be displayed from the main page and not user control, what's my alternative?
Solved the problem by creating a user control and putting it in the same grid location as the original control with Visibility:Collapsed. Every time I'd need a modal dialog, I'd disable the user control that is visible and set Visibility:Visible on the modal dialog user control.
This blog post helped a great deal: Silverlight Tutorial Part 6: Using User Controls to Implement Master/Detail Scenarios

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

Resources