I have a TabControl to which the user can add tab pages.
I am trying to attach some events to it such as: MouseEnter, MouseLeave, MouseClick, But it seems the these events are not firing at all, they only fire when I attach them to the TabControl itself, but this is not what I need.
What is the problem with attaching events to a tab control tab page ?
Here is my latest attempt to attach these event from my code:
private void customerTabCtrl_ControlAdded(object sender, ControlEventArgs e)
{
TCTabPage tctab = (TCTabPage)e.Control; // Option A
TCTabPage tctab = (TCTabPage)customerTabCtrl.Controls[customerTabCtrl.Controls.Count - 1]; //Option B
tctab.MouseEnter += new EventHandler(tctab_MouseEnter);
tctab.MouseLeave += new EventHandler(tctab_MouseLeave);
}
I fill so silly...
I found out the "problem", I thought that the MouseEnter, MouseLeave, MouseClick events should fire even when the cursor is on the tab header, but it appears that these events fire only when the cursor is at the tab body...
Sory for the trouble, I am using winforms only 6 months now...
You don't need an event for this since by default, the end user cannot add TabPages to the TabControl without you providing the code for it.
So wherever you are adding the TabPage, that's when you should wiring up those events:
TCTabPage tctab = new TCTabPage();
tbtab.Text = "New Tab";
tctab.MouseEnter += tctab_MouseEnter;
tctab.MouseLeave += tctab_MouseLeave;
customerTabCtrl.TabPages.Add(tctab);
Related
I have a UserControl (let's call it "PresentationCell") which contains a label, and an PictureBox.
In another control, which is using this PresentationCell, I have added an event
presentationCell.GotFocus += OnFocus;
private void OnFocus(object sender, EventArgs e)
{
if (sender is PresentationCell current)
current.BackColor = Color.Azure;
}
This will not be fired, if I click / focus on the Label or PictureBox that is within the PresentationCell.
How can I make it fire, when just something within the PresentationCell is in focus?
The problem here is, that the Label and PictureBox controls aren't selectable controls, so they aren't able to receive focus from mouse clicks.
What you could to instead, is to handle the mouse click event and check if you have hit the PresentationCell. If the PresentationCell is hit you can programatically set the focus like so:
hitPresentationCell.Focus();
This will then fire the GotFocus event.
In your OnFocus method you will have to switch the focus to another control or the event will fire endlessly.
I have 2 images for 2 players on my windows form. I have added a MouseClick event for both of them. Now when I mouse click, it activates both the events for both the players. I wanted to know only one player where I am clicking.
How do I do that?
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player1_MouseClick);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player2_MouseClick);
Assume you have two PictureBox controls with images. Thus you have posted code from designer, I also assume that you are subscribing to events via designer.
Select one of PictureBox controls
In Events tab find Click property, type-in Player_Click as handler name and hit Enter. You have subscribed to Click event of first control.
Select second PictureBox
In Events tab find Click property, click on drop-down and select Player_Click as well. Hit Enter. You have used same event handler to subscribe to Click event of second control.
Now go to code view (you can double click any of those controls for that) and find Player_Click method
In order to find which control was clicked you need to cast sender argument to PictureBox type:
private void Player_Click(object sender, EventArgs e)
{
var pictureBox = (PictureBox)sender;
// use control which was clicked. e.g. get it's name
var name = pictureBox.Name;
}
I have Windows Form named - Form1 and inside Form1 I have a panel named panel1. I use this panel only to add buttons in him. For now there are exactly 9 buttons but I intend to change their number dynamicly if this has something to do with my current problem. What I need is way to detect a when a button from this panel is clicked (I have other buttons too but, they are in Form1 outside the panel) and also to know exactly which button was clicked.
I tried this:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("HI" + sender);
}
As you can see, it's not much, but was enough to see that I can't do that using pnael1's_click event. Using this code I get the message box when I click anywhere in the panel except the buttons. So how can I do that. Is it possible to do it from inside panel1 or I should group those buttons using another approach but it's important to be able to keep the difference between those buttons which are now in panel1 and the other buttons I may (and in in fact I do have)?
When creating the dynamic buttons, you register that button instance's Click event and attach to an event handler (a single handler can handle all buttons' click event):
var dynamicButton1 = new Button();
dynamicButton1.Click += MyButtonClickHandler;
As long as MyButtonClickHandler has a signature that's suitable for a Click event (that's any method returning void and taking an object and an EventArgs, the handler should respond to a dynamic button's click event for as long as the button instance exists.
As long as you aren't adding controls dynamically over time, and the number of buttons is fixed as soon as the form is initialized, you can use this to add a click event handler to all buttons within a panel:
foreach (var button in panel.Controls.OfType<Button>())
{
button.Click += HandleClick;
}
I'm using tab control and I want to handle tabchanged event.
I was trying to use SelectionChanged event with no luck. It's being fired too many times (after loading tabcontrol, or adding new tab).
I would like to handle this event only when user navigates between tabs.
I have found solution for WPF (Is there Selected Tab Changed Event in the standard WPF Tab Control) but it's no good for Silverlight.
TIA.
Firing "too many times" should not be a problem if you check for an actual change to the SelectedIndex property in the event.
private int LastSelectedTab = -1;
void tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tab = sender as TabControl;
if (this.LastSelectedTab != tab.SelectedIndex)
{
this.LastSelectedTab = tab.SelectedIndex;
// Now do your thing...
}
}
I have a requirement to hide the contextmenustrip when a particular flag is not set. As i don't think we can explicitly control the show/hide of the context menu strip, i decided to trap the right mouse button click on the control with which the contextmenustrip is associated. It is a UserControl, so i tried handling it's MouseClick event inside which i check if the flag is set and if the button is a right button. However to my amazement, the event doesn't get fired upon Mouse Right Click, but fires only for Left Click.
Is there any thing wrong with me or is there any workaround?
RIGHT CLICK IS GETTING DETECTED, Question TITLE and Description Changed
After Doing some more research, i got the rightclick to fire, when i Handled the Mouse_Down Event on the Control. However Am still clueless, as how to explicitly prevent the ContextMenuStrip From Loading. Another question being, why MouseClick didn't detect Right Button Click?
Current WorkAround
Registering the event handler
userControl1.Control1.MouseDown += new MouseEventHandler(Control1_MouseDown);
void Control1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && flag == false)
{
userControl1.Control1.ContextMenuStrip = null;
}
else
{
userControl1.Control1.ContextMenuStrip = contextMenuStrip1;
}
}
this is the current workaround i'm doing.
But how can i change it in the Opening Event of the ContextMenuStrip
Your solution will fail anyway when the context menu is invoked with the context menu key (or what it's called) on the keyboard. You can use the Opening event to cancel the opening of a context menu.
There is a work around.
Lets say Menu Item A sets the flag that controls the context menu on control B.
In the click event for A, you set b.ContextMenu = nothing to switch it off, and set b.ContextMenu back to the context menu control to switch it back on.
In WinForms there's also the Click event - which does get fired for a right mouse click.
If you are using WPF you should have MouseRightButtonDown and MouseRightButtonUp events.
Check out the table on this MSDN page which lists which controls raise which click events. Significantly, Button, CheckBox, RichTextBox and RadioButton (and a few others) don't raise and event on right click.