VB.NET/Visual Studio 2010,在运行时递增控件名称 [英] VB.NET/Visual Studio 2010, incrementing control names at runtime

查看:99
本文介绍了VB.NET/Visual Studio 2010,在运行时递增控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写发票应用程序。通过从ComboBox中选择一种产品,用户应该能够在发票的开票部分中添加新行,该产品随后会根据所选产品填写其他各个字段(价格,说明等)。

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.

我的问题是:当用户添加产品时,我希望程序自动为下一个产品生成新行。看来,完成此操作的唯一方法是创建一组名称递增的新控件,例如,第一个产品的Product1,Price1,Desc1,第二个产品的Product2,Price2,Desc2,等等。还是我要解决所有问题?

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?

推荐答案

我不得不在某一时刻做这样的事情,而我所做的就是创建一个自定义用户控件,在您的情况下,它将由一个组合框(装有要装载的产品)和所有相关控件组成,并在 selectedindexchanged 组合框的事件。在要显示此信息的主窗体上,放置一个面板,然后将自定义用户控件添加到 panels.controls 集合中。

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.

对于在用户进行选择时添加用户控件,您可以将控件 withevents 变暗并附加一个自定义事件传递给用户控件,并以主要形式对其进行处理。由于控件将在运行时添加,因此您需要使用 AddHandler 调用附加事件处理程序。

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. 自定义用户控件
  2. 自定义用户控件上的一个事件,告诉主窗体组合框具有选定的值
  3. ,其中包含每个产品所需的所有控件以及代码中的ID。 >
  4. 主窗体上的一个面板,用于保存自定义用户控件

  5. 一个事件处理程序,用于在其中进行选择时向该面板添加新的客户用户控件

  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.

如果要使用相同的按钮与某个产品进行交互,可以在ID后面的代码中附加一个ID用户控件,并设置为每次添加新的用户控件。这并不是我想的那样难,实际上,您可以在面板控件集合上运行linq,并在满足您指定条件的所有面板上进行操作,然后从那里进行您想要的操作。

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.

关于第二个问题,我假设您的用户控件正在从某个位置提取虚假的连接字符串。我会尝试跟踪发生的情况并从那里继续。除此之外,任何其他事情都只是WAG,并不能真正帮助您。

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.

这篇关于VB.NET/Visual Studio 2010,在运行时递增控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆