编辑: 这不是Icons in TabControl C# - How?的副本。这里的问题是关于向选项卡页面添加图标。这是关于如何将错误提供程序错误图标的位置更改为标题内而不是选项卡页面本身右侧的位置。另外,错误提供程序错误图标具有的功能是,当您将鼠标悬停在其上时,会看到错误文本,如果您只是在标题中添加图标,就看不到错误文本。
我有一个带有TabControl
的表格。该表单还有一个ErrorProvider
。当我尝试使用以下代码时:
errorProvider1.SetError(tabPage1, "error");
错误图标显示在选项卡页面的右侧,由选项卡控件本身将其切除:
我希望图标显示在选项卡页标题的旁边。像这样的东西(用Photoshop制作):
我不知道从哪里开始,或者如何解决这个问题。
编辑:
我有一个类负责向控件添加错误,并使用错误提供程序显示它们。此类用于TextBox
,NumericUpDown
等。我也想将其用于TabPages
。问题是,当我将其用于选项卡页面时,会得到上面显示的结果。使用ImageList
向标题添加错误图标,然后添加工具提示的技巧不好,因为它特定于选项卡页面,而且我无法在所有控件通用的类中实现它。因此,我确实需要更改标签页的设置,以便在使用errorProvider.SetError(...)
时将其显示在标题中。
答案 0 :(得分:1)
您可以执行以下操作。
Rectangle rc = tabControl1.GetTabRect(0); // Replace with the index of Tab required
errorProvider1.SetIconPadding(tabControl1, -rc.Left-20);;
errorProvider1.SetError(tabControl1, "Error String");
您还需要设置
errorProvider1.SetIconAlignment(tabControl1, ErrorIconAlignment.TopLeft);
样本(已选择第二个选项卡-基于注释)
您需要在TabPage Text前面添加空格,以确保有足够的空间来显示图标
在第二个标签上带有图标
答案 1 :(得分:1)
ErrorProvider
shows error icon of the TabPage
in tab page's client area.
By playing with IconAlignment
or IconPadding
, you can show error icon of TabControl
on one of the tab pages' headers, but it's error icon for the whole TabControl
.
In a real application each of the tab pages can contain controls which are not valid and you may want to show the validation icon on tab pages not for the tab control.
My suggestion is using tab page icon by setting ImageList
containing the error icon as image list of the TabControl
and by setting ImageIndex
of the TabPage
, show or hide the image icon. This way you can show the error icon for every tab page which needs it:
Example
To setup the example, follow these steps:
Form
.TabControl
, an ErrorProvider
and an ImageList
on the Form
.ImageList
property of tabControl1
to imageList1
.TextBox
on tabPage1
.I assume, just for example, you are going to validate these two text box controls using Validating
event. The key point is here. When you validate any control, check if it's hosted in a TabPage
, check validity of all children of TabPage
and set the error icon based on that:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.AutoValidate = AutoValidate.EnableAllowFocusChange;
imageList1.ColorDepth = ColorDepth.Depth32Bit;
imageList1.Images.Add(errorProvider1.Icon);
tabControl1.ImageList = imageList1;
textBox1.Validating += textBox_Validating;
textBox2.Validating += textBox_Validating;
}
private void textBox_Validating(object sender, CancelEventArgs e)
{
var textBox = (TextBox)sender;
if (string.IsNullOrEmpty(textBox.Text))
{
this.errorProvider1.SetError(textBox, "Value is required.");
e.Cancel = true;
}
else
this.errorProvider1.SetError(textBox, null);
var tabPage = textBox.Parent as TabPage;
if (tabPage != null)
ValidateTabPage(tabPage);
}
void ValidateTabPage(TabPage tabPage)
{
var tabIsValid = tabPage.Controls.Cast<Control>()
.All(x => string.IsNullOrEmpty(errorProvider1.GetError(x)));
if (tabIsValid)
tabPage.ImageIndex = -1;
else
tabPage.ImageIndex = 0;
}