Accessing Form control values in C#.Net


C#.net programs are completely build upon class hierarchy, unlike VB.Net it is a full fledged OOP language.

The first problem newbie faced  in C#.Net while migrating from VB.Net was the global accessing of controls in other classes. In C# every Windows Form is a partial class derived from Form. This problem can be solved by creating properties with Get and Set method.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PharmaERP
{
    public partial class Product_Registration : Form
    {
        public Product_Registration()
        {
            InitializeComponent();
        }
        public string  _txtPcode
        {
            get
            {
                return txt_Pcode.Text;
            }
                
            set
            {
                txt_Pcode.Text  = value;
            } 
        }

Once you are created the object you can Set or Get values from the control as follows.
Declare the form Object in general section of a Form class,where you would like to access the controls.

public Product_Registration frm_PR = new Product_Registration(); frm_PR._txtPcode=”S706″;

will set the Text Box with “S706” string using the Set method You can also get the string from control using the Get as,

Messagebox.show(frm_PR._txtPcode)
or
 Messagebox.show(this._txtPcode) — from the Form itself

<<<That is all I have today…. please leave comments on Disqus>>>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.