Dynamically Disable Particular Context Menu Item - c#

I've added 4 menus in context menu. If during the start context menu item is clicked, how to disable that particular ("Start") menu item?
ContextMenu conMenu1 = new ContextMenu();
public Form1()
{
InitializeComponent();
conMenu1.MenuItems.Add("Start", new System.EventHandler(this.Start_Click));
conMenu1.MenuItems.Add("Pause", new System.EventHandler(this.Pause_Click));
conMenu1.MenuItems.Add("Resume", new System.EventHandler(this.Resume_Click));
conMenu1.MenuItems.Add("Stop", new System.EventHandler(this.Stop_Click));
}
private void Start_Click(object sender, EventArgs e)
{
// Functionalities to disable start context menu item
}

You can do like this. Handle the ContextMenu.Opening Event
private void conMenu1_Opening(object sender, CancelEventArgs e)
{
conMenu1.Items[0].Enabled= false;
}

Use PopUp event such as
Declaration
var trayMenu = new ContextMenu();
trayMenu.Popup += MenuOpening;
trayMenu.MenuItems.Add(...);
...
Subscribed Event
private void MenuOpening(object sender, EventArgs e)
{
var cm = sender as ContextMenu;
if (cm != null)
cm.MenuItems[0].Enabled = false;
}

Related

How do I store first control in flowLayoutPanel c#?

I am new to C# and I am using windows forms.
I have flowLayoutPanel and I programmatically add some buttons to it .
What I am trying to do is: I want to save the first button located in the flowLayoutPanel into ButtonToSave object.
flowLayoutPanel1.FlowDirection= FlowDirection.LeftToRight
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button ButtonToSave = new Button();
ButtonToSave = "First button in flowLayoutPanel1"
}
Anyone knows how to save the first button located in flowLayoutPanel1 into ButtonToSave when StoreTheFirstButton event is raised?
Thank you
Try code below. You may want to look at my response at following posting (How to (create and) add components to a Table Layout?) :
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += new EventHandler(StoreTheFirstButton_Click);
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
Button ButtonToSave = button;
//ButtonToSave = "First button in flowLayoutPanel1";
}

copy paste and cut clicks do not exist in current context

Am creating a context menu programmatically as to have right click options on my datagrid. Here is my code:
public partial class Form1 : Form
{
//string fileExcel;
public Form1()
{
InitializeComponent();
fillCari();
FillCombo();
ContextMenuStrip mnu = new ContextMenuStrip();
ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
//Assign event handlers
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
//Add to main context menu
mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste });
//Assign to datagridview
dataGridView1.ContextMenuStrip = mnu;
}
This particular block I keep getting the error that it does not exist in current context. Any idea why?
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
Updated question:I created the strips from the context menu but not sur ehow to implement the copy commands.
public partial class Form1 : Form
{
//string fileExcel;
public Form1()
{
InitializeComponent();
fillCari();
FillCombo();
ContextMenuStrip mnu = new ContextMenuStrip();
dataGridView3.ContextMenuStrip = mnu;
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I had figured it out for awhile now. Hopefully it helps anyone in the future. Keep in mind after creating your contextMenuStrip and adding a "copy" field to the strip you will then bind it to the dataGridView. This is done by going into properties of the context menu and looking for the dataGridView name and bind it to it. After that open the click event in properties on the contextMenu and add this code in it:
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView3.Select();
DataObject o = dataGridView3.GetClipboardContent();
Clipboard.SetDataObject(o);
}
You should now have your right click copy button working.
For the click event you need event handlers:
// event click
mnuCopy.Click += new EventHandler(mnuCopy_Click);
mnuCut.Click += new EventHandler(mnuCut_Click);
mnuPaste.Click += new EventHandler(mnuPaste_Click);
// event handler method
void mnuPaste_Click(object sender, EventArgs e) {
// paste logic
}
void mnuCut_Click(object sender, EventArgs e) {
// cut logic
}
void mnuCopy_Click(object sender, EventArgs e) {
// copy logic
}

DataGridView Does not refresh after editing in c#

I have a data grid view whose data source gets assigned a list of items after the following function on load:
public void refreshGrid(object sender, FormClosingEventArgs e)
{
dgvItems.SuspendLayout();
itemBindingSource.SuspendBinding();
List<Item> items = db.Items.ToList(); // db is MyContext db = new MyContext();
itemBindingSource.DataSource = items;
dgvItems.DataSource = null;
dgvItems.DataSource = itemBindingSource;
itemBindingSource.ResumeBinding();
dgvItems.ResumeLayout();
}
private void AllItemsForm_Load(object sender, EventArgs e)
{
refreshGrid();
}
and there is a edit button which does the following on click:
private void btnEditItem_Click(object sender, EventArgs e)
{
Item item = (Item)dgvItems.SelectedRows[0].DataBoundItem;
var editForm = new EditItemForm(item);
editForm.FormClosing += new FormClosingEventHandler(refreshGrid);
editForm.Show();
}
i.e. opens an edit form and assigns refreshGrid() to its closing event.
On that Edit Form I have this Save button which does this:
private void btnSave_Click(object sender, EventArgs e)
{
Item itemEdited = db.Items.Where(i => i.itemId == itemEditing.itemId).Single();
itemEdited.categoryId = (int)cbxCategory.SelectedValue;
itemEdited.description = tbxDescription.Text;
itemEdited.price = (Double)nudPrice.Value;
db.Entry(itemEdited).State = EntityState.Modified;
db.SaveChanges();
this.Close();
}
the item edit is working, but is apparent only after closing and reopening the edit form, i.e. that refreshGrid() method which was assigned to its closing event is not working!
How can I fix this?
I found my own mistake. The mistake was using two different instances of Context class.
The solution was to add:
SomsaContext database = new SomsaContext(); // i.e. new instance of Context class
right before the refresh takes place.

Closing an active mdi child

I have this code where i can close and open a child form using a menu strip. My question is how do i close a specific active child form if i have multiple child forms that is active?
private void fileMenu_Click(object sender, EventArgs e)
{
frmtview tv = new frmtview();
if (ActiveMdiChild != null)
{
ActiveMdiChild.Close();
}
else
{
tv.MdiParent = this;
tv.Dock = DockStyle.Left;
tv.Show();
}
}
private void Home_Load(object sender, EventArgs e)
{
frmtview tv = new frmtview();
tv.MdiParent = this;
tv.Dock = DockStyle.Left;
tv.Show();
}
Do you mean all opened child windows? If so, when open/create child window, add the object to the list<>(member variable). when click over the close menu, just iterate all items in the list and call close method.
List childControls = new List();
void Closeclick(.......)
{
foreach(UserControl uc in childControls)
{
uc.Close();
}
}
void ActivateClick(.......)
{
HomeForm home = new HomeForm();
childControls.Add(home);
home.Show();
}

I need to close the previous active window when navigating a windows form with menustrip

I'm putting together a simple UI that interacts with a SQL database. My problem is a UI problem, ever time a menustrip item is selected, it opens a new active window. How do I set this up to close the previous active window? I've tried using Form.Close();, but that just closes everything.
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_campus go = new if_add_campus();
go.Show();
}
private void addDepartmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_dept go = new if_add_dept();
go.Show();
}
private void addEmployeToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_employee go = new if_add_employee();
go.Show();
}
Just keep track of the last form you created in a variable:
private Form lastForm;
private void showForm(Form frm) {
frm.FormClosed += (sender, ea) => {
if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
};
frm.Show();
if (lastForm != null) lastForm.Close();
lastForm = frm;
}
And use showForm() to display your forms:
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new if_add_campus());
}
Not tested, should be close.

Categories