Edit:
This is not a duplicate of Icons in TabControl C# – How?. The question there is about adding icons to tab pages. Here it is about how the change the error provider error icon position to be inside the header instead of to the right of the tab page itself. Also, the error provider error icon has the functionality that when you hover the mouse on it, you see the error text, which you do not see if you simply add an icon to the header.
I have a form with a TabControl
. The form has also an ErrorProvider
. When I try to use the following code:
errorProvider1.SetError(tabPage1, "error");
The error icon is shown to the right of the tab page, and it is cut-off by the tab control itself:
I would like the icon to be shown next to the tab page header. Something like this (made with Photoshop):
I do not know where to start, or how to approach this.
Edit:
I have a class responsible for adding errors to a control, and showing them using an error provider. This class is used for TextBox
es, NumericUpDown
s etc. I would like to use it also for TabPages
. The problem is that when I use it for tab pages I get the result shown above. The trick of adding an error icon to the header using an ImageList
and then add a tooltip is not good, because it is specific to tab pages, and I cannot implement it in my class which is general to all controls. So I really need to change the settings of the tab page so when I use errorProvider.SetError(...)
it is shown in the header.
2
Answers
You can do the following.
You also need to set
Sample (With Second Tab Selected – based on comments),
You would need to prepend whitespace to your TabPage Text to ensure there is sufficient space for showing the icon
With Icon on Second Tab
ErrorProvider
shows error icon of theTabPage
in tab page’s client area.By playing with
IconAlignment
orIconPadding
, you can show error icon ofTabControl
on one of the tab pages’ headers, but it’s error icon for the wholeTabControl
.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 theTabControl
and by settingImageIndex
of theTabPage
, 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
, anErrorProvider
and anImageList
on theForm
.ImageList
property oftabControl1
toimageList1
.TextBox
ontabPage1
.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 aTabPage
, check validity of all children ofTabPage
and set the error icon based on that: