VB.Net Winforms用户控制变量

时间:2022-10-27 00:02:31

I'm using VS 2005 in a VB.Net WinForms application. I have a custom User Control that requires a variable to render its data correctly. My question is, what's the best way to require the calling sub to populate the variable? I've thought of a couple of options:

我在VB.Net WinForms应用程序中使用VS 2005。我有一个自定义用户控件,需要一个变量来正确呈现其数据。我的问题是,要求调用子填充变量的最佳方法是什么?我想到了几个选择:

  • Have a WriteOnly property and check to see if it is "Nothing" when the User Control is loaded and throw an exception if this is the case. I don't like this because the error would be at runtime and I would like to require the variable to be populated at design time (like passing parameters to a sub/function).

    有一个WriteOnly属性,并在加载用户控件时检查它是否为“Nothing”,如果是这种情况则抛出异常。我不喜欢这个,因为错误是在运行时,我想要在设计时填充变量(比如将参数传递给子/函数)。

  • Have a global variable to hold the variable that I want the User Control to use. I could also use the exception with this technique too, but, like the option I stated above, the error would be thrown at runtime.

    有一个全局变量来保存我希望用户控件使用的变量。我也可以使用这种技术的异常,但是,就像我上面提到的选项一样,错误将在运行时抛出。

What are your suggestions for populating the required variable that the User Control needs at design time?

您有什么建议填写用户控件在设计时需要的变量?

Thanks in advance.

提前致谢。

3 个解决方案

#1


Add the variable as a parameter to all public constructors for the control?

将变量作为参数添加到控件的所有公共构造函数中?

#2


I've been down this road a few times with custom controls and I've come to one truth

我已经使用自定义控件在这条路上走了几次,我已经明白了一个道理

Custom Controls must render without crashing if necessary properties are not set.

如果未设置必要的属性,则自定义控件必须呈现而不会崩溃。

There are just far too many cases where you will get into this situation. The primary example is the WinForm designer. As soon as you drag your control onto a host, WinForms will create an instance of it and render it in the VS process. By default it will pass no variables to the constructor and hence the initial render will happen without any of your properties set.

你遇到这种情况的情况太多了。主要的例子是WinForm设计器。只要将控件拖到主机上,WinForms就会创建一个实例并在VS进程中呈现它。默认情况下,它不会将任何变量传递给构造函数,因此初始渲染将在没有设置任何属性的情况下发生。

You'll save a lot of time if you have your control render in some lesser state when the property is not set. This is how many of the standard controls work. Usually I have the control render a message to the effect of "Lacking Property X" or simply display nothing.

如果在未设置属性的情况下将控件渲染设置为较小的状态,则可以节省大量时间。这是多少标准控件的工作原理。通常我让控件呈现一条消息“Lacking Property X”或者根本不显示任何内容。

#3


I would overload the constructor, the default constructor would set the variable to a predetermined default value.

我会重载构造函数,默认构造函数会将变量设置为预定的默认值。

#1


Add the variable as a parameter to all public constructors for the control?

将变量作为参数添加到控件的所有公共构造函数中?

#2


I've been down this road a few times with custom controls and I've come to one truth

我已经使用自定义控件在这条路上走了几次,我已经明白了一个道理

Custom Controls must render without crashing if necessary properties are not set.

如果未设置必要的属性,则自定义控件必须呈现而不会崩溃。

There are just far too many cases where you will get into this situation. The primary example is the WinForm designer. As soon as you drag your control onto a host, WinForms will create an instance of it and render it in the VS process. By default it will pass no variables to the constructor and hence the initial render will happen without any of your properties set.

你遇到这种情况的情况太多了。主要的例子是WinForm设计器。只要将控件拖到主机上,WinForms就会创建一个实例并在VS进程中呈现它。默认情况下,它不会将任何变量传递给构造函数,因此初始渲染将在没有设置任何属性的情况下发生。

You'll save a lot of time if you have your control render in some lesser state when the property is not set. This is how many of the standard controls work. Usually I have the control render a message to the effect of "Lacking Property X" or simply display nothing.

如果在未设置属性的情况下将控件渲染设置为较小的状态,则可以节省大量时间。这是多少标准控件的工作原理。通常我让控件呈现一条消息“Lacking Property X”或者根本不显示任何内容。

#3


I would overload the constructor, the default constructor would set the variable to a predetermined default value.

我会重载构造函数,默认构造函数会将变量设置为预定的默认值。