(设计)2个元素相互阻挡
(design) 2 Elements blocking each other
这里是简单的问题:
我的其中一个标签有问题。
我的问题是与文本框“Datei Name”对应的 (!!!) 标签被所述文本框阻止了。是否有我错过的设置,将所述标签 behind/in 放在文本框前面?
程序(的这一部分)的工作原理:
当在其中一个文本框中输入错误时,会出现一个包含 (!!!) 的标签和一个消息框,告诉用户错误所在。
正如我在问题的评论中提到的,我会使用 ErrorProvider 而不是花时间编写代码来操作表单上的控件。
这是 MSDN 示例:How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component
它是如何工作的?
当表单无效时,您将看到:
当您将光标移到图标上时,您会看到错误详细信息:
如果你有 LinqPad,你可以使用这个脚本来检查它是如何工作的:
void Main()
{
MyForm mf = new MyForm();
mf.Show();
}
// Define other methods and classes here
public class MyForm: Form
{
private TextBox TxtDateiName = null;
private TextBox TxtDateiNr = null;
private Label Label1 = null;
private Label Label2 = null;
private Button BtnValidate = null;
Dictionary<Control, ErrorProvider> ValidatedControls = null;
public MyForm()
{
Initialize();
}
private void Initialize()
{
this.Size = new Size(250, 180);
this.MinimizeBox = false;
this.MaximizeBox = false;
this.Text = "ErrorProvider Example";
Label1 = new Label(){Text="Datei Name:", Location =new Point(10,12), AutoSize = true};
Label2 = new Label(){Text="Datei Nr:", Location =new Point(10,42), AutoSize = true};
TxtDateiName = new TextBox(){Name="TxtDateiName", Size= new Size(140,24), Location =new Point(78,10)};
TxtDateiNr = new TextBox(){Name="TxtDateiNr", Size= new Size(140,24), Location =new Point(78,42)};
BtnValidate = new Button(){Size = new Size(220, 48), Text = "Validate", Location = new Point(10, 78) };
BtnValidate.Click += BtnValidate_Click;
this.Controls.Add(Label1);
this.Controls.Add(Label2);
this.Controls.Add(TxtDateiName);
this.Controls.Add(TxtDateiNr);
this.Controls.Add(BtnValidate);
ValidatedControls = new Dictionary<Control, ErrorProvider>();
ValidatedControls.Add(TxtDateiName, new ErrorProvider(this));
ValidatedControls.Add(TxtDateiNr, new ErrorProvider(this));
ValidatedControls[TxtDateiName].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiName].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiName].BlinkRate = 1000;
ValidatedControls[TxtDateiName].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
ValidatedControls[TxtDateiNr].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiNr].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiNr].BlinkRate = 1000;
ValidatedControls[TxtDateiNr].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
}
private void BtnValidate_Click(object sender, EventArgs e)
{
bool ans = true;
foreach(var k in ValidatedControls.Keys)
{
switch(k.Name)
{
case "TxtDateiName":
ans = IsValidName(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Name is wrong!");
break;
case "TxtDateiNr":
ans = IsValidNumber(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Number is wrong!");
break;
}
}
ans = ValidatedControls.All(kvp=>kvp.Value.GetError(kvp.Key)=="");
MessageBox.Show(ans ? "Form is valid!" : "Form contains errors!", "Information");
if(ans) this.Close();
}
private bool IsValidName(string dName)
{
return Regex.IsMatch(dName, @"^(\w{3,})$");
}
private bool IsValidNumber(string dNumber)
{
return Regex.IsMatch(dNumber, @"^(\d{3,5})$");
}
}
不要忘记添加引用 (F4) 到:System.Windows.Forms.dll
然后添加以下命名空间:
System.Drawing
System.Windows.Forms
最后说明:
这只是一个例子。所以,代码没有优化。
这里是简单的问题:
我的其中一个标签有问题。
我的问题是与文本框“Datei Name”对应的 (!!!) 标签被所述文本框阻止了。是否有我错过的设置,将所述标签 behind/in 放在文本框前面?
程序(的这一部分)的工作原理: 当在其中一个文本框中输入错误时,会出现一个包含 (!!!) 的标签和一个消息框,告诉用户错误所在。
正如我在问题的评论中提到的,我会使用 ErrorProvider 而不是花时间编写代码来操作表单上的控件。
这是 MSDN 示例:How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component
它是如何工作的?
当表单无效时,您将看到:
当您将光标移到图标上时,您会看到错误详细信息:
如果你有 LinqPad,你可以使用这个脚本来检查它是如何工作的:
void Main()
{
MyForm mf = new MyForm();
mf.Show();
}
// Define other methods and classes here
public class MyForm: Form
{
private TextBox TxtDateiName = null;
private TextBox TxtDateiNr = null;
private Label Label1 = null;
private Label Label2 = null;
private Button BtnValidate = null;
Dictionary<Control, ErrorProvider> ValidatedControls = null;
public MyForm()
{
Initialize();
}
private void Initialize()
{
this.Size = new Size(250, 180);
this.MinimizeBox = false;
this.MaximizeBox = false;
this.Text = "ErrorProvider Example";
Label1 = new Label(){Text="Datei Name:", Location =new Point(10,12), AutoSize = true};
Label2 = new Label(){Text="Datei Nr:", Location =new Point(10,42), AutoSize = true};
TxtDateiName = new TextBox(){Name="TxtDateiName", Size= new Size(140,24), Location =new Point(78,10)};
TxtDateiNr = new TextBox(){Name="TxtDateiNr", Size= new Size(140,24), Location =new Point(78,42)};
BtnValidate = new Button(){Size = new Size(220, 48), Text = "Validate", Location = new Point(10, 78) };
BtnValidate.Click += BtnValidate_Click;
this.Controls.Add(Label1);
this.Controls.Add(Label2);
this.Controls.Add(TxtDateiName);
this.Controls.Add(TxtDateiNr);
this.Controls.Add(BtnValidate);
ValidatedControls = new Dictionary<Control, ErrorProvider>();
ValidatedControls.Add(TxtDateiName, new ErrorProvider(this));
ValidatedControls.Add(TxtDateiNr, new ErrorProvider(this));
ValidatedControls[TxtDateiName].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiName].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiName].BlinkRate = 1000;
ValidatedControls[TxtDateiName].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
ValidatedControls[TxtDateiNr].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
ValidatedControls[TxtDateiNr].SetIconPadding (TxtDateiName, 2);
ValidatedControls[TxtDateiNr].BlinkRate = 1000;
ValidatedControls[TxtDateiNr].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
}
private void BtnValidate_Click(object sender, EventArgs e)
{
bool ans = true;
foreach(var k in ValidatedControls.Keys)
{
switch(k.Name)
{
case "TxtDateiName":
ans = IsValidName(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Name is wrong!");
break;
case "TxtDateiNr":
ans = IsValidNumber(((TextBox)k).Text);
ValidatedControls[k].SetError(k, ans ? "" : "Number is wrong!");
break;
}
}
ans = ValidatedControls.All(kvp=>kvp.Value.GetError(kvp.Key)=="");
MessageBox.Show(ans ? "Form is valid!" : "Form contains errors!", "Information");
if(ans) this.Close();
}
private bool IsValidName(string dName)
{
return Regex.IsMatch(dName, @"^(\w{3,})$");
}
private bool IsValidNumber(string dNumber)
{
return Regex.IsMatch(dNumber, @"^(\d{3,5})$");
}
}
不要忘记添加引用 (F4) 到:System.Windows.Forms.dll
然后添加以下命名空间:
System.Drawing
System.Windows.Forms
最后说明:
这只是一个例子。所以,代码没有优化。