Reputation: 321
I have a windows form application with a list style form with a parent record and many related children records for any selected parent record.
ie. Customers with multiple invoices / quotations / correspondence etc etc etc.
My form layout has a list of parent records and when the user selects a parent record other lists on the form are populated with the related child information. All of these lists the user can open a detail form for any of the records. For instance the user can select customer 1 and then select details and a customer details form will open with the customer 1 information displayed. The user can go back and select a related record for customer 1 lets say Invoice 1 and then open the invoice detail form displaying invoice 1 information.
So at any point in time the user may have several invoice / quotations / correspondence etc etc etc detail forms open. The user could also select another customer and go through the same process of opening detail forms.
The problem I have is that when customer 1 is selected and the user wants to display the detail form I need to check all open forms to see if customer 1 detail form is already opened.
I was wondering what is deemed to be good practice in this suituation.
I was trying to find some generic approach which I could apply to all forms in some way.
Upvotes: 2
Views: 666
Reputation: 3322
Other option maybe is to use MDI form. Open up all your forms, as a child of main MDI form. While opening a new form, you can always check whether it already exists.
Read this article for details.
Hope this helps.
Advantage : No need to maintain separate list of already open instances.
Upvotes: 0
Reputation: 12521
You could store an IDictionary<Customer, Form>
somewhere. This would enable you to do a simple and O(1) lookup:
var dict = new Dictionary<Customer, Form>();
var cust = new Customer();
if(!dict.ContainsKey(cust) || dict[cust].IsDisposed)
{
dict[cust] = new Form1();
}
dict[cust].Show();
dict[cust].Activate();
This sample is shortened a little: dict
would of course not be local, nor would be cust
. They'd be an instance field and a parameter.
Upvotes: 1
Reputation: 5909
As I guessed, you need to open same form for multiple times, but you don't need to show same customer information twice. If it's so, then one option is to create static list of customers in your customer details form (for example list of customer Id) and check new instance. If customer exists in static list, then you can cancel showing or focus already opened details form (If you save form handle of course).
Upvotes: 0
Reputation: 56727
One approach would be to store the form instances in a list and trying to find the corresponding instance before opening a new form (things get easier if you have some "base form" which all others inherit from, which has a property for the customer number).
You need to make sure that the list item is removed when the form is closed.
If you want, I could provide pseudo-code here.
Upvotes: 0
Reputation: 19800
What you can do is make a list of the customers on the left, and have a panel with customer info on the right. Clicking on a customer will update the panel with the new customer information.
(Think of how explorer works in windows. Folders on the left, info of the selected folder on the right)
The panel will hold the customer info, such as the name and list of invoices etc. Clicking on the invoice will open the invoice in a new form. The customer detail don't need to be opened in a new form.
Upvotes: 0