WinForms context menu - not open in certain parts / detect underlying control - c#

I have a .NET 2.0 Windows Forms application. On this app there is a Form control with a Menu bar and a status bar. Also there's a ListView on this form.
If I add a context menu to this form, the context menu will open when the user right clicks any part of the form, including the menu bar and the status bar.
How can I prevent the context menu from opening when the click happened on the menu bar / status bar? I want it to open only when clicking the "gray area" of the form.
If the click happened above a control on this form (for example, on the ListView), how can I identify this? I'd like to know if the user right clicked above the gray area or above the ListView, so I can enable/disable some menu items based on this.

After you've placed your Statusbar at the bottom and MenuStrip at the top,
Set ContextMenuStrip on your form to None
Place a standard Panel in the middle (between MenuStrip and StatusStrip) with the Dock property set to Fill.
Set the ContextMenuStrip property on your Panel (instead of on the form).
And place the ListView and all other controls that should go into the form in the Panel
Eg
~~~~~~~~~~~~~
menustrip
~~~~~~~~~~~~~
Panel. Dock=Fill. ContextMenuStrip=yourContextMenu.
~~~~~~~~~~~~~
StatusStrip
~~~~~~~~~~~~~

I found the answer:
Point clientPos = this.PointToClient(Form.MousePosition);
Control control = this.GetChildAtPoint(clientPos);
This should give the underlying control that was clicked on the Form, or null if the click was on the gray area. So we just need to test for the type of the control on the Opening event of the context menu. If it's MenuStrip, ToolStrip or StatusStrip, do e.Cancel = true;.

Related

How do I make a form TopMost from a different existing form?

I have a navigation menu to select the settings for the main form. How would I go about making the whole application TopMost from a different existing form.
Picture 1 shows the settings form
How would I be able to change TopMost from the Menu Settings section
I am using panels to display the change between forms.
(Assuming this is WinForms) I think I'd do it by bind to a setting.
On the form you want to be topmost open the form in the designer, click the form background so the Props grid shows props for the form, then expand Application Settings and click the 3 dots next to Property Binding
Drop down next to TopMost and choose New
Call it IsTopMost ...
Then in some code (anywhere, in any form, anywhere.. Such as this button click handler..) change Properties.Settings.Default.IsTopMost
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.IsTopMost = !Properties.Settings.Default.IsTopMost;
}
If you run this, and then click that button on form2, form 1 will pop to top and be topmost. If you click it again, it will toggle off. If you have some settings form2, and you bind a checkbox to this value, by the same process, then when you click the checkbox on form2, form1's TopMostness will follow the check state of the checkbox on form2.
Regardless how you change that bool in Properties.Settings.Default.IsTopMost, it will influence Form1's TopMostness

Is it possible to drag something from a dialog window to it's parent in C#

Just as an example, say I have a form with a listbox. From this form I open a dialog window using dialog.ShowDialog(this), which has another listbox. Normally, the user would double-click items in the dialog's listbox to add it to the owner form's listbox, then close the dialog when they are done. I want to know if I could enable drag and drop so the user could instead drag an item from the dialog's listbox to the owner form's listbox. From what I can tell, at least on my Windows 7 computer (using .Net Framework 4.0), this is not possible.
An additional feature I'd like, but is not necessary, is if the owner form could be brought in front of the dialog window while user is dragging item over owner form. (This is to enable the user to better see the listbox on the owner form while dragging.)
I dont think this is possible, more accurately not advisable. Clarify if I am wrong. What you are trying to do is drag an item from a modal that appears super-imposed on the form ( Instead of double-clicking and bubbling the data back up to the parent). In order to achieve a drag and drop, somehow you will have to, upon clicking to drag, trigger a loss of focus on the modal, and then drop, after which you want to regain focus on the modal. To me this seems like a circuitous way of going about a minor quality of life improvement.
This can be done by having the owner form handle the drag event directly rather than having the child control on the form handle the drag event. Set AllowDrop = true and handle DragDrop and DragOver events for the form. You can use control.ClientRectangle.Contains(control.PointToClient(new Point(e.X, e.Y))) in the form's drag events to determine if the mouse is in the desired control's client region.
You cannot bring the owner window in front of the dialog (and probably shouldn't try), but the user can easily move the dialog out of their way if necessary before beginning the drag.
Note: You may want to add if (!this.CanFocus) e.Effect = DragDropEffects.None; to your form's DragOver event to disable drag events while dialog is being shown, unless the drag is from the dialog window.

How to change TabControl's width inside UserControl?

I have created a windows form with two panels with width 25:75 ratio. That is 25 ratio is the sidemenu and the other panel is Main menu/screen of app.So i used usercontrol and dragged & dropped it on the Main screen of form when user clicks the respective button on the sidemenu then the usercontrol is seen.
Also the side menu has a partial hide feature the is a three lined button on top right of sidemenu panel when user clicks that button the menu partially hides (basically decreasing width of sidemenu and increasing the with of main menu).
The problem is usercontrol has a tabcontrol in it when user clicks hide button on sidemenu the mainmenu width increases but it leaves the width of tabcontrol as it is.
I want to also change the width of tabcontrol when user clicks hide button on sidemenu, i am not getting access to tabcontrols width which is in usercontrol in panel in form...
What I have tried:
I have tried to access the width of tab control as follows:
//inside hide button in panel1 of form1
private void hidemenu_click()
{
usercontrol1.tabcontrol1.width= mainmenu.width;
}
but there isn't any suggestion showing by intellisense.... simply showing error
error:
are you missing a using reference or an assembly reference?
Try removing usercontrol1 as when I drag&dropped a tabcontrol, I could acces the Width property of it.
This worked for me...!!!
Open the user control in the designer.
Select the tab control.
In the property grid, change the "Modifiers" to public.
Or, open the user control's code file, and add a public property to get and set the width of the tab control.
public int TabControlWidth
{
get { return tabcontrol1.Width; }
set { tabcontrol1.Width = value; }
}

Taskbar right click application context menu [JumpList]

How can I make a menu on the right click in the taskbar using Windows forms?
Something like this:
You can simply add contextMenuStrip from toolbox to form1. then go to form1 propertires -> contextMenuStrip . and the select the recently added contextmenuStrip1. You can add buttons to the contextMenustrip.
You should add at least one button in order to get the menustrip on rightclick.

DevExpress TabbedView Freeze Focused Panel

I have a TabbedView with multiple Panels
on each Panel I have a button for edit/save/cancel
i need to freeze all other Panels when I am editing one of them
I have an event on Edit Button and can read all Panels in TabbedView and make
dockPanel1.Enabled = false;
the problem is when they are on same panelContainer like in the image
i would like to freeze the other Tabs
with Enabled = false the Panel is disabled but the user can click on Tab, and I want to restrict him to view other panels
any suggestion is welcome
You would have to disable each control within each of the tab, this would probably be best handled on the click event of the tab.
Just spin through the tabView.controls object and set each control enabled or disabled based on whether you want them enabled or disabled.

Categories

Resources