Databound control won't lose focus [duplicate] - c#

I have a Windows Forms Application. I have several forms in this application (a main form, and several specialized forms), and on only one form, click events are not firing for any of my buttons.
It is not that the code in the handler is broken. This can be determined by the fact that a breakpoint on the first line of the handler is never reached when clicking the button.
Other events are working (I'm using CheckedChanged events on this form and they are behaving).
My team members have reviewed, and also can't spot the problem.
Here is a simplified view of my code:
Designer Generated Code
partial class MyForm
{
private System.Windows.Forms.Button addButton;
private void InitalizeComponent()
{
this.addButton = new System.Windows.Forms.Button();
this.addButton.Name = "addButton";
// Drawing statements here
this.addButton.Click += new System.EventHandler(this.addButton_Click);
this.Controls.Add(this.addButton);
}
}
My Code
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
MessageBox.Show("The debugger is not reaching a break point on this line");
}
}
Edit: Additional Information from Testing
There are several data-bound dropdownlists in my form. I have discovered that the click event only fails to fire if I make a selection in a drop down box first.
If I make no selections, the break point in the button's handler fires. Otherwise it doesn't. There are no events registered on these drop down lists.

Here is the reason:
When using data binding, when you enter a value in a data bound control, it first tries to validate entry and then if the entry was valid, data binding will put the value in data source, but if a validation error occurs validation returns false and your control goes to invalid mode.
When a child control of form didn't validate, by default you can not change focus from invalid control.
Click on a button by default causes validation of the control that are losing the focus, so you can't click on button, as you see your button reflect to mouse but not actually click.
The same problem will happen if you handle Validating event of a control like TextBox and set e.cancel = true.
Here is the fix:
you can fix this behavior using either of following options:
Set CausesValidation property of your button to false
Set AutoValidate property of your form to AutoValidate.EnableAllowFocusChange

This will do the trick for you
Change
public ScheduleMeeting()
{
InitializeComponent();
}
to
public MyForm()
{
InitializeComponent();
}

I have discovered the issue after further testing.
I the issue is not with button events, but with the form becoming blocked after making a selection from a drop down box.
I have not yet discovered why the form blocks after the drop down is selected (it has no events, but does have databinding, so there are some possible causes there).
Thank you for all your help!

Related

Showing/Hiding a control from within another control

I have got a Windows forms application with a main application form and two controls I added to my main form.
The first control has got a login button. When that button is pressed, I want to hide control1 and show control2, which is currently hidden.
Hiding control1 works by using this.Hide() in the onClick event of the login button, but I have so far been unable to find a way to reference control2 and call .Show()
How can I show control2 from within the onClick event of the login button?
EDIT:
Hope the below information helps.
Controls:
LoginMenu.cs - Has all of the control's code in it. - Control name in MainForm.cs is loginMenu1
TicketSearch.cs - Has all of the control's code in it. - control name in MainForm.cs is ticketSearch1
After adding all controls, I rebuilt the project and dragged them into the main form from the tools menu.
The code for the login button is in LoginMenu.cs
public partial class LoginMenu : UserControl
{
public LoginMenu()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
*CODE HERE*
}
void ButtonLoginClick(object sender, EventArgs e)
{
*CODE HERE*
try
{
HttpWebResponse response =
(HttpWebResponse)loginTest.GetResponse();
}
catch (System.Exception ex)
{
if (ex.Message.Contains("401") == true)
{
*CODE HERE*
}
else if (ex.Message.Contains("403") == true)
{
*CODE HERE*
//Hide login control. This works
this.Hide();
//This is where I want to show the control ticketSearch1
}
}
}
}
Thanks to those who tried to help. I learnt something new today.
For some reason, I don't quite understand I could not access the controls in MainForm.cs from LoginMenu.cs even though they are in the same namespace and the control 'Modifiers' property is set to 'Public'.
I resolved this by adding the following method call to my login button onClick action to reference the control I wanted Form.ActiveForm.Controls.Find("ticketSearch1", true)[0].Show();
But it should work.
Look at the properties of Control2 and pick the name (or set a name) under the line "(Name)".
then you should:
private void button_Click(object sender, EventArgs e)
{
firstcontrol.Hide();
secondcontrol.Show();
}
it should work.
Maybe you failed with the placing of the control. So look what happens if you start your programm with the first control hidden and the second one shown. And try to hide the second control and show the first.
There must be the problem.
Did you place your Control via "Toolbox"?
Or do you call your Control via code?
EDIT:
Referring to your inserted Code, it seems, that you call this code in your Usercontrol.
So you need to set the second Usercontrol to "Public". When you click on it and go to the Properties, you choose "Mofifiers" and set it to public.
Now you should look for the (Name) Attribute in his properties.
Then just add:
NameOfYourSecondControl.Show();
This will fix your problem.

Initializing Form Controls

I have a dialog box in my program where a user can change certain settings. When the form is closed, the changes are stored in the app's settings file.
Every time the dialog loads, the settings are restored in the constructor of the form. This creates a problem: the CheckedChanged event (in the case of a checkbox as an example) will always be triggered by the time the form fully opens, without the user doing anything!
I have not tried this, but it is my guess one way to overcome this problem would be to pull the initial statuses of the controls in the designer under Data->Application Settings. But this approach requires a separate setting for every control - not practical due to the large number of controls being one problem.
Is there a (better) way to pre-initialize controls on a form without triggering the CheckedChange event?
You could create a class-level variable called IsFormLoading. Set it to True at the beginning of your constructor, then set it to False at the end, after all the controls have been initialized and settings have been restored.
Each of the events on the form that you don't want to be triggered could check if (IsFormLoading) and just return;.
I'm assuming you have event subscriptions in InitializeComponent() method, something the following line :
private void InitializeComponent() {
...
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
...
}
and that you restored the form's settings after the InitializeComponent() method is called in your constructor.
If so, why not try to defer the event subscriptions after all the controls are fully initialized with your saved settings. Remove all event subscription (Event += Handler) from InitializeComponent() and place it after the settings are restored.
class Form1 : Form {
public Form1() {
InitializeComponent(); // remove event subscriptions from InitializeComponent
ApplySettings(); // restore settings
AttachEventHandlers(); // subscribe to events here
}
}
So the basic over-view is the pop-up shows, user checks some boxes, closes the form, and changes are saved right?
First question: If your just saving the current state of the controls (checked, not-checked,etc), why even deal with the CheckedChange event? Why not just save all the values of the controls in the Form.FormClosing event? I mean I understand the changed event is called because you are setting the values controls in the form load event, but why bother with the changed event unless you are doing something when the change event is fired??
If you really want to use the CheckedChange event though to perform some actions then you could use a counter like so (top of my head code so may be a little buggy):
private int _loadCount;
private void Form1_Load(object sender, System.EventArgs e)
{
_loadCount = 0;
//Code to load values for controls...
}
private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
_loadCount++;
if(_loadCount > 1)
{
//Do something here...
}
else
{
//Do nothing, return false, etc...
}
}
In any case, AFAIK the controls changed event always fires regardless of how the controls value was changed (user click, via code, etc).

How To supress getting focus to TextBox and its form when Text Changes in C#?

I have a Form (Form1) and a Rich Text Box (RichBox1) inside of it.
I have many objects that can append text in RichBox1
and Two Other Forms (Form2, Form3)
When that happens, if the application does not have focus (say user is using a calculator)
and when the user sets focus back to the application thru selecting Form2,
Form1 gets focus first if the RichBox1 got new text while the user wasn't using it.
I've looked at all properties and can't find it. a .CanFocus for the textbox cannot be set to false as it can only be read. Any ideas?
Thanks
Edit:
I've Added:
this.GotFocus += new EventHandler(Form1_GotFocus);
This event does not fire at all.
I have a whole bunch of other controls in Form1, a few labels, buttons and menu strips. I do not understand what is causing this behaviour.
EDIT 2:
Perhaps can someone suggest an event to raise in Form1 to start tracking what is causing this. I am assuming it has to do with RichBox1 but not so sure now.
EDIT 3.
Added:
this.Activated += new EventHandler(Form1_GotFocus);
This event does not fire when I do the above. Perhaps this means that Form2 loses focus, instead of Form1 getting activated? Also worth noting this behaviour appears when I right click in Form2
Try using the Form Activated event. Though I made a small test case and I can not duplicate your problem, if the RichTextBox's Text Changes I can still select Form2, make sure you do not have a timer or event setting focus to the RichTextBox
public Form1()
{
InitializeComponent();
this.Activated += new EventHandler(Form1_Activated);
}
void Form1_Activated(object sender, EventArgs e)
{
//Set the Focus to the Control that you want
button1.Focus();
}

Is there any way to detect a mouseclick outside a user control?

I'm creating a custom dropdown box, and I want to register when the mouse is clicked outside the dropdown box, in order to hide it. Is it possible to detect a click outside a control? or should I make some mechanism on the containing form and check for mouseclick when any dropdownbox is open?
So I finally understand that you only want it to close when the user clicks outside of it. In that case, the Leave event should work just fine... For some reason, I got the impression you wanted it to close whenever they moved the mouse outside of your custom dropdown. The Leave event is raised whenever your control loses the focus, and if the user clicks on something else, it will certainly lose focus as the thing they clicked on gains the focus.
The documentation also says that this event cascades up and down the control chain as necessary:
The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.
Overriding your UserControl's OnLeave method is the best way to handle this:
protected override void OnLeave(EventArgs e)
{
// Call the base class
base.OnLeave(e);
// When this control loses the focus, close it
this.Hide();
}
And then for testing purposes, I created a form that shows the drop-down UserControl on command:
public partial class Form1 : Form
{
private UserControl1 customDropDown;
public Form1()
{
InitializeComponent();
// Create the user control
customDropDown = new UserControl1();
// Add it to the form's Controls collection
Controls.Add(customDropDown);
customDropDown.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Display the user control
customDropDown.Show();
customDropDown.BringToFront(); // display in front of other controls
customDropDown.Select(); // make sure it gets the focus
}
}
Everything works perfectly with the above code, except for one thing: if the user clicks on a blank area of the form, the UserControl doesn't close. Hmm, why not? Well, because the form itself doesn't want the focus. Only controls can get the focus, and we didn't click on a control. And because nothing else stole the focus, the Leave event never got raised, meaning that the UserControl didn't know it was supposed to close itself.
If you need the UserControl to close itself when the user clicks on a blank area in the form, you need some special case handling for that. Since you say that you're only concerned about clicks, you can just handle the Click event for the form, and set the focus to a different control:
protected override void OnClick(EventArgs e)
{
// Call the base class
base.OnClick(e);
// See if our custom drop-down is visible
if (customDropDown.Visible)
{
// Set the focus to a different control on the form,
// which will force the drop-down to close
this.SelectNextControl(customDropDown, true, true, true, true);
}
}
Yes, this last part feels like a hack. The better solution, as others have mentioned, is to use the SetCapture function to instruct Windows to capture the mouse over your UserControl's window. The control's Capture property provides an even simpler way to do the same thing.
Technically, you'll need to p/invoke SetCapture() in order to receive click events that happen outside of your control.
But in your case, handling the Leave event, as #Martin suggests, should be sufficient.
EDIT: While looking for an usage example for SetCapture(), I came across the Control.Capture property, of which I was not aware. Using that property means you won't have to p/invoke anything, which is always a good thing in my book.
So, you'll have to set Capture to true when showing the dropdown, then determine if the mouse pointer lies inside the control in your click event handler and, if it doesn't, set Capture to false and close the dropdown.
UPDATE:
You can also use the Control.Focused property to determine if the control has got or lost focus when using a keyboard or mouse instead of using the Capture with the same example provided in the MSDN Capture page.
Handle the Form's MouseDown event, or override the Form's OnMouseDown
method:
enter code here
And then:
protected override void OnMouseDown(MouseEventArgs e)
{
if (!theListBox.Bounds.Contains(e.Location))
{
theListBox.Visible = false;
}
}
The Contains method old System.Drawing.Rectangle can be used to indicate if
a point is contained inside a rectangle. The Bounds property of a Control is
the outer Rectangle defined by the edges of the Control. The Location
property of the MouseEventArgs is the Point relative to the Control which
received the MouseDown event. The Bounds property of a Control in a Form is
relative to the Form.
You are probably looking for the leave event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx
Leave occurs when the input focus leaves the control.
I just wanted to share this. It is probably not a good way of doing it that way, but looks like it works for drop down panel that closes on fake "MouseLeave", I tried to hide it on Panel MouseLeave but it does not work because moving from panel to button leaves the panel because the button is not the panel itself. Probably there is better way of doing this but I am sharing this because I used about 7 hours figuring out how to get it to work. Thanks to #FTheGodfather
But it works only if the mouse moves on the form. If there is a panel this will not work.
private void click_to_show_Panel_button_MouseDown(object sender, MouseEventArgs e)
{
item_panel1.Visible = true; //Menu Panel
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!item_panel1.Bounds.Contains(e.Location))
{
item_panel1.Visible = false; // Menu panel
}
}
I've done this myself, and this is how I did it.
When the drop down is opened, register a click event on the control's parent form:
this.Form.Click += new EventHandler(CloseDropDown);
But this only takes you half the way. You probably want your drop down to close also when the current window gets deactivated. The most reliable way of detecting this has for me been through a timer that checks which window is currently active:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
and
var timer = new Timer();
timer.Interval = 100;
timer.Tick += (sender, args) =>
{
IntPtr f = GetForegroundWindow();
if (this.Form == null || f != this.Form.Handle)
{
CloseDropDown();
}
};
You should of course only let the timer run when the drop down is visible. Also, there's probably a few other events on the parent form you'd want to register when the drop down is opened:
this.Form.LocationChanged += new EventHandler(CloseDropDown);
this.Form.SizeChanged += new EventHandler(CloseDropDown);
Just don't forget to unregister all these events in the CloseDropDown method :)
EDIT:
I forgot, you should also register the Leave event on you control to see if another control gets activated/clicked:
this.Leave += new EventHandler(CloseDropDown);
I think I've got it now, this should cover all bases. Let me know if I'm missing something.
If you have Form, you can simply use Deactivate event just like this :
protected override void OnDeactivate(EventArgs e)
{
this.Dispose();
}

.NET UserControl accept button best practice?

I have a windows forms app where I have split different functionality into several user controls. I want each of these user controls to have an accept button.
Any best practices here?
My idèa is to detect which user control that has focus, and than set it in the parent Form.
Any other idèas?
The best practice is usually to only have one accept button for your form so that its behavior is consistent. It generally would be confusing for users if hitting return caused different actions depending on which section of the form had focus. However, if you have a specialized application or users have requested this feature then I think the solution you propose would work.
Jan Miksovsky has an excellent blog on UI design, and wrote an article about this very thing.
Most UI platforms allow a designer to
indicate which button in a dialog
should be the default button: the
button that will be pressed if the
user types the Enter key. The default
button is generally the button the
user is most likely to press next,
often a button like OK that closes the
dialog. In very high-traffic dialogs,
you may want to consider dynamically
changing the default button to save
keystrokes and help speed the user's
task.
The example he uses is the "Select Names" dialog in Microsoft Outlook, which changes the default button depending on what you are doing.
I assume each user button is its own instance on the individual user controls?
If so then you can trap the button events on the Parent form. If you expose the individual buttons through a property you can tie into their Click events. Like all controls they have a name property so you can have one method that is called on all button click events.
Below I have a partial sample code. I have two user controls that have one button each. The button on UC1 is named "btn1" and "btn2" for UC2. I call the exposed property "ButtonOK"
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public Button ButtonOK
{
get { return btn1; }
}
}
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
public Button ButtonOK
{
get { return btn2; }
}
}
Now on the parent ("Form1") when it loads have a mthod that ties into the Click events of each button but it calls the same method. Inside the method I test for the "Name" property.
public Form1()
{
InitializeComponent();
}
void Form1_Load(object sender, EventArgs e)
{
RegisterButtonEvents();
}
void RegisterButtonEvents()
{
userControl11.ButtonOK.Click += new EventHandler(ButtonOK_Click);
userControl21.ButtonOK.Click += new EventHandler(ButtonOK_Click);
}
void ButtonOK_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
if (btn.Name == "btn1")
{
Console.WriteLine(" ButtonOK from UserControl1 was pushed. The tag is " + btn.Tag.ToString());
}
else if (btn.Name == "btn2")
{
Console.WriteLine(" ButtonOK from UserControl2 was pushed. The tag is " + btn.Tag.ToString());
}
}
}
You can also user the "Tag" property of a control. This property can be very useful as it can reference objects.
You don't need to do exactly as shown but you can use any "Parent" form to get a reference to the UserControls, have them expose their Buttons, then you can do anything you want with properties and events from those Buttons (or any control for that matter).
Keep in mind that if you are tying into the click event on the user control also (in addition to the parent form), you will have to be mindful of the order in which it will enumerate through it list of delegates and execute code after the event is intiated.
Hope that helps.
I know this is an old post, but I think I figured it out.
Use the "Enter" event on each user control from the main form, such that when the user "enters" (focuses on) the user control, this.AcceptButton = myUserControlButton. You can also use the "Leave" event on each user control to set the accept button back to the default, if you have one.
I'm not sure if I understood your question correctly.
If you want to assign one event to several buttons:
For this you could for instance:
- Get the button name on the Button_Click event.
- Enumerate between names
- Iterate over the controls.
Example bellow:
"How to get the button name from the Button_Click event".
// First; dont forget to assign the "Button_Click" event to your Button(s).
private void Button_Click(object sender, EventArgs e)
{
// The line bellow assigns to "btn" variable the currently clicked button.
Button btn = (Button)sender;
// Then using a switch block; you can compare the button name and
// perform the action desired for the clicked button.
switch(btn.Name)
{
case "buttonName1": /* Do Something Here */ break;
case "buttonName2": /* Do Something Here */ break;
// etc
}
}
Additionally; if you require; there's always the way to retrieve the Button outside the form class by exposing them.

Categories

Resources