将值传递给 .Net 中的表单
我想将整数值传递给 .Net 中的表单,以便它可以加载正确的数据。 我使用它是为了当我双击列表中的记录时,会打开一个表单,其中加载了该记录中的数据,以便可以对其进行编辑。 做这个的最好方式是什么? 我应该创建一个属性并在调用 Show() 方法之前设置它,还是应该重载构造函数或其他内容并以这种方式将值作为初始化值发送?
注意 - 这不需要将多个值发送到表单中 - 只需要一个值。
这应该不重要,但这是在 C++ .Net 中。
I want to pass an integer value to a form in .Net so that it can load the right data. I'm using this so that when I double click on a record in a list, a form opens with the data from that record loaded so it can be edited. What is the best way to do this? Should I create a property and set it before calling the Show() method, or should I overload the constructor or something and send the value in as an initialisation value that way?
Note - this doesn't need to work with sending more than one value in to the form - only one value will be needed.
It shouldn't really matter but this is in C++ .Net.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会建议别的东西。
创建一个静态方法(到您要打开的表单)- 将参数传递给静态方法。
将其留给静态方法来新建表单、加载数据并调用 Show 方法。
这样,调用表单就不必过多地干扰表单(ctor、设置值、调用 show) - 您可以保留此逻辑并将其封装在表单中 - 这意味着您也可以重复使用它,而无需复制代码。
I'd suggest something else.
create a static method (to the form you want to open) - pass the parameter to the static method.
leave it up to the static method to new up the form, load the data and call the Show method.
this way the calling form doesn't have to mess with the form to much (ctor, setting the value, calling show) - you keep this logic away and encapsulated in the form - which means you can also re-use it without copying the code.
使其在构造函数中成为强制性的。 如果您没有要编辑的内容,那么使用其中一种表单就没有意义。
Make it obligatory in the constructor. It wouldn't make sense to have one of these forms anyway if you don't have something to edit.
添加一个新的构造函数,该构造函数接受参数并调用默认构造函数。 通过保留默认构造函数,您仍然可以使用 Visual Studio 表单设计器,并且新构造函数所需要做的就是存储值。
或者您可以只添加一个存储该值的公共属性。 然后创建表单类的对象,设置属性并显示表单。
Add a new constructor that takes the argument and calls the default constructor. By keeping the default constructor you can still use the Visual Studio forms designer, and all your new constructor needs to do is to store the value.
Or you can just add a public property that stores the value. Then you create an object of the forms class, sets the property, and shows the form.