English 中文(简体)
全球目标还是不是?
原标题:Global objects or not?

我有“WinForms”和多种表格,希望使用ErrorProvider关于其中每一部分的内容EDIT:,以检查用户投入是否为“OK”(例如,输入号码在幅度之内......) 在我看来,把这一构成部分从各个形式上删除似乎毫无用处。 如果我做一个全球目标(或如何称呼)并以每一种形式使用?

我的想法:

namespace MyApplication {  
static class Program {  

public static ErrorProvider EP = new ErrorProvider();
...

And then in that individual form to handle Validating and Validated events:

private void txtBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
  if (txtBox1.Text != "correct text") {
    e.Cancel = true;
    Program.EP.SetError(txtBox1, "You have error in your input");
...

这种做法是正确的,还是我做些什么?

And if I need more global objects, maybe I should put them all together to some separate static class and in the Program create just this one (?)
Thanks.

最佳回答

I think you can create a new class that inherits textbox class and add your validations there, then create a new control which is your class and use it where ever you need, in this case you will edit/modify your validations in one place later on.

f 您的认证非常不同,例如: NumericTextBox和StingTextBox

问题回答

This approach is valid as long as your individual forms are never running concurrently or your error provider is re-entrant, and your error provider does not need a permanent link back to your forms. From the example that you show it appears that your provider requires you to pass the control on which to set the error state, so it has a good chance of being re-entrant.

object有全球目标,但是,如果可能,我将把它变成一个静态的无国籍阶层。 无国籍状态将便于阅读的安全。 整个网络都有全球(以名称为空间)固定类别,例如<代码>FileInfo或Convert

If you only need a few global methods and they operate on UI elements you can also write some extension methods for System.Windows.Controls.Control or even System.Windows.Controls.TextBox if that s the only control you want. This will give you a bit nicer code:

txtBox1.Validate("correct text", "You have error in your input");

推广方法可以这样考虑:

public static class MyExtension
{
    public static void Validate(this TextBox myTextBox, string correctText, string error)
    {
        if(myTextBox.Text != correctText)
            Console.WriteLine(string.Format("{0} [{1} ==  {2} ]", error, myTextBox.Name, myTextBox.Text));
    }
}

Which would give you this message in the console:

<编码> 页: 1

http://msdn.microsoft.com/en-us/library/bb383977.aspx” rel=“nofollow” 如果你需要更多信息。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签