In Visual C++ MFC there is an inbuilt mechanism for setting the menu item states. I am trying to do the same with C# and a WindowsForm object.
I found this which is not quite the same:
Grey out menustrip items when certain forms are open/active/focused
Here is my menu structure:
So, I decided to try this:
private void viewToolStripMenuView_Click(object sender, EventArgs e)
{
zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
}
It kind of works. But I am a bit picky. I can see the menu displayed with the item enabled and then I see it change to disabled.
What is the right way to set the menu item states before the menu is displayed? I know this sounds like a simple issue but I can't find the equivalent methodology to ON_UPDATE_COMMAND_UI.
I was using the wrong event handler!
private void viewToolStripMenuView_DropDownOpening(object sender, EventArgs e)
{
zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
}
Related
I can't believe that disabling specific tabpages (visible but greyed out that means not clickable) is not possible in Visual Studio C# Form app. I have found just a workaround that I couldn't bring to work.(See link below)
I wanted to ask this question again, because perhaps there is another solution/workaround in the meantime.
How to highlight/grey-out Tabcontrol Tabpage
For just making the tab nonclickable (what is also OK for me) I have used this code, but not working
private void tabControl1_Selecting(object sender,TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
For example if you want to disable tabPage2 use this code in form load event. It will not show in intellisense but you can type.
tabPage2.Enabled = false;
Then use tab control selecting event like this
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex < 0) return;
e.Cancel = !e.TabPage.Enabled;
}
I have a C# Windows Forms App that contain a menu bar.
I want to display a Help Message when I press on the "HELP" menu button.
All that I can see when I press view code is this:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I think that I need to create inside the function a MessageBox or an event that will display the desired message.
Do you have any idea how should I do this, please?
Below should work for what your asking. If you are on your form you can double click the button you want to interact with, and Visual Studio should take you to the empty method.
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This is supposed to be helpful");
}
I'm new to WPF and coming from a C++ background so maybe I'm worry about memory management too much here.
Anyways, I've got a UserControl (NewContact) that has a grid with 2 columns, upper column displays 3 radio buttons and depending on which is selected it loads the appropriate UserControl into the lower section of the grid.
private void newMilitaryContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NMC = new NewMilitaryContact();
NewContactWindowGridDisplay.Children.Insert(1, NMC);
}
private void newMilitaryContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
private void newLegalContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NLC = new NewLegalContact();
NewContactWindowGridDisplay.Children.Insert(1, NLC);
}
private void newLegalContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
private void newFirmContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NFC = new NewFirmContact();
NewContactWindowGridDisplay.Children.Insert(1, NFC);
}
private void newFirmContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
Now my question is whether I should be, and how to, unload the UserControls I create, when a radio button is unchecked. I did some searching around MSDN documentation and saw that the using the remove method from the parent object would unload the usercontrol. If that is the case is the code I'm using to in the various "unchecked" methods correct so as not to pile up a ton of NFC/NLC/NMC UserControl objects if someone were to click amongst the three radio buttons over and over and over again?
Much thanks to anyone to who can explain this to me :)
Actually you need to read more about .Net memory management and know how it works. In your case it depends on what your UserControls are doing? If they are using system resources it will good to dispose their references in UserControl unloaded events, otherwise GC will take care of them.
Read this article :
Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework
Also the way you are going is not so good, because soon you will find out you need to do more with your UserControl like setting its DataContext, Styles handling events and etc... and this will hard to do with code.
I'm using a drop down menu, and I'd like to make an item unavailable once it's been selected. Is there a method for that? As of now, the only action I have on it is a visible=true on some input form panels. I'm using C# and ASP.NET in visual studio 2010. Fair warning: if it isn't obvious from the nature of my question, this is actually my first go-round in the world of programming, so an advanced concept in an answer may just inspire more questions.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//Do Code with the Item
if(comboBox1.SelectedItem != null) //I forgot that ,better to prevent
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
Usually in a drop-down menu, you intercept the OnOpening event and either set Visible=false or Enabled=false just before the menu opens. It sounds like you want to disable after the menu item was selected? Just put Enabled = false at the end of your item selected handler
In a C# winforms app, I've assigned the same contextual menu to four PictureBox controls.
I'd like to determine which was used to activate the contextual menu.
I did the following in the Click event for a given menu item, which seems awkward:
MenuItem_Click(object sender, EventArgs e)
{
PictureBox Origin = (PictureBox)sender;
switch (Origin.Name)
{
case "pbOne":
// do something with #1
break;
case "pbTwo":
// do something with #2
break;
}
}
Working with the control name is the part that feels awkward.
Can you suggest a better way?
Edit:
Casting sender to a PictureBox does not work, as I forgot the menu item would be the sender, not the PictureBox. So I will have to further backtrack.
Simply use SourceControl property:
var pictureBox = contextMenuStrip1.SourceControl as PictureBox;
Not so sure how you made that work. The sender is the menu item, not the picture box. If this actually works then you already have the reference to the picture box you want to tinker with. It's Origin. No need for the switch statement.
Another way that works is to use the Opening event:
private PictureBox currentBox;
private void allContextMenuStrips_Opening(object sender, CancelEventArgs e) {
currentBox = (sender as ContextMenuStrip).SourceControl as PictureBox;
}
And you can now use currentBox in any of the menu item Click event handlers. It works because there can be only one menu open at the same time.