context sensitive menu for a grid using a menu class - c#

I am using a context menu for my gridcontrol.
theGrid.ContextMenuStrip = contextMenuStrip1;
This has add, delete, edit as the choices.
How can I create a menu class and use for the choices in the contextmenustrip.
Can anyone show me some sample code to do this.
Thanks
Sun

MSDN has information on how to add items to a contextmenustrip
Sample Code:
// Declare the ContextMenuStrip control.
private ContextMenuStrip fruitContextMenuStrip;
public Form3()
{
// Create a new ContextMenuStrip control.
fruitContextMenuStrip = new ContextMenuStrip();
// Attach an event handler for the
// ContextMenuStrip control's Opening event.
fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
// Create a new ToolStrip control.
ToolStrip ts = new ToolStrip();
// Create a ToolStripDropDownButton control and add it
// to the ToolStrip control's Items collections.
ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");
ts.Items.Add(fruitToolStripDropDownButton);
// Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top;
// Assign the ContextMenuStrip control as the
// ToolStripDropDownButton control's DropDown menu.
fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;
// Create a new MenuStrip control and add a ToolStripMenuItem.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
ms.Items.Add(fruitToolStripMenuItem);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Assign the MenuStrip control as the
// ToolStripMenuItem's DropDown menu.
fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;
// Assign the ContextMenuStrip to the form's
// ContextMenuStrip property.
this.ContextMenuStrip = fruitContextMenuStrip;
// Add the ToolStrip control to the Controls collection.
this.Controls.Add(ts);
//Add a button to the form and assign its ContextMenuStrip.
Button b = new Button();
b.Location = new System.Drawing.Point(60, 60);
this.Controls.Add(b);
b.ContextMenuStrip = fruitContextMenuStrip;
// Add the MenuStrip control last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);
}
Or another link to ContextMenuStrip in C#

Related

Child form appearing underneath controls on parent form

I have a C#, .net, Windows Forms application. I have a form set as an MDI container on which I dynamically add buttons. Clicking a button opens a child form. However, the buttons I created appear on top of the child form instead of the child form appearing over (and covering) everything on the parent form. What am I doing wrong?
Here's how I add the buttons to the form:
Button btn1 = new Button();
btn1.BackColor = ColorTranslator.FromHtml("#456EA4");
btn1.Text = department.DepartmentName;
btn1.Location = new System.Drawing.Point(posX, posY);
btn1.Size = new System.Drawing.Size(sizeX, sizeY);
btn1.Font = new System.Drawing.Font("Calibri", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btn1.ForeColor = Color.WhiteSmoke;
btn1.TabStop = false;
this.Controls.Add(btn1);
Here's is where I open the child form:
frmToBuildFromSchedule frmToBuild = new frmToBuildFromSchedule(department);
frmToBuild.FormClosed += new FormClosedEventHandler(frmToBuildFromSchedule_FormClosed);
frmToBuild.MdiParent = this;
frmToBuild.Show();
Here is the result:
You are putting the buttons directly on the parent MDI form, which is not typical of an MDI style application. Instead, put the code that is currently in the click event of your buttons in a menu option or a ribbon button (those can also be dynamically created).
Alternatively, create a child form, maximize it, and place your buttons on that.
I think the answer by sam on the posted duplicate link is interesting. Rather than using MDI, set form.TopLevel = false; and add the form as a child control. I'm not sure if there are any downsides to this approach.
Form fff = new Form();
fff.Controls.Add(new Button { Text = "Button1" });
fff.Load += delegate {
Form ffff = new Form { TopLevel = false };
fff.Controls.Add(ffff);
ffff.Visible = true;
ffff.BringToFront();
};
Application.Run(fff);

Displaying user control in parent form panel

The first line is able to clear the panel but doesnt seem to show the usercontrol in the panel in the parent form.
panel1.Parent.Controls.Clear();
if (panel1.Parent == null)
return;
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Parent.Controls.Add(uc);
Why you use panel1.Parent? In this way, you remove all controls not from the panel itself, but from the parent control on which this panel is located.
This action removes the panel itself. And, accordingly, the panel does not have a parent now. The Parent property becomes null.
I suppose you need to write like this:
panel1.Controls.Clear();
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Controls.Add(uc);

C# How to create TabPage as child form of MDI?

I would like to create TabPages (fill docked with frmCalculationReport) as child forms of a MDI, so that I can switch active TabPage from ToolStripMenu such as "Window" pull down menu.
I have one tabControl1 fill docked in MDI MainWindow with ToolStripMenu of that "Window".
When I click one ToolStripMenu item such as performCodeCalculationsToolStripMenuItem from MDI MainWindow, I would like one TabPage (with a docked frmCalculationReport) added on tabControl1 as a child form of MDI MainWindow. Following are the codes. But, I do not know how to add the TabPages as child of MDI MainWindows. Any suggestion and comments will be greatly appreciated.
private void performCodeCalculationsToolStripMenuItem_Click(object sender, EventArgs e)
{
string title = "CalReport";
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
CalculationReport frmCalculationReport = new CalculationReport();
frmCalculationReport.TopLevel = false;
frmCalculationReport.Visible = true;
frmCalculationReport.FormBorderStyle = FormBorderStyle.None;
frmCalculationReport.Dock = DockStyle.Fill;
tabControl1.TabPages[tabControl1.TabCount - 1].Controls.Add(frmCalculationReport);
tabControl1.SelectedIndex = tabControl1.TabCount - 1;
}

Is it possible that instead ContextMenuStrip of NotifyIcon, display a Form, which will behave like ContextMenuStrip?

I want to replace ContextMenuStrip of NotifyIcon with some more complex Form. I can display form when user click on NotifyIcon in SystemTray but I can't hide/close form like ContextMenuStrip close when user click somewhere else.
Is that possible?
Here is example code, I Show that form this way:
private void Mouse_Up_Event(object sender, MouseEventArgs e)
{
FormMenu f = new FormMenu();
f.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
f.SetDesktopLocation(Cursor.Position.X - f.Width / 2,
Cursor.Position.Y - f.Height - 20);
f.Show();
f.Focus();
}
And FormMenu is a complex form with panels and multiple buttons.
You can host your complex control or form in ContextMenuStrip using ToolStripControlHost.
Code:
var c= new MyUserControl();
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(c));
You can also add your form to context menu strip this way:
var f = new YourForm() {
TopLevel = false,
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None,
MinimumSize= new Size(200, 200), /*Your preferred size*/
Visible=true
};
//Set up properties and events then add it to context menu
this.contextMenuStrip1.Items.Add(new ToolStripControlHost(f) );
Screenshot:
In above screenshot I added a form to a context menu strip that has not other items and set ShowImageMargin property of context menu to false.
You can also have other Items and sub menus.

how to create a Context Menu from other ContextMenu's ToolStripMenuItems in c#?

I have a ContextMenuStrip in my c# win application that include some submenu.
How i can display a existing submenu(from above ContextMenuStrip) on a button when this clicked?
Takle munu first and add menu items into it e.g delete or resize and after that take e.g take resize menu item and add items into it like submenu1 and submenu2
menu = new ContextMenu();
mnudel = new MenuItem("Delete");
mnuresize = new MenuItem("Resize");
menu.MenuItems.AddRange(new MenuItem[] { mnudel, mnuresize });
submenu1 = new MenuItem("Sub1");
submenu2 = new MenuItem("Sub2");
mnuresize.MenuItems.AddRange(new MenuItem[] { submenu1, submenu2 })

Categories

Resources