Reputation: 219
I am coding an invoicing application. The user should be able to add a new line to the billing section of the invoice by selecting a product from a ComboBox, which subsequently fills out various other fields (price, description, etc) based on the selected product.
My problem is this: When the user adds a product, I would like the program to automatically generate a new line for the next product. It seems like the only way to accomplish this would be to create a new set of controls with an incremented name, i.e. Product1, Price1, Desc1 for the first product, Product2, Price2, Desc2 for the second product, etc. Is this possible, or am I going about it all wrong?
Upvotes: 0
Views: 629
Reputation: 1423
I had to do something like this at one point and what I did was create a custome user control, in your case it would consist of a combo box, that is filled with products on load, and all the related controls, which would be filled out on the selectedindexchanged
event of the combo box. On your main form where you want to display this info place a panel and add your custom user control to the panels.controls
collection.
since all the functionality will basically be contained in the usercontrol itself you shouldn't run into naming issues.
As for adding the user control when the user makes a selection, you could dim the control withevents
and attach a custom event to the user control and handle it in the main form. since the controls would be added at run time you would need to attach the event handlers using the AddHandler
call.
So to break it down you would need:
if you want to interact with a certain product with the same button you can attach an ID in the code behind of the user control and set that everytime a new user control is added. this is not as hard it I think I make it sound, you can actually run linq on the panels control collection and get all the panels where any condition you designate is met and then do what you want from there.
As for your second question, I would assume your user control is pulling a bogus connection string from somewhere. I would try to track down where that is happening and go from there. Anything beyond that would just be a WAG and that doesn't really help you.
Upvotes: 1