I am using below method to validate data within textbox
in groupbox
so for improvement of user Feedback for example
message text Please enter
First Name
where First Name is the label for textbox, so I used Textbox.Tag
to store the name of textbox
to achieve it since there is no link between the Textbox
and it is Label
I search and found that I can use the Tag
property to store anything but I wan to be sure of using it by the I told you about
Is there any problem with that ?
public int ValidateData()
{
foreach (Control cont in GB_PatientInfo.Controls)
{
if (cont is TextBox)
{
if (string.IsNullOrWhiteSpace(cont.Text.Trim()))
{
MessageBox.Show("enter data " + cont.Tag, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
cont.BackColor = Color.Red;
cont.Focus();
return -1;
}
}
}
return 1;
}
Thanks
如您所读,您可以在Control.Tag属性中存储从对象(即所有对象)派生的任何类型,因此可以存储标签的名称。
The use of the Tag property does not influence your application. You can store whatever you want in there with no problem.
As it's already mentioned in other answers, it's OK to use Tag
property to store any kind of additional information about the control, including a display name.
But have you ever noticed how ToolTip
lets you to set ToolTip
for your control at design-time?
A ToolTip
, ErrorProvider
or HelpProvider
are examples of extender provider components. They add some properties to the controls at design-time. You also can create such component for DisplayName
by implementing IExtenderProvider
.
Example
The following code shows you how easily you can create a component called DisplayNameExtender
. When you drop an instance of this component on the form, then a new property will be added to design-time of controls, you can set the value to the property at design-time: DisplayName on diaplayNameExtender1 .
Then at run-time, whenever you want to get the value of DisplayName
for a control, it's enough to find it this way:
var displayName = displayNameExtender1.GetDisplayName(control);
Here is the code for DisplayNameExtender
component:
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
[ProvideProperty("DisplayName", typeof(Control))]
public class DisplayNameExtender : Component, IExtenderProvider
{
private Hashtable displayNameValues = new Hashtable();
public bool CanExtend(object extendee)
{
return (extendee is Control && !(extendee is Form));
}
public string GetDisplayName(Control control)
{
if (displayNameValues.ContainsKey(control))
return (string)displayNameValues[control];
return null;
}
public void SetDisplayName(Control control, string value)
{
if (string.IsNullOrEmpty(value))
displayNameValues.Remove(control);
else
displayNameValues[control] = value;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.