Saturday, April 5, 2014

Change Property Value at Run Tiime

Change Property Value at Run Time
  • You can change the property values at run time (programmatically).
  • For  example,  you  have  designed  a  button  with  blue  background.  Later,  after  the  user clicks it, you want to display that button with green background.
  • Then you require to change the BackColor property value at run time.
  • Syntax:
  • controlname.property = value;
  • Ex:
  • button1.Text = Button is clicked
  • But  sometimes,  when  you  are  trying  to  change  some  property  values,  you  may  face some problems.
  • For example, the following statements are not valid statements:
              button1.BackColor = green;
              button1.Cursor = hand;
              button1.TextAlign = TopLeft;
  • So, while you are changing the property values, you remember and follow the following syntaxes:

Assign Property Values at Run Time
Property STATEMENT TO ASSIGN THE VALUE AT RUN TIME
Name Not possible to change at run time
Text controlname.Text = "xxxxx";
Enabled controlname.Enabled = true / false;
Visible controlname.Visible = true / false;
Location controlname.Location = new System.Drawing.Point(x, y);
Size controlname.Size = new System.Drawing.Size(width, height);
Font controlname.Font = new System.Drawing.Font("font name", fontsize);
BackColor controlname.BackColor = System.Drawing.Color.xxxxx;
ForeColor controlname.ForeColor = System.Drawing.Color.xxxxx;
Cursor controlname.Cursor = System.Windows.Forms.Cursors.xxxxx;
Image controlname.Image = System.Drawing.Image.FromFile("image file path");
TextAlign controlname.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
ImageAlign controlname.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
TabIndex controlname.TabIndex = n;
ContextMenuStrip controlname.ContextMenuStrip = xxxxx;
Demo on Changing Property Values at Run Time
private void button2_Click(object sender, EventArgs e)
 {
  button1.Text = "My Test Button";
 }
 private void button3_Click(object sender, EventArgs e)
 {
  button1.Enabled = false;
 }
 private void button4_Click(object sender, EventArgs e)
 {
  button1.Enabled = true;
 }
 private void button5_Click(object sender, EventArgs e)
 {
  button1.Visible = false;
 }
 private void button6_Click(object sender, EventArgs e)
 {
  button1.Visible = true;
 }
 private void button7_Click(object sender, EventArgs e)
 {
  button1.Location = new Point(150, 400);
 }
 private void button8_Click(object sender, EventArgs e)
 {
  button1.Size = new Size(250, 100);
 }
 private void button9_Click(object sender, EventArgs e)
 {
  button1.Font = new Font("Showcard Gothic", 17);
 }
 private void button10_Click(object sender, EventArgs e)
 {
  button1.BackColor = Color.LightCoral;
 }
 private void button11_Click(object sender, EventArgs e)
 {
  button1.ForeColor = Color.Green;
 }
 private void button12_Click(object sender, EventArgs e)
 {
  button1.Image = Image.FromFile("c:\\flagindia.gif");
  button1.ImageAlign = ContentAlignment.MiddleLeft;
  button1.TextAlign = ContentAlignment.MiddleRight;
  button1.Size = new Size(200, 60);
 }