Stateful Winform and Cancel Buttons - c#

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();

Related

c# pass parameters from a previous action

I'm writing an application that will have a datagridview control and 4 textboxes.
One requirement for the application is that when the user double clicks a row in the datagrid, the 4 textboxes fill with the corresponding textboxes with the data -> this is already done and working. Once the user has edited the field and clicks OK, they will be saved on the datagridview.
On the other hand, if the user clicks a button called "Add Register", the same buttons will be enabled and once they click OK a new register with the data in the textboxes will be created.
The thing is that I don't know how and I have not found any information about knowing if I should edit a row (and which one) or add a new one when OK is clicked.
In other words: how can I know if I'm coming from an "edit" request or an "Add register" request when the user presses "OK"?
Thanks!
Welcome to StackOverflow.
The simplest way to achieve this is to have a boolean field on your form, e.g. bool isEdit. When the user double clicks a line in the DataGridView (a handler for which you say you have working), then set the field to true. Within the Add register button handler, set the field to false.
Now within your OK handler, you simply examine the value of the isEdit field to know, if you are adding or editing!
I have done a lot (a very great lot!) of such DataGridView forms. Personally I do not like to combine the form with data entry. I prefer to leave my DataGridView form as totally read-only, and then when the user clicks edit or add buttons, I display a separate modal form to handle the data entry. On saving from the modal form, I refresh the grid.
HTH

Popup Control closes on fireing event

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

How do you create an EWF UI page that modifies data when the user clicks a button?

I want to make a page that collects some information from the user and then modifies the database if the user clicks a button. If the user navigates away from the page without clicking the button, I don't want anything to be modified.
How do I do this without having to manually put my own button in the content area?
Call EwfUiStatics.SetContentFootActions from LoadData of the page. This will cause a big button to be displayed in the lower right corner of the page. If the user clicks it, the data modification that you passed to the method will be executed.

passing data from Ajax Modal popup extender to a textbox on the asp.net page

I have a Ajax modal popup that displays a set of options for the user. On submit button click event on the ajax modal popup, i need to pass the user selected data back to a text box on the user control (which has the modal pop up) on the calling page.
Structure/flow is as follows. There is a page and two user controls. One is a search control that has another user control that contains the user options. The master page has the search user control. When the user chooses an option in a dropdownlist in the search control, it does a mpe.Show of the user control with options. User makes his selections and hit submit button. In the button click event in the popup, i delegate an event back to the search user control which tries to set the value in one of its text boxes. Everything is going fine until this step and i can see the value but the text box never changes. It seems like the user control is already rendered and the changes are ignored. Any idea how I can get around this?
In short, how to get back the data to a control from an Ajax modal popup.
Use jquery - when the user clicks a button on the modal - use jquery to set the value field to be the data that the user has selected.
e.g.
$('#modalButton').click(function() {
var userData = $('#tbUserData').val();
$('#textBoxElsewhere').val(userData);
});
for anybody's future reference, i did a work around for this. What was happening is that, on the final button click postback, the page/parent user control load events run first and then the button click event.. so the changes that were being made in the button click event did not make it back to the parent user control on the page.. i had to add a middle step to display the user selections for approval and force the user to hit a final confirmation hit. On that post back, the changes were already available for the parent user control, and not have to wait for the button click event to trigger to grab the data. I'm sure there is a better way to do this but this is what I could come up with.

Confirmation before closing

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.

Categories