Enable button from a parent form - c#

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

Related

How to switch view on menu item click?

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

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

right-click doesn't work with toolstripmenuitem

I have some code:
_Item.MouseDown += new MouseEventHandler(delegate(Object o, MouseEventArgs a)
{
SrcRoot = BuilderParametresPath[_index].pngPath;
DstRoot = BuilderParametresPath[_index].scenesPath;
TextsXmlFileName = BuilderParametresPath[_index].textsPath;
NavigationSystemPath = BuilderParametresPath[_index].hintPath;
LevelsXmlFileName = BuilderParametresPath[_index].LevelsFilePath;
if (a.Button == MouseButtons.Right)
{
ContextMenuStrip docMenu = new ContextMenuStrip();
ToolStripMenuItem deleteLabel = new ToolStripMenuItem();
deleteLabel.Text = "Удалить";
docMenu.Items.AddRange(new ToolStripMenuItem[] { deleteLabel });
ocMenu.Show(MousePosition);
}
});
But it does not work, because pressing the right button does not work out. What can be done?
Add a contextMenuStrip control to the form.
Now enter some menu items in the menu strip.
Click the target control which may be button/textbox/form and go to properties and in the properties select ContextMenuStrip and set the required contextMenuStrip control.
Then the context menu strip for the target control will appear when you right click to the targetead control.

how to add menuItemClick event when menuItem is created programatically in asp.net?

In my asp.net web application, i have created a menu and menuItem programatically based on the Logged in user role.The Code is given below...
Menu menu = new Menu();
menu.CssClass = "menu";
menu.IncludeStyleBlock = false;
menu.EnableViewState = false;
menu.Orientation = Orientation.Horizontal;
if (roleType.equals("teacher"))
{
MenuItem categoryItemCh1 = new MenuItem("Home");
categoryItemCh1.NavigateUrl = "Teacher/TestsList.aspx";
menu.Items.Add(categoryItemCh1);
MenuItem categoryItemCh2 = new MenuItem("Account");
categoryItemCh2.NavigateUrl = "Account/underconstruction.aspx";
menu.Items.Add(categoryItemCh2);
MenuItem categoryItemCh3 = new MenuItem("Reports");
categoryItemCh3.NavigateUrl = "Account/underconstruction.aspx";
menu.Items.Add(categoryItemCh3);
MenuItem categoryItemCh4 = new MenuItem("Logout");
menu.Items.Add(categoryItemCh4);
}
So, When user clicks the logout menu item , then i have to fire menuItem click event to do the following process.
1.Clear all the session associated with the user
2.Redirect to Login page.
But i don't know how to add a menuItem click event programatically in asp.net.Please guide me to get out of this issue...
Use the MenuItemClick event. You don't add an event to each menu item but to the menu itself
menu.OnMenuItemClick += Menu_MenuItemClick;
void Menu_MenuItemClick(Object sender, MenuEventArgs e)
{
// Display the text of the menu item selected by the user.
Message.Text = "You selected " +
e.Item.Text + ".";
}
You can use the MenuEventArgs to figure out which menu item was clicked
To add event handlers to the menu, you'll need to use either the AddHandler or AddHandlers methods on the menu's EventHandlerList. To get the EventHandlerList, see the Menu.Events property.
Here is how I solved the problem...
protected void RadMenu2_ItemClick(object sender, RadMenuEventArgs e)
{
switch (RadMenu2.SelectedItem.Text)
{
case "Menu Text 1":
Your code or method;
break;
case "Menu Text 2":
Your code or method;
break;
case "etc...":
Your code or method;
break;
}

How to Leave ToolStripMenu Open After Clicking an Item

I'm creating a ToolStripMenu shown below that is supposed to allow the user to interact with the items "XML" and "Non XML" as though they are regular check boxes on a form. However, when one item is checked/unchecked the menu closes. How can I allow an item to be checked/unchecked without closing the menu? Or is there a different standard method of achieving the same behavior?
So what I want is to be able to click on "Non XML", show a check box and leave the menu open.
The idea is that the last menu item will be "Done" and when it's clicked the "G2S" sub items will remain open but the "Display" sub items ( XML, Non XML ) will close.
Any ideas?
Note: I am aware that this is likely not the best user interface design. I'd like to know however how this could be accomplished just to gain some technical knowledge about handling menus.
Interesting concept is described in this thread on Stackoverflow:
Here is the essence of the accepted answer:
ParentMenu.DropDown.AutoClose = false;
It does exactly what you are asking for - prevent menu from closing when subitem is clicked.
Here's a useful extension that requires user to click outside of menu item + dropdowns to close.
public static void KeepOpenOnDropdownCheck (this ToolStripMenuItem ctl)
{
foreach (var item in ctl.DropDownItems.OfType<ToolStripMenuItem>())
{
item.MouseEnter += (o, e) => ctl.DropDown.AutoClose = false;
item.MouseLeave += (o, e) => ctl.DropDown.AutoClose = true;
}
}
Posted in case somebody finds it helpful.
Instead of trying to do exactly what I had originally intended, I've come up with the following:
1- Use a ContextMenuStrip
2- When the user clicks on the ToolStripMenu item I display the ContextMenuStrip at a location near the menu item as shown below: ( note the positioning still needs adjusting )
To get this working I build the ContextMenuStrip in code at run-time so that the items in the ContextMenuStrip can be build dynamically based on the situation.
Code snippets:
Show the ContextMenuStrip when the menu item is clicked:
private void filterToolStripMenuItem_Click(object sender, EventArgs e)
{
contextMenuStrip1.Show(this, 180, 20);
}
Build the ContextMenuStrip:
if (protInfo.Name == "QCOM" )
{
BroadCast = new CheckBox();
BroadCast.Text = "Date/Time Broadcast";
BroadCast.Checked = FlagSet(CurrentFilter, (Byte)Filter.DateTimeBC);
ToolStripControlHost Ch1 = new ToolStripControlHost(BroadCast);
GenPoll = new CheckBox();
GenPoll.Text = "Status Poll";
GenPoll.Checked = FlagSet(CurrentFilter, (Byte)Filter.GenStatusPoll);
ToolStripControlHost Ch2 = new ToolStripControlHost(GenPoll);
GenPollResp = new CheckBox();
GenPollResp.Text = "Status Poll Response";
GenPollResp.Checked = FlagSet(CurrentFilter, (Byte)Filter.GenStatusResponse);
ToolStripControlHost Ch3 = new ToolStripControlHost(GenPollResp);
Button btnDone = new Button();
btnDone.Text = "Done";
ToolStripControlHost Ch4 = new ToolStripControlHost(btnDone);
btnDone.Click += new EventHandler(btnDone_Click);
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add(Ch1);
contextMenuStrip1.Items.Add(Ch2);
contextMenuStrip1.Items.Add(Ch3);
contextMenuStrip1.Items.Add(Ch4);
contextMenuStrip1.Enabled = true;
filterToolStripMenuItem.Enabled = true;
}
else
{
filterToolStripMenuItem.Enabled = false;
}
This may not be the best user interface design, but it seems to work.
The original solution will work with the use of mouse events.
On mouse enter event:
parent.dropdown.autoclose = false;
on mouse leave event:
parent.dropdown.autoclose = true;
The only catch is if the user access the menu items by other means than a mouse.
I used a combination of Neolisk's and Chimera's answers to allow deletion of multiple leaf items from a treeview. My solution is below
Note: the following Items created at design time are used:
TreePromotions (TreeView)
menuVendorSection (Context Menu Strip)
removeMultipleItemsToolStripMenuItem (DropDown of menuVendorSection)
private void removeMultipleItemsToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
removeMultipleItemsToolStripMenuItem.DropDownItems.Clear();
ToolStripMenuItem detailMenuItem;
TreeNode vendorSectionNode = treePromotions.SelectedNode;
for (int vsn = 0; vsn < vendorSectionNode.Nodes.Count; vsn++)
{
//add checkbox item
detailMenuItem = new ToolStripMenuItem(vendorSectionNode.Nodes[vsn].Text);
detailMenuItem.Tag = vendorSectionNode.Nodes[vsn].Tag;
detailMenuItem.CheckOnClick = true;
removeMultipleItemsToolStripMenuItem.DropDownItems.Add(detailMenuItem);
}
//add action buttons
Button buttonDeleteMultiple = new Button();
buttonDeleteMultiple.Text = "Remove Checked Items";
ToolStripControlHost buttonHost = new ToolStripControlHost(buttonDeleteMultiple);
buttonDeleteMultiple.Click += new EventHandler(buttonDeleteMultiple_Click);
removeMultipleItemsToolStripMenuItem.DropDownItems.Add(buttonHost);
Button buttonCancelMultipleDelete = new Button();
buttonCancelMultipleDelete.Text = "CANCEL";
buttonHost = new ToolStripControlHost(buttonCancelMultipleDelete);
buttonCancelMultipleDelete.Click += new EventHandler(buttonCancelMultipleDelete_Click);
removeMultipleItemsToolStripMenuItem.DropDownItems.Add(buttonHost);
removeMultipleItemsToolStripMenuItem.DropDown.AutoClose = false;
menuVendorSection.AutoClose = false;
}
private void buttonDeleteMultiple_Click(object sender, EventArgs e)
{
//delete items
for (int dmi = 0; dmi < removeAllItemsToolStripMenuItem.DropDownItems.Count - 2; dmi++) //do not include buttons
{
((Detail)removeAllItemsToolStripMenuItem.DropDownItems[dmi].Tag).Delete(); //deletes item from database
}
//rebuild leaf
treePromotions.SelectedNode.Nodes.Clear();
addItemNodes(treePromotions.SelectedNode); //builds leaf nodes from database
//close menus
removeMultipleItemsToolStripMenuItem.DropDown.Close();
menuVendorSection.AutoClose = true;
menuVendorSection.Close();
}
private void buttonCancelMultipleDelete_Click(object sender, EventArgs e)
{
//just close menus
removeMultipleItemsToolStripMenuItem.DropDown.Close();
menuVendorSection.AutoClose = true;
menuVendorSection.Close();
}
If someone is still interested, here is a vb solution:
1) For the parent tool strip menu item, add the following handler in the form's constructor:
AddHandler ParentTSMI.DropDown.Closing, AddressOf onDropDownClosing
2) The handler:
Private Sub onDropDownClosing(sender As Object, e As ToolStripDropDownClosingEventArgs)
If e.CloseReason = ToolStripDropDownCloseReason.ItemClicked Then
e.Cancel = True
End If
End Sub
That's it all.
Don't forget to remove the handler (RemoveHandler) when you close the form.

Categories