Post: [C# Tutorial 3] Using Code To Change Form Properties
05-11-2012, 02:29 AM #1
Pichu
RIP PICHU.
(adsbygoogle = window.adsbygoogle || []).push({}); C# Tutorial 3 -Using Code To Change Form Properties's

With this tutorial, you will learn how to use codes in order to change the values of the form's properties. This is sueful for cutting down the need of having many forms, buttons, ect…
An example of chaning properties through code would be the use of a start/stop button. Rather than having 2 separate buttons, you could change the properties of the one button to say Start or Stop. By using If Statements, you could hold the power of 2 buttons in a single button. You will learn later what If Statements are, so don't worry if you are lost.

Let's get you started by creating a new project. Go ahead and name this whatever you like.

In the previous tutorial, you learend how to view the code of the current form. You may do this by either:


  • Double clicking form
  • Go to View > Code
  • Pressing F7



Go back to Form1.cs[Design] and in the Toolbox, find the following items:


  • Button
  • TextBox



Click on the button and two buttons. Do the same with the Textboxes.

You must login or register to view this content.

Now, double click on Button1 and Button2, just like how you would with the Form to view the code source.

You must login or register to view this content.

Now for Button1, insert –
    textBox1.Text = "Hello";


For Button2, insert –

    textBox2.Text = "Button 2 Did This!";


It should now look like this:


You must login or register to view this content.

textBox1 is the name of the object you are dealing with.
.Text is defining the value in which we are invoking.
= is what we are using to set an object to.
"" surrounds text so it lets the program know you are dealing with a string.
; is to end a line of code.

*Note: = sets while == compares

Now, compile the application. You will notice that TextBox1 and TextBox2 are still blank. Click Button1 and watch the magic of programming occur right before your eyes.

Go ahead and click Button2.

Your form should look like this.

You must login or register to view this content.

Now, let's get crazy here.

Stop the debug session and add to button1's code the following:


    this.Text = "TechPlus is awesome";
    
[COLOR=#111111][FONT=verdana]
this.Size = new Size(500, 340);[/FONT][/COLOR]


Add to button2's code:


    this.BackColor = Color.Red;


Debug and press button1 and button2, notice how the Form has just changed!

You must login or register to view this content.

You must login or register to view this content.


You can play around and try different things with the other tools.

Originally posted by another user
This is taken off from my website, the website is free and is designed only for tutorials. I will posting tutorials I create for my site here as well. Link is in my signature.[/QUOTE]
Last edited by Pichu ; 05-11-2012 at 02:33 AM.

The following user thanked Pichu for this useful post:

caleb01

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo