Share This!

Thursday, November 15, 2007

Day 3: WinForms App

Less prose! More examples!

Start C# File - New Project.

Windows Forms Application.

This is what you should see.

Look in the toolbox for a class object called a button and another called a textbox. Do something like this:

Rightclick the textbox. Select properties.

Look now at the properties box.

Ok now a few things to note. Look at the speed buttons just below the word "textbox1". The first 2 icons pick whether you are sorting alphabetically or by groups. The next 2 icons determine whether you are viewing properties or methods.

Now find the multiline property of textbox1 and change it to true.

Note how the handles on the textbox have changed. Grab the lower right handle and stretch it like this.

ok, now double click button1 in the editor.


        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Written by CODE";
        }

Add the text in bold. Then hit the F5 key to start with debugging. After clicking the button, you'll see this.

More examples...

        private void button1_Click(object sender, EventArgs e)
        {
            //this is a comment, the compiler ignores it.
            /*
             * this kind of comment can span more
             * than one line
             */

            //putting text in the textbox
            textBox1.Text = "Written by CODE";

            //adding to the text that is already there.
            textBox1.Text += " because we cared.";
            
            //newline example 
            /*(Environment.NewLine ensures that 
             * the correct chars are used based 
             * on the operating environment)*/
            textBox1.Text += Environment.NewLine+"(new line)"; 

            //appendtext
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText("appended text");

            //casting non-strings
            textBox1.AppendText(Environment.NewLine);
            double j = 22.0 / 7.0;
            textBox1.AppendText(j.ToString("F04"));

            textBox1.AppendText(Environment.NewLine);
            int i = 1024;
            textBox1.AppendText(i.ToString());

            //results of methods
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(Environment.CommandLine);

            //math results
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(Math.Cos(0.1).ToString("F05"));

            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText((110.0/73.0).ToString("F05"));

            //dates
            textBox1.AppendText(Environment.NewLine);
            DateTime D = new DateTime(2007, 12, 31, 17, 00, 00);
            textBox1.AppendText(D.ToShortDateString());
            textBox1.AppendText(" "+D.ToShortTimeString());

            DateTime NOW = DateTime.Now;
            TimeSpan diff = D - NOW;
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(diff.Days.ToString()+" days " );
            textBox1.AppendText(diff.Hours.ToString() + " hours");
            textBox1.AppendText(diff.Minutes.ToString() + " minutes");

            //changing object properties
            this.BackColor = Color.AliceBlue;
            this.ForeColor = Color.Blue;

            /*
             * note how we can refer to the current form as "this"
             * 
             */
        }

No comments:

Post a Comment

Contact Us

Name

Email *

Message *