简体   繁体   中英

VB.NET/Visual Studio 2010, incrementing control names at runtime

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, ie 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?

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:

  1. A Custom user control with all the controls you want for each product and an ID in the code behind
  2. An event on your custom user control to tell the main form that the combo box has a selected value
  3. A panel on your main form to hold the custom user controls
  4. An event handler to add a new customer user control to the panel when a selection is made in the combo box.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM