C# - Refresh dropdown after a child form closes - c#

I have a c# application with multiple "worker" forms. These forms have numerous comboboxes that are populated from the database on form load, with 'add' buttons beside them. When a user clicks the add button the administrative form is opened allowing the user to add a corresponding value to the database.
For instance, the combobox may be a list of street types. "Drive" is not in the street types table in the database, so the user wants to add it. They click the add button and the admin form is loaded so they can add the "Drive" value to the street types. When the admin form closes, I want to repopulate the combobox upon return to the worker form.
Any insight as to the best way to accomplish this?

Thanks guys. I used ShowDialog and it worked great.
Administration adminForm = new Administration();
adminForm.tcAdministration.SelectedIndex = 1;
adminForm.ExistingCaseNumber = this.ExistingCaseNumber;
adminForm.ShowDialog();
this.PopulateComboBoxes();

When the forms that can administer the lists is created, I'd add an event handler for the Closed event of the form. In that event handler is where I would reload the data source for the list, and then rebind it to the combobox.

One way is to create the form as a modal form, and you can use the this.Parent, and access a public method from there that updates the combo box.

It takes more work to set it up if you haven't from the start, but if you're doing the proper MVP[~] thing, that child "Add" form should trigger an update in your model, which your controller observes, which reacts by updating that portion of the view.
[~] Martin Fowler has retired his use of the term Model-View-Presenter, but he's still waffling between Supervising Controller and Supervising Presenter as its replacement.
A couple references:
http://martinfowler.com/eaaDev/SupervisingPresenter.html
http://msdn.microsoft.com/en-us/magazine/cc188690.aspx

You can also do:
Form1 frm = (Form1)Application.OpenForms["Form1"]; This will allow you to update the form from another one.

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

c# Passing an index of a list item from one form to another

In c#, how do you write the code so that when a user select an item in list box and click edit, it's data displays in separate textboxes in another windows form? I'm guessing the passing of the selected index between the two forms is required.
I think you can create a Form and set a String property. Every time you click edit button, just pass that item's text to the Form's contructor.

Stateful Winform and Cancel Buttons

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

swicth combobox to textbox

I have add a options form to my application. There is 2 radio buttons and when the options form is closing that form saves the data to database user is selected witch one of these radio buttons.
And I have another form is writing some data to database, user is selecting a item from combobox's list to add. I want to user can add that data manually too without of this combobox. When user select "manual" on options form, combobox must disappear and a textbox must appear and user is selected "list" then combobox must appear.
Hot can i do that? I need a new sql query too.
If it's just the two controls, one easy way of doing it would be to place both of them on the form, on top of each other, and then just make one of them invisible, and when the user selects the other options, just change visibility on both of them.
You'll have to make sure that your form closing code that saves the data reads from the correct control of course, but that's just the matter of a simple if statement to check which of them is visible.

How do I override the OK/Cancel button of new list item form in SharePoint?

I have a custom list in SharePoint, and I want to override the OK/Cancel buttons that are in the New Item form (The form displayed when you click "New" in the default view of the list).
I want the saving logic to remain intact, but I just want to change the default page it redirects to, ideally, to a whole separate page on the site that's not a view of the list.
Is there any way I can achieve this?
Thanks in advance!
Depending on what you are looking for, a programmatic approach would be to write an Event Handler and after the ItemUpdated or ItemAdded event redirect to the location you want.
For more information
Ted Pattison MSDN Article on Event Handlers
Yes you can do that. [I guessing you are working with list]
Navigate to NewForm.aspx of that particular list
Open it for editing
Go to Data view Controls and drop Form action control to the form
Add Commit and navigate to page actions in sequence.

Categories