I have a windows form application , In which i created two tab pages. In one of the tab page i have a button to send email notification.
In the tabpage leave event i have some code to perform some other actions.
When i click on this button to send email. First it fires tabpage leave event , as ithe button contains button1.enabled=false; in the first line as below,
private void btnTestEmail_Click(object sender, EventArgs e)
{
btnTestEmail.Enabled = false;
bool sent = Support.SendEmail("Test Email", "This is a test email, Please ignore.", null);
--
}
But when i remove btnTestEmail.Enabled = false; code it is not firing tabpage leave event.
What could be the reason that it fires the leave event of tab page. As it is vbery strange behaviour. As i dont want to fire any event of tab page .
Regards
Changing btnTestEmail.Enabled to false will change the ActiveControl, which fires the Leave event.
According to MSDN:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and
so on), by calling the Select or SelectNextControl methods, or by
setting the ContainerControl.ActiveControl property to the current
form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
What you can do:
What I would do to eliminate this behavior is unsubscribing the Leave event and re-subscribing it after setting the Enabled property to false.
Like this:
this.tabPage1.Leave -= new System.EventHandler(this.tabPage1_Leave);
btnTestEmail.Enabled = false;
this.tabPage1.Leave += new System.EventHandler(this.tabPage1_Leave);
The Problem is, that the Leave event fires if the control isnt the active control. If you click the Button the TabPage changes from active to inactive because the Button is active control now.
Related
I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control
Your issue is that the event MainForm_KeyUp does not even get fired on your key up, because you have focues another control. But you can fix that with KeyPreview.
A Form object has the property KeyPreview. According to the MSDN:
Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
So when you set:
this.KeyPreview = true;
You enable that your MainForm gets notified about those key events always. Even when any other Control is focused. So you enable that those key events will invoke MainForm_KeyUp().
Now set a breakpoint:
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
//set a breakpoint here, so you get confirmation, that the event will get fired
//on key up of the *delete* button
//...now do what you desire
}
How to stop textbox leave event on clicking on linklable/button while Textbox was in focus?
I have a textbox TextBox1. On leave event of it, I have to validate it's text. And as per text of it, I have to populate suggestion of next textbox TextBox2.
so in this process, when I'm clicking on a link-label [which is there for different reason] while TextBox1 is in focus, It is firing Leave event [which is obvious] and calling validating functionality [which is not supposed to be called - because user have not left textbox after completing input- It was triggered by linkable click].
I have tried to unsubscribe Leave event on link-label's click event but that doesn't help as leave event is being fired in first place.
what should be done in this case ?
EDIT :
posting related code,
I'm Having same Event Handler for both textbox
private void txtBox_Leave(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.Visible = false;
#region TextBox 1
if (textBox.Equals(txtBox1))
{
//Text Box 1 validation
//Populating Suggestions for TextBox2
//Passing focus on Textbox 2
}
#endregion
#region TextBox 2
else if (textBox.Equals(txtBox2))
{
//Text Box 2 validation
}
#endregion
}
As I've mentioned earlier, Link label is there for different and I cannot disable or hide it during operation on Text box.
One observation is,
When I click on Link label, Leave event of text box raise first and after that Click Event of Link Label, so we cannot perform any logic (like unsubscribing leave event of Textbox or something else) on Link-Label's click event - as by the time validation process would be done which we don't want.
When you click on link label, it becomes focused control. What you can do is - find the focused control of the form in textbox1_leave event handler. If it is not that link label then only the functionality for populating suggestions of textbox2 should be done.
This link should be helpful for you to find out focused control of the form - What is the preferred way to find focused control in WinForms app?
Is there an event for panel that is equivalent to form event Shown?
I had a few couple of panel switching within a form which will never be closed.
However i couldn't find anything close to an event like Shown which is used in form.
The closes i had is Paint event. However i only wish to update the panel once every time it is shown.
Form.Shown is not raised every time the form is shown, rather it Occurs whenever the form is first displayed. This being said, there is no Panel.Shown event, and no event which is raised "whenever a panel is first displayed".
You can simulate this behavior with the Panel.Paint event, using a flag to keep track of whether it's been "shown" once before. This will make it behave similar to Form.Shown.
private bool panel1Painted = false;
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (!panel1Painted)
{
// do your shown stuff here
panel1Painted = true;
}
}
To keep in the spirit of Form.Shown, you may want to reset the flag if the Panel is reconstructed. This is not the same as shown.
You could listen on the VisibleChanged event and only act on when visibility = true.
https://msdn.microsoft.com/en-us/library/system.windows.forms.panel_events%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
You could also experiment with the Enter and Invalidated events to see if these give you the results you want.
Or if disabling the panel when leaving it is an option, you might be able to use the EnabledChanged event in your toolbox.
I have a small problem with focusing in .net/wpf. I have written a custom user control which contains a TextBox and a SearchButton. The user-control has a lost focus event, which validate the content of the textbox if the focus is leaving. But now I have the problem, if I click on my search button, the lost focus event of the user control gets fired, even if I click on the button in the custom user control. (The button additionally has the option TabStop="False":
The problem is, that I don't want to fire the event if I click on the button.
Set
Focusable="False"
of your search button and the TextBox will not lose the focus because the button doesn't get the focus.
You can Do this Check in Event like this
protected void LostFocusEvent(object sender, RoutedEventArgs e)
{
if(textBox.Text.Length>0)
{
// if there is any character in TextBox
return;
}
// your validation Code goes here
}
I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}