net windows form application. I have a combo box and a text box and a close window button. Now If I make any change in the combo box or textbox and click on the close window button, it should prompt the user to save the modifications.. If no modification are made ( The user will just run the application, doesnt make any modification) then it should not prompt the user. It should close directly.. How can I do this?
An easy way to do it is by adding a dirty member to the form, which I set to true whenever anything changes and then check it whenever the form is closing .
Override the OnClosing method of your form (or attach to the Closing event). In the handler check for modifications and display a message box to the user. If you do not want the form to close just set the e.Cancel property to false before returning.
One way is to keep a bool flag called _changed or something like that as a member variable on your form.
Then in the TextChanged event of the TextBox, and the SelectedIndexChanged event of the ComboBox you just set _changed = true.
Then, just before your form closes you prompt the user if _changed is true.
Edit:
If you have many TexBox controls on the form, you could hook them all up to the same TextChanged event handler. Then, no matter which TextBox's text changed, _changed will be set to true.
Then do the same with multiple ComboBox controls and one SelectedIndexChanged event.
If you really have many controls, rather than hooking each up manually, you could even write a method that recursively loops through the Controls collection of your form and hooks each type of control up to the appropriate event handler. Then you could reuse that method in more than 1 form to save you lots of time and maintenance, as when ever you add new controls, they will automatically be taken care of.
Related
i have two controls. First is a comment control, which allow the user to create, update and delete comments. It got own MenĂ¼bar with add, safe, delete button.
If someone want to add this control in his form it could be to big. So i have a second control, which is just a PopupContainerEdit with PopupControl. In the popupcontrol i add the commentcontrol. So i have an popupcommentcontrol and if someone want to add it in his form he just need a place for a small button of the popupcontaineredit.
My problem is, if i change the list of commentars (save, delete) in the commentarcontrol i fire a event, so that the popup can check the change of the list. So the Popupcontrol know if there are comments or not and i can swap the icon of the button. The enduser directly see on the icon of the button if there are comments or not. But if i fire the event to tell my popup "hey the list changes" the popup window get closed.
Is there a possibility to stop this close if i fire the event?
regards
With the prevelence of the "OK" and "Cancel" buttons at the bottom of forms/dialogs, it is odd to me that I can't seem to find a "standard" way to save control state.
For instance, I have a checked list box of filters. When the user clicks the OK button, it applies the filters to a data set and the form closes. If the user clicks the cancel button, the form undoes all the checked-item changes and the form closes.
In a perfect world, when the user clicks the "OK" button, the saved control state is overwritten with the current control state and a new-state flag is set. When the form is closing, if the new-state flag is set the form resets the flag, and if it is not set the form replaces the displayed control with the saved control state. That way if the cancel button is hit, all the checked-changes the user made are reset.
What is the best-practice way of handling a cancel button undoing changes to a control, or even an entire form? Is there a best practice solution? I could see this being necessary for text boxes, radial buttons, check boxes, and practically every control, so please try and keep it generic and not specific to checked list boxes.
I would suggest it's as simple as:
Keep the data reflected in the UI separate from the UI itself
When the form is loaded, set its contents based on the data
When the user clicks OK, save the changes to the data model (however that is achieved, which will depend on exactly how you're populating the model)
When the user clicks Cancel, don't save any changes
There's no need to "undo" the changes on Cancel - you just throw away the form. When you next want to show the form, the same data will be loaded as before, because you didn't save any changes to it.
The easy way: don't re-use form instances. Do var childForm = new MyChildForm(); before each childForm.Show();
I have a Windows Forms application and I want to be able to show a 'post-it note' type thing when the user does a specific action.
For example: The user does something which automatically hides a control.
My application should:
o Pop up a post it note which explains what happened.
o Hide the post it note again when the user clicks anywhere on the form.
I have implemented the post it note as a simple panel with a label in it, which shows and hides when specific things happen.
However, I can't seem to capture the OnClick event of the parent UserControl. The parent control is a nested control, containing a split container, one side of which contains the panel and a tab control, each of which contains a user control with various things in it.
Apart from handling the click event of every single child control, can anyone think of an event that I can capture on the parent control that I can use to hide the post it note when the user clicks anywhere in the parent control?
Thanks,
Rik
That's what the Capture property was designed to do. Set it to true when you pop up the note. Any mouse events will now be directed to your control, even if the mouse moves outside of the window. This is also the way that, for example, the combobox dropdown list works. Keep in mind that it is only good for one click.
If the popup contains any controls itself then mouse capture isn't the solution. Make it an owned form instead and simply call Close() in an event handler for the Deactivate event.
There is bubling of events in windows form, while you click on child event, the event is raised for child, and then for parent. Unless you specify "handleEvnet" property to true. So just leave it false, untill event reaches parent.
I have a feature request that when a ComboBox is 'clicked into' that it clears the text so that the user can start entering in new data to search. Does anyone know of a way to hook into this? The 'click' event is raised on when the text is clicked as well as when the drop down arrow is also clicked (which opens up the drop down with items). I only want it to happen on the first, not the latter.
Right now I'm capturing the click event and filtering on the DroppedDown property like so:
if(!comboBox.DroppedDown)
{
// clear selection
}
This seems to work most of the time, but bugs out frequently as well... so its not 100%.
If anyone knows of a proper way to do this I would appreciate!
Don't handle the click event. For one thing, it won't fire if the user tabs the focus into the control. Use the Enter event which fires when the control receives focus. And rather than clearing text you should just select it all which will give the best of both worlds:
1) The user can start entering new text which will clear any old text or
2) tab past the control and leave the contained text as it was.
If you always remove the previous text you may anger users.
Try the "Enter" event. It happens when a control gains focus on the form.
I'm currently updating it on click, but this results in the user being able to see the repopulation occur. Which other event can I use which will allow me to handle it myself, then show the combobox when i'm ready? ( after population)
I don't know what you're developing, but that combobox is probably on a window or so that will have an event that fires on show. Use that event to populate the combobox in.
[edit] Ah Winforms. Use the Load event.
[edit2] On each click eh.
Alright. I found a dirty solutions that advises you to override the WndProc and capture messages, but I think it's better to inherit the combobox and override OnDropDown to perform you populating before calling the ancestor's OnDropDown method.
You should populate the box when entered too, because a value may be selected using the keyboard (arrows) without even dropping down the box. You'll need both if you want it on each selection, because a click only causes the Enter event when the box didn't have focus before.
Have you tried the ComboBox.DropDown Event?
You could try to call SuspendLayout() before updating and calling ResumeLayout() after the changes.
combobox.add_HandleCreated triggers after the control is created as the form is loading