How to switch view on menu item click? - c#

Hello I am using Windows forms application on visual studio to create a custom application. I want to implement such that when i click a menu item i switch the view to respective view. In below case clicking on Autopilot switches to the autopilot settings etc, or clicking on other menu items switches to respective views.
My Result.

-Add a panel where you wants to display your interfaces
-Add a new UserControl in your project (ex : ucNavigation)
-Initialise the UserControl in your main form
-Add the UserControl in your pannel form like an other control
Add a panel in your main form, from the toolbox if you want.
Right clic in your project --> Add new element --> UserControl (named ucNavigation) and the same for others UC)
public partial class frmMCAV : Form
{
private ucNavigation ucNavigation = new ucNavigation();
private ucDebriefing ucDebriefing = new ucDebriefing();
...
}
private void menuNavigation_Click(object sender, EventArgs e)
{
this.panelUC.Controls.Clear();
this.panelUC.Controls.Add(ucAccueil);
}
private void menuDebriefing_Click(object sender, EventArgs e)
{
this.panelUC.Controls.Clear();
this.panelUC.Controls.Add(ucAccueil);
}

Related

Q: DevExpress TreeListLookUpEdit

i want to add the button like plus beside the dropdown button like the picture
I don't have a problem writing the problematic code. I want to add a button in the outline design of the TreeListLookUpEdi. When I click on this button, which resembles the plus icon in the image, I add code.
Like opening a new window and adding new items to TreeListLookUpEdit
In the Runtime you can doing the following:
//add Plus Button
treeListLookUpEdit1.Properties.Buttons.Add(new EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Plus));
//add ButtonPressed Event
treeListLookUpEdit1.Properties.ButtonPressed += new ButtonPressedEventHandler(this.treeListLookUpEdit1_Properties_ButtonPressed);
private void treeListLookUpEdit1_Properties_ButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (e.Button.Kind == ButtonPredefines.Plus)
{
//here your code
MessageBox.Show("Hello!");
}
}

Trying to make changes on two different child forms from main form using Custom Events

I have an application with three forms.
I want to be able to update the listview control when a new object is being created in a separate form when all three of them are displayed.
MainForm - Contains a List collection instantiated inside, two buttons where CarForm and ListViewCarForm are instantiated.
public static List<Cars> listOfCars = new List<Cars>();
public List<Cars> CarList
{
get { return listOfCars; }
set { listOfCars = value; }
}
private void displayListViewToolStripMenuItem_Click(object sender, EventArgs e)
{
ListViewCarForm lvcf = new ListViewCarForm();
lvcf.Show();
}
private void newCarFormButton_Click(object sender, EventArgs e)
{
CarForm cf = new CarForm();
cf.Show();
//Adds new item created to listOfItems
cf.ObjectCreatedToList += ObjectCreatedToListCollectionHandler;
}
CarForm - Contains controls for the user to enter values, those values are stored inside a class member variable that is created as an object and is then added inside the List Collection in the MainForm
public EventHandler ObjectCreatedToList;
//Class Property that assigns values to member variables
public Cars Info
{
get
{
//Instantiates new Cars class, assigns member variables to control values and returns new Cars object
}
set
{
//set control values to Cars member variables
}
}
private void addCarToolStripButton_Click(object sender, EventArgs e)
{
if(ObjectCreatedToList != null)
{
ObjectCreatedToList(this, new EventArgs());
}
//Some Validation here to prevent control values to reset
//If all control values are entered, this will clear the controls
Info = new Cars();
}
ListViewForm - Contains a ListView where when items are being added into the List Collection in CarForm, it should also be added to the listview control
The problem I am having is when all three forms are opened, the listview control inside the ListViewForm should be updated as new objects are being created by the CarForm and added inside the List Collection inside the MainForm.
Since the two forms are being instantiated inside a different button in the MainForm, I can't seem to figure out how to add the items inside the listview control without it not hitting the method or giving me an error.
*This is my first time working with Windows Application Forms
To keep this simple, one solution is to pass the variable you need to the child form. Example: assuming the main form contains a ListBox of items and a button to open a Controlsform, then, when the user clicks the “Open Control Form” button… the ControlsForm is open, it has a text box and a button. When the user types something into the text box and then clicks the “Add to Parent List” button, the text in the text box should be added to the parent form’s ListBox items. You can pass the ListBox or List<Car> or anything you need to the child form. Hope this makes sense.
The MainForm has a ListBox and a button. The ListBox on the form is named Form1ListBoxControl. When the user clicks the “Open Control Form” button, the variable Form1ListBoxControl is passed to the newly created ControlsForm then the ControlsForm is shown…As above
Main form “Open Control Form” button click event…
private void btnOpenControlsForm_Click(object sender, EventArgs e) {
ControlsForm controlsForm = new ControlsForm(Form1ListBoxControl);
controlsForm.Show();
}
ControlsForm...
To use the passed Form1ListBoxControl variable in the ControlsForm we need to make a global ListBox variable so the other methods in the form can use the parents ListBox…
ListBox parentListBox;
In addition, we need to create a new "Constructor” for the ControlsForm to accept a ListBox parameter and use this parameter as the global variable defined above.…
public ControlsForm(ListBox passedParentListBox) {
InitializeComponent();
parentListBox = passedParentListBox;
}
Finally the button event in the ControlsForm to add new items to the parents ListBox…
private void btnAddToParentList_Click(object sender, EventArgs e) {
parentListBox.Items.Add(textBox1.Text);
}

How to reference/link to a listbox that is dynamically created using a button and tabControl?

I am working on making a music player in c#. I am making music playlists right now and am stuck. As of right now I am using tabControl and a button that adds a tab with an empty listbox in it. Here is the code for that button:
private void button10_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage("Playlist");
tabControl1.TabPages.Add(tp);
ListBox lb = new ListBox();
lb.Dock = DockStyle.Fill;
tp.Controls.Add(lb);
}
The problem I am running into is that I do not know how to allow the user to add music to these dynamically created listboxes within the tabs. The main list of music is located in a listbox in the first tab and I want the user to be able to select this music and put it in the new listboxes or "playlists" so I need to reference them somehow.
I'll just assume that you have a button (addToPlayListButton), a textBox (playListName) to add the selected song to the entered playList (tab-) name and that your songs listBox is called songList. I'll furthermore assume that every new playlist has a new tab. In that case you'll have to identify them so I'd change the name of the tabs:
TabPage tp = new TabPage($"Playlist {tabControl1.TabPages.Count}");
So you'll have to handle the button click event from addToPlayListButton like that:
private void onAddToPlayListButton_Click (object sender, EventArgs e) =>
(tabControl1.TabPages.Cast<TabPage>()
.FirstOrDefault(page => page.Text == playListName.Text)
?.Controls.Cast<Control>()
.FirstOrDefault(control => control is ListBox) as ListBox)?.Items.Add(songList.SelectedItem);

How do I use the DevExpress DropDownButton c#?

I am trying to create a dropdown so that users can see the names of accounts. How do i do that using DevExpress DropDown Button?
You should associate the drop-down button with a popup control/context menu. To accomplish this task use the DropDownControl property.
DXPopupMenu menu = new DXPopupMenu();
menu.Items.Add(new DXMenuItem("Admin"));
menu.Items.Add(new DXMenuItem("Guest"));
// ... add more items
dropDownButton1.DropDownControl = menu;
// subscribe item.Click event
foreach(DXMenuItem item in menu.Items)
item.Click += item_Click;
// setup initial selection
dropDownButton1.Text = menu.Items[0].Caption;
//...
void item_Click(object sender, EventArgs e) {
// synchronize selection
dropDownButton1.Text = ((DXMenuItem)sender).Caption;
// ... do something specific
}
The following objects can be used as popup controls:
PopupMenu - represents a popup menu managed by a BarManager or RibbonControl component.
PopupControlContainer - represents a container for other controls. This control is also managed by a BarManager component.
DXPopupMenu - represents a popup menu.

Enable button from a parent form

I have a C# Win Form.
Main Menu Form have
Button A
Button B
Button C
Button Setup
Each Button will open a new win form.
After clicking Setup Button, It will open a Setup form.
This form will use a datagridview to list out all the button in Main Menu except Setup button.
Admin can click the check box to select which button to enable in the Main Menu
Anyone know how to achieve this implementation
part of my code in Setup form
foreach (Button button in ????.Controls.OfType<Button>())
{
if (!button.Text.Contains("Setup"))
{
int index = dgvCheckbox.Rows.Add();
dgvCheckbox.Rows[index].Cells["Selected"].Value = 0;
dgvCheckbox.Rows[index].Cells["Button"].Value = button.Text;
}
}
if I use
MainMenu mainMenu = new MainMenu
foreach (Button button in mainMenu.Controls.OfType<Button>())
will have problem because Main Menu is already open
I create CheckBox (instead of menu) before show form so you can fill menu in the same way (this code is on main form):
private void buttonSetup_Click(object sender, EventArgs e) {
using (var adminForm = new AdminForm()) {
foreach (var button in Controls.OfType<Button>().Where(bt => !bt.Text.Contains("Setup"))) {
adminForm.Controls.Add(new CheckBox {
Text = button.Text,
Location = button.Location
});
}
adminForm.ShowDialog();
}
}

Categories