ContextMenuStrip Behavior Problem - c#

I am adding a label to a form dynamically, then dynamically adding a ContextMenuStrip control. Whether I use the label.ContextMenuStrip property to connect them, or add the event handler to the label manually to have it respond to the right-click and show the context menu, I get odd behavior:
1) The menu does not appear next to the mouse pointer, it is offset down and to the right. It appears that it is related to the position of the label in it's parent control (a picture control), rather than the form.
2) The menu does not disappear when I click on something other than the menu.
Any ideas what I am missing here?
Thanks,
Andy

I worked it out. First off, I was adding the control to the label control collection, changing it to the the form collection corrected the positioning problem. I never did figure out why the menu would not close.
Ultimately I restructured things by adding a static instance of the menu to the form, then just connected the label.ContextMenuStrip property to that stastic instance. All is well with that approach.
I used the tag of the label control to identify it to the click event handler.

Related

WPF: LostFocus as in Old WinForms

I am not getting it after quite a research, that how can I implement simple Lostfocus like we use to do in Winforms. In Windows Form Control, we usually have LostFocus which I use when someome press TAB to lose focus or use mouse to select other controls. But it is not the case with WPF. I first use LostFocus, but when i press tab it doesn't fire the event. however when I click using mouse to other control it does get fired properly. So, this doesn't solve my problem.
Second, I try to use LostKeyboardFocus, it does get fired when using TAB key, and on few occassion when I use mouse to select other element, but not when I select menu item from Parent window menu.
Also, I have my control on User Control, that I put in TAB control on parent window. But when I press TAb key it always select the Parent Window Menu instead of TreeView I have in UserControl.
Any suggest is great help. Thanks.
Well, my quest for finding logically reasoning is still not satisfied, but I get the simple thing done simply.
#Daniel, as I said I think the two questions are related, well the answer is indeed related. Maybe you can explain my finding that I am putting as answer here.
I simply set the TabIndex property of my Textbox and other control [TreeView] as 1 and 2, now when I press TAB or select the TreeView item using mouse, i got Lostfocus fired up. and since I have Tab Index set within control, it doesn't select Menu item of parent [it does select it as last now]. So that make Lostfocus the function I should use for my purpose.
Anyone with explanation or better solution is still requested to share it. Thanks.

Keep Label on top of TabControl .NET

I have a label that I need to stay on top of my TabControl as I switch from tab to tab. I have tried calling the Label's BrintToFront method in the SelectedIndexChanged Event of the Tab Control but this has no effect. I also simply tried right clicking my label in design view and selecting "Bring to Front" but again, this had no effect.
When I switch to my second tab it drops behind the TabControl however, when I go back to my first tab it is in front again.
I placed the label itself on the Form rather than on the TabControl.
I am working in C#. Any ideas would be greatly appreciated. Thanks.
You should make sure that your label is not located inside a specific tab. To verify this you need to look at the nesting inside the 'document outline' (ctl+alt+T)
If it is I recommend;
dragging it just outside the tab control (you can also use the document outline).
then 'bring it to the front'.
and then use the arrow keys, or location property, to move it back into position.

How to prevent menu (ContextMenuStrip) to steal focus from my TextBox control?

I am trying to replicate an intellisense like feature where you have a textbox and a menu that's shown below it. I know intellisense doesn't use ContextMenuStrip, but my version has to have categories which are sub-menu items.
So as soon as the user clicks into my TextBox, I bring up the menu below once, but then even though I can see the caret in my TextBox it doesn't receive any key inputs. I have to click inside the TextBox again but that removes the menu from the screen.
Is there a way to prevent this? Or perhaps make the menu persistent on the screen without stealing focus?
ToolStrip control with items added to it seems to work since it's always on the form.

DataGridView cannot be Focused, but one of them can

I've come across the strangest bug pertaining to DataGridViews in Windows Forms.
I have a TabControl, that is supposed to contain a docked DataGridView in each tab page. I thought it would be convenient that the grid is focused upon changing the tab page, so that the user could simply hover the mouse over the grid and start scrolling when he changes the page. So, I just put a grids[tabs.SelectedIndex].Focus() in the event handler for changing the tab page.
However, something really strange happened. In my test application, I have three tab pages. If I try scrolling the grid right after starting the application, it doesn't work; I have to click in the grid first. I was expecting this. However, if I change the tab page, I can't scroll in any of the other grids until I click, except for the first one!
So, if I switch pages to the second page, then back to the first, I can automatically scroll that grid without clicking, but if I then switch to the third, I have to click for the grid to focus.
I had a look at the CanFocus properties of the grids, and it seems that only the first grid has it set to True. They are all created programmatically, and all in the same way. I don't see why they would be different.
Any ideas?
Inactive tab pages have their Visible property set to false. The documentation for CanFocus says:
In order for a control to receive
input focus, the control must have a
handle assigned to it, and the Visible
and Enabled properties must both be
set to true for both the control and
all its parent controls
Well, I solved it. Stupid programming error on my part, I had grids[tabs.TabIndex].Focus() instead of grids[tabs.SelectedIndex].Focus().
Oh well.

Custom User Control in C#...Right Click Menu to Copy Text (Java Developer learning C#)

I'm working on a custom user control that essentially displays a name value pair (name is on a black background, value on a white). I have my control displaying correctly, even showing up in Designer and on my build page.
What I'd like to do from here is have the ability to right click on the user control and have a menu come up that has a "Copy Value" option, that when selected will copy the value in the "value" part of the user control to the clipboard. What is the best method of approach?
I'm not sure where to start since most of the documentation on user controls I've found deals with displaying the control, not necessarily interacting with it. Additionally, since I'm still learning C#, I might have left out an important part of my problem in this question, so please point that out if it's the case.
I'm using Visual Studio 2008 (if that matters).
Examine the ContextMenu control and the ContextMenu property of other controls. By assigning a ContextMenu control to the ContextMeny property of another control, you will have the right-click->popup menu wiring done for you. Then you only need to implement the click event of the different menu items in the context menu.
Then you can use the Clipboard.SetText (as suggested by BFree) to set the desired value to the clipboard.
Add a ContextMenu to the control. The, hook into the MouseClick (or MouseDown, whichever works better) event and if it's a Right-Click, then call show on the ContextMenu (there are a few overloads, try to mess with them see which works best for you). Then, in the click event of your context menu, just call Clipboard.SetText(...) to set the value to the clipboard.

Categories

Resources