TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Tag Property: Windows Forms

Tag. The Tag property stores an object reference. Windows Forms programs can use object models of arbitrary complexity. But the Tag property is a simple way to link a certain object to a certain control. It is useful in certain situations.Property
This program shows the use of the Tag property inside the object model of the Windows Forms program. The Form1 constructor, which instantiates the control, assigns the Tag property to a new ExampleTag—a custom type.

Then: This object reference can be accessed at any time and in any method later in the event loop of the Windows program.

Form1: In the Form1 constructor, the ExampleTag constructor is called. It returns an ExampleTag reference, which is copied to the Tag reference field.

Next: In the Form1_Load event handler, we see that you can access the Tag property on the enclosing control.

And: This is the same Tag that we previously set. We could also modify the ExampleTag object at this point.

C# program that uses Tag property using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Set tag on constructor. this.Tag = new ExampleTag(1, 2); } private void Form1_Load(object sender, EventArgs e) { // When form loads, render tag to the title bar. this.Text = this.Tag.ToString(); } } class ExampleTag { public int _a; public int _b; public ExampleTag(int a, int b) { // Store the fields. this._a = a; this._b = b; } public override string ToString() { // Write the fields to a string. return string.Format("Tag a = {0}, b = {1}", this._a, this._b); } } } Output The window is displayed with the title bar reading the tag ToString output.
The Form1_Load event accesses the Tag reference as the base type object. However, when you invoke the ToString method upon a base class, the most derived method is actually called, typically through a matrix data structure.ToString

Thus: You can see that the title bar of the program is equal to the result of the ToString method.

When planning a program, the object model is an important part of the program's architecture. If you use the Tag property too much, you will conflate the user interface specific information in the program with the data model.

Caution: This makes portability of your program to new interfaces more difficult.

Summary. We explored an example usage of the Tag property in the Windows Forms widget layout system. The Tag property essentially provides a user-defined field that can store any form of object in a persistent way.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf