How to change content in the same form - winforms

I need to view new content in the same form (not to open a new form) when I click some button.
For exapmle this opera installer. When I click "Options" content changes in this window. And when I click "Back" I see previous components.

There is different ways to achive this easily :
You can use the TabControl control, which holds different pages you can design separatly in the vs designer.
Then you choose the initial one, hide the tabs selection bar on top, and control the TabPage selection with your buttons with SelectedTab or SelectedIndex properties.
Or you can simply wrap each section in a GroupBox control, set them all to Visible=false except the initial one and handle their visibility with your buttons with something like :
List<GroupBox> sections = new List<GroupBox>() { /* add your sections */ };
nextButton.OnClick += (o, e) =>
{
sections.ForEach(s => s.Visible = .../* condition to select the section you want displayed and hide others */);
};

Related

How can I get back my lost Checkboxes (after moving them from the Form to the Panel)?

I placed several panels on my form. When I clicked the central panel in the designer, the main form was what was actually selected (even though the panel should be on top of it, and the main form should not be (easily) clickable in the designer).
By the way, clicking the other panels on the form don't have this problem (all of the other panels are on top of the form).
Because of the form being clicked when I expected that I was clicking the panel, when I added several controls to what I thought was the panel (after successfully adding some labels on top of the panel), all of these controls (checkboxes) ended up on the form / below the panel, so that they don't display at runtime:
When I realized that the controls were on the form, and not on the panel, I selected the form from the Property browser, selected all of the checkboxes at once, and cut the group of them.
I then selected the main panel from the Property Browser, and pasted those cut checkboxes onto the panel. It sort of worked, but all I saw was their outline (all selected together). I used the Ctrl+Up arrow to move them in a body up on the panel, a little at a time, until they were all near the top where they belong. But then they just disappeared.
From now on I will make sure to right-click the panel and select "BringToFront" before adding any controls to that area; for now, though, I don't see the checkboxes on either the form or the panel. How can I get them back (and place them on the panel), and prevent the form from ever appearing on top of the panel (except if/when I select it from the Property browser)?
The checkboxes do still exist, although their names were changed from "ckbxAll" etc. to Checkbox1, etc. They appear on the correct panel in Form1Designer.cs, but do not display. Here is how they are represented there:
// panelMain
//
this.panelMain.BackColor = System.Drawing.Color.White;
this.panelMain.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panelMain.Controls.Add(this.checkBox21);
this.panelMain.Controls.Add(this.checkBox20);
this.panelMain.Controls.Add(this.checkBox19);
this.panelMain.Controls.Add(this.checkBox18);
this.panelMain.Controls.Add(this.checkBox17);
this.panelMain.Controls.Add(this.checkBox16);
this.panelMain.Controls.Add(this.checkBox15);
this.panelMain.Controls.Add(this.checkBox14);
this.panelMain.Controls.Add(this.checkBox13);
this.panelMain.Controls.Add(this.checkBox12);
this.panelMain.Controls.Add(this.checkBox11);
this.panelMain.Controls.Add(this.checkBox10);
this.panelMain.Controls.Add(this.checkBox9);
this.panelMain.Controls.Add(this.checkBox8);
this.panelMain.Controls.Add(this.checkBox7);
this.panelMain.Controls.Add(this.checkBox6);
this.panelMain.Controls.Add(this.checkBox5);
this.panelMain.Controls.Add(this.checkBox4);
this.panelMain.Controls.Add(this.checkBox3);
this.panelMain.Controls.Add(this.checkBox2);
this.panelMain.Controls.Add(this.checkBox1);
this.panelMain.Location = new System.Drawing.Point(160, 0);
this.panelMain.Name = "panelMain";
this.panelMain.Size = new System.Drawing.Size(639, 740);
this.panelMain.TabIndex = 2;
//
UPDATE
It turns out they ARE there, but they are invisible now (except for their outlines when I click on them):

How to tab through RadioButton group in Windows Forms without selecting

I have a Windows Forms GUI that has a bunch of controls, one of which is a FlowLayoutPanel containing a group of RadioButtons.
I can tab to the first RadioButton (TabIndex and TabStop set properly), but when I hit tab again it takes me to the next control on the form, not the next radio button in the FlowLayoutPanel.
Once the first RadioButton has focus, the only way to go through the group is to use up/down arrows, but that actually selects the RadioButton. Selecting a RadioButton kicks off some other stuff, so I just want a way to tab through the list to figure out what to select.
What I want, ideally, is to have each RadioButton act like any other control on the page and allow me to tab through the whole group without selecting a RadioButton until I hit Space.
This tab order is default Windows behaviour and it is rarely good practice to change this.
It happens as the Radio Buttons in a group have TabStop juggled to work this way.
To change this, explicitly set the Tab Stop to true every time it is reset:
public Form1()
{
InitializeComponent();
foreach (var control in this.flowLayoutPanel1.Controls.OfType<RadioButton>())
{
control.GotFocus += (s,a) => SetTabStops(flowLayoutPanel1);
control.CheckedChanged+= (s,a) => SetTabStops(flowLayoutPanel1);
}
}
private static void SetTabStops(Control host)
{
foreach (Control control in host.Controls)
{
control.TabStop = true;
}
}

Implementation of menu buttons in WPF/MVVM/Prism

I have a Region in the top left of the WPF application which I want to be my global button bar where the user chooses which screen they want to view, and the appropriate content will then be displayed in the main region. THere will also be a sub navigation region in the top right with sub-menu options within that screen, eg if the user clicked "Maintenance" from the main menu, the sub menu would show options for New, Update, Delete, Edit etc.
What I want to have is a way to create a custom menu bar by simply specifying a list of Text button names, and ICommand/Parameter pairs for the action to invoke on button click. The natural way to do this would be have a MenuButtonViewModel class with dependency properties Title, Command and CommandParameter. One more would also be needed, IsSelected, so there is some visual way for the user to see which screen you are currently on, so it needs to behave like a toggle button, but where only one button in the group can be selected at a time. I can then have a UserControl where the content of the bar binds to an ObservableCollection and uses data templates to render the button bar.
I have tried a few ways of doing this so far but cannot figure out a way that gives me the visual behaviour I want. These are my requirements
1) Button to change background to a different Brush OnMouseOver
2) When button is selected, a different Brush is displayed as the background until a new button in the group is selected, like a togglebutton IsSelected behaviour
3) Only one button in the group can be selected at a time
The ways I have tried to do this so far are
1) Extending RadioButton with my own class, adding dependency properties for command and commandparameter. Setting all controls to have the same group Id. Data template to override display of radio button to make it look like a visual button with triggers for mouseover and isselected.
This works fine, except for one thing. Once you select a radio button in a group, there is no way to deselect all options in the radio button group. So if you navigate to "maintenance" and then click the sub menu for "Countries" for example, then you are displayed the country maintenance screen. If you then go to a different area of the app and select "Deal Entry" from the main menu, you are taken to the deal entry screen. If you then click "Maintenance", it displays the generic "maintenance" content and brings back the sub menu control for maintenance, where "Country" is selected in the radio button group, but this is undesirable. When you navigate back to Maintenance, it should deselect all sub menu options, display the generic maintenance landing page content and let you select a sub menu option before displaying that screens content. The first time you load Maintenance, nothing is selected, but once you have chosen an option, then there is no way to have nothing selected again when you reload the maintenance screen.
2) I then tried extending a ListBox, styling it with a horizontal stackpanel for the content, each listboxitem is a menubuttonViewModel. This allows me to only select a single option at a time and to clear the selection when you navigate away from the page. It also lets me change the background when you mouse over each listboxitem.
The bit I can't get working with the listbox is to have the background different on the IsSelected trigger. There seems to be some trigger on the default ListBoxItem template that overrides anything you specify in CSS so no matter what trigger I put on the listboxitem or menubuttonviewmodel style, the background simply does not change. I think I need to redefine the data and content template for listboxitem, but only have it apply for this listbox, so it can't be a global template change to all listboxitems - as I want to be able to use listboxes elsewhere in the app without inheriting this behaviour.
Can anyone give their thoughts on these two approaches and how to perhaps solve the issues I'm having to make one of them work in the way I want, particularly if you can advise how I can override the content/data template for the listboxitems in my style so they do not use the default triggers, and I can get the IsSelected trigger working for me?
If I understood correctly, you would want to clear the selection on the RadioButtons from the Sub-Menu each time you navigate back from another Main Menu option.
In order to solve this, you could have every Radio Button "unchecked" by manually setting the RadioButton´s IsChecked property value to false. A possible solution could be the following:
If you want to clear any checked option from the Sub-Menu everytime you navigate to a different section of the Main Menu, you could first notify to the Sub-Menu´s ViewModel by publishing an event using the EventAggregator after selecting and clicking a different MainMenu button, from the SelectionChangedEventHandler method of the Main Menu´s ViewModel.
Therefore, the Sub-Menu EventHandler of the published event would update each RadioButton´s IsChecked property value to false, which it could be accomplished by using a "Two-Way" Binding between each IsChecked property defined on the View, and each boolean "RadioButton1IsChecked" property implemented on the ViewModel.
I hope this helped you.

How to add already existing ToolStrip Buttons to a ToolStrip Dynamically

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}");
}

Display the menu for a ToolStripDropDownButton as a context menu

I have a tool strip which contains a ToolStripDropDownButton. As an alternative access method, I would also like to be able to display this button's dropdown menu as a context menu, when the user right-clicks in the area below the tool strip.
I tried the following code, but it didn't work (it displayed the button's dropdown in the normal location, directly under the button):
Point contextMenuLocation = [get from WM_CONTEXTMENU]
myButton.DropDown.Show( contextMenuLocation );
The best idea I can think of would be to copy the toolstrip items from the button's dropdown into a ContextMenuStrip, but I don't see any simple way to do that (ToolStripItem does not implement ICloneable or a Clone method). Tool strip items store a reference to their Parent, so I can't just add the existing items to the context menu, as that would break the button.
Does anybody have a good idea on how to accomplish this?
A good way of populating two different dropdown with the same items is to extract the items creation into a function that builds the necessary drop down just before you open any instance of that dropdown. This also lets you enable a disable stuff if the application state changes.
class A
{
public A()
{
button = new ToolStripDropDownButton();
button.DropDown = new ToolStripDropDown();
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDown.Opening += DropDownOpening;
menu.Items.DropDown = dropDown;
}
void DropDownOpening(object sender, EventArgs e)
{
ToolStripDropDown dropDown = sender as ToolStripDropDown;
if(dropDown != null)
{
dropDown.Items.Clear();
BuildMenu(dropDown);
}
else
{
// throw if you like
}
}
void BuildMenu(ToolStripDropDown dropDown)
{
// TODO : Add items to dropdown
// TODO : Take decisions depending on current application state
}
ToolStripDropDownButton button;
MenuStrip menu;
}

Resources