How to determine which control activated a contextual menu? - c#

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.

Related

How do I highlight the selected item (tab index location) in my windows form application?

I want to be able to highlight the selected item on my page because it's busy and there is a lot going on. The user will not have access to their mouse, only keyboard so currently they tab through buttons quickly and enter in to what they need to do (it's a fast data entry sort of app if you must know).
I want to be able to highlight the selected button (so when you tab through currently it will select a button but it isn't very noticeable, it just has a slight border around it when selected).
I know that you can use a focusEnter and focusLeave event, but I would like to avoid that if at all possible just because there are so many buttons on the page that I would have to have a ton of repetitive events with almost the same code.
You can and should use just two common event handlers for Enter and Leave events for all your buttons!
Use the sender param to access the buttons:
private void buttons_Leaveobject sender, EventArgs e)
{
((Button)sender).BackColor = SystemColors.Control;
((Button)sender).ForeColor = SystemColors.ControlText;
}
private void buttons_Enter((object sender, EventArgs e)
{
((Button)sender).ForeColor = SystemColors.Control;
((Button)sender).BackColor = SystemColors.ControlText;
}
Use your own ideas about how to highlight the focussed button; this is a bit excessive imo..:
Of course Button with FlatAppearance can do the highlighting all by themselves as they have separate Colors for their states.
I would suggest creating your own class derived from Button, and then handling the background painting yourself. That would allow you to play with the background look/color and/or the border effects.

Adding MouseClick event on System.Windows.Input.Mouse

(I am newbie and this is probably a duplicate question.)
To see which control is clicked on form, I have a method like this;
public void IdentifyControl(object sender, System.Windows.Input.MouseEventArgs e)
{
Control ctrl = sender as Control;
if (ctrl != null)
SelectedControl = ctrl;
this.Cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
MessageBox.Show(""+ctrl.GetChildAtPoint(System.Windows.Forms.Cursor.Position).ToString());
}
and trying to call it from mouse click.
In my first attempt, I added this function to main forms MouseClick event but it only worked for form itself but controls in form. Then I tried to create a general click event and use by Mouse class.
The main point I stuck is that I couldn't create the suitable parameters Mouse.AddMouseUpHandler(DependencyObject element,
MouseButtonEventHandler handler)
Its probably because of I don't know event handling but maybe I am all in a wrong way.
You can create a mouse click event just double clicking on any buttons, labels, etc, and the Visual studio will create a code for you, like this:
private void YOUROBJECT_Click(object sender, EventArgs e)
{
}
Just put the method you want into this click event.
Hope this can help you!

How to hide control when click outside it?

I have a WindowForm and some controls on it.
My point is that when I click button "?" on top-right of the datagridview, it will show a picture box and when I click outside the pictureBox, it must invisible.
My MainForm
MyPictureBox
I have searched some topics on this site, but some dont work, some work partly. Like
this.
I also tried:
void pictureBox1_LostFocus(object sender, EventArgs e)
{
if (pictureBox1.Visible)
pictureBox1.Visible = false;
}
But when I click on button2, button3, ... The pictureBox wasn't invisible.
Any solution will be highly appreciated.
I think your pictureBox1 isn't losing focus, cause it never actually GOT focused. Set it to be focused after making it visible.
Oh, I have encountered this before...
I was making a Label that you could double click and it would allow you to edit the Label.Text, like a TextBox. However, I was having problems hooking into the events to know when the user had clicked off the Control and wished to stop editing. I tried Control.LostFocus, and Control.Leave, but nothing. I even got frustrated/desperate and tried some silly ones like Control.Invalidated.
What I ended up having to do was subscribe to the Click event of the Form/Container/Control behind it.
However, putting the responsibility of wiring up this event into the Form that wants to use it is poor design. What you can do, however is to make the constructor to Control class require a reference to the owner/parent/container as a parameter. That way, the requirements are not hidden, they must be satisfied before you can get a object instance, and the control can wired up to the Form.Click within itself, where that logic belongs.
private Form owner;
public EditLabel(Form Owner)
{
this.owner = Owner;
owner.Click += EndEditing;
}
Add this method in designer.cs:
pictureBoxEvent this.MouseLeave += new EventHandler(pictureBox_MouseLeave);
Add this code in cs file:
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

if more than one control using the same strip menu , how to determine which called it

im having 24 controls(pictureboxes) using the same strip menu, on the click event of any item in that strip menu i would want to know which control that used this strip menu item
for instance
private void getInfoToolStripMenuItem_Click(object sender, EventArgs e)
{
...
}
how to determine which control from the 24 controls that's using this menu
if it's not possible, is there any way around that to achieve the same purpose ? (to have all the controls sharing the same code without having 24 menu for the 24 controls with writing the code in all of them)
thanks and have a wonderful day
edit: for sake of clarification here is a rephrase of the problem
"a groupbox with 24 pictureboxes inside, all sharing same strip menu, i would want to determine which picturebox clicked the strip menu item so i can use that in the code to do something with the name of control (matching it with a keyvaluepair list)"
here is a picture
all i want to know which picturebox of those had clicked getinfo, that's it
After some replies, it's clear that you want to get the control clicking on which the ContextMenuStrip is popped up. Simply you can use the SourceControl property to get that control.
private void getInfoToolStripMenuItem_Click(object sender, EventArgs e) {
PictureBox pb = contextMenuStrip1.SourceControl as PictureBox;
}

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

Categories

Resources