Announcement

Collapse
No announcement yet.

เรียนลัด Visual C# 2008 เบื้องต้น

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • เรียนลัด Visual C# 2008 เบื้องต้น

    ใครที่มีพื้นฐาน VB.Net มาบ้างแล้ว การเรียนรู้ Visual C# ไม่ใช่เรื่องยาก ผมเพียงแต่แนะนำรูปแบบของ Visual C# ให้เท่านั้น
    ให้ดาวน์โหลด Visual C# มาติดตั้งลงเครื่องก่อนเลย จะซื้อแผ่นมาติดตั้งเอง หรือ จะใช้บริการจากเว็บ Microsoft ก็ได้ ..ตามลิ๊งค์ที่ให้
    LinkDownload: http://www.microsoft.com/express/vcsharp/ (ใช้ฟรี 1 เดือน)
    ---------------------------------------------------------
    เว็บบริการแปลงโค๊ดภาษา C# เป็น VB.Net..Click here

    Last edited by sak2005; 1 Dec 2009, 22:37:12.

  • #2
    รูปแบบโครงสร้างสคริ๊ปคำสั่งภาษา C#
    ถ้าใครเคยเขียนสคริ๊ปคำสั่งภาษา JAVA หรือ JScript จะเห็นได้ว่า รูปแบบสคริ๊ปคำสั่ง เกือบเหมือนหรือคล้ายคลึงกันมาก
    ฉนั้นใครเรียนรู้เกี่ยวกับ JScript และเขียนเป็นแล้ว ย่อมทำความเข้าใจกับ C# ได้ไม่ยาก
    การใช้งานโปรแกรม Visual C# ใช้หลักการเดียวกันกับ Visual VB.NET ทุกประการ
    -------------------------------------------------------------------
    ประเดิมด้วยคำสั่งง่ายๆ "Hello world!" MessageBox พร้อมคำอธิบายเกี่ยวกับ รูปแบบคำสั่ง

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello World!", ""); 
            }
        }
    }
    -------------------------------------------------------------

    ---------------------------------------------------------------
    จะสังเกตุได้ว่า..ในคำสั่งจะประกอบไปด้วยเครื่องหมาย ปีกกา อยู่หลายปีก ..มีทั้ง ปีกกาปิด และ ปีกกาเปิด
    เวลาเขียนต้องใส่ให้ครบ ขาดไม่ได้แม้แต่ปีกเดียว ..เพราะว่า ปีกทุกปีก มีความหมาย
    และเครื่องหมาย ; (semi colon) ก็ต้องใส่ไว้ท้ายข้อความที่เป็นคำสั่ง Statement ทุกๆประโยค
    ยกเว้นคำสั่งที่เป็น Event และ Public
    ----------------------------------------------------------------
    วิธีสังเกตุว่า..ใส่ปีกกาได้ครบและถูกต้องหรือไม่?
    ปีกกา Class ประกอบด้วย ปีกกาเปิด (ปีกแรก) และ ปีกกาปิด (ปีกสุดท้าย)
    ปีกกา Object ประกอบด้วย ปีกกาเปิด (ปีกที่2) และ ปีกกาปิด (ปีกที่7)
    ปีกกา Statement ประกอบด้วย ปีกกา (ปีกที่3-4) และ ปีกกา (ปีกที่5-6)
    Last edited by sak2005; 2 Dec 2009, 10:50:26.

    Comment


    • #3
      ชีวิตผมเริ่มต้นที่ java แล้วมาจบลงที่ visual C# 2005,2008,.... ยังมีต่ออีก

      Comment


      • #4
        ชีวิตผมเกิดจาก C โตมา VB ทำงานจะอะไรวะเนี่ย

        Comment


        • #5
          กำลังเรียนอยู่เลย....

          ถ้ายังไง มีเป็นแบบ Console มาสอนบ้างจะดีมากเลย...

          ไม่เก่งเรื่อง Array เอามาก ๆ ยัง งง ๆ อยู่

          ส่วนเรื่องอื่น ๆ เดี๋ยวจะมาช่วยเพิ่มนะครับ เป็นแบบ Console นะครับ...

          อิอิ

          Comment


          • #6
            คำสั่งพื้นฐาน
            การเขียนคำสั่งทุกคำสั่งมักจะอ้าอิงมาจากคำสั่งที่เรียกว่า.. namespace
            จะเขียนแบบเต็มคำสั่ง หรือใช้คำสั่ง.. using System เพื่ออ้างอิง namespace ก็ได้
            ดังตัวอย่าง2ตัวอย่างต่อไปนี้ ทำงานได้ผลเหมือนกัน
            -----------------------------------------------------------------
            ตัวอย่างที่1 คำสั่ง: สร้างโฟลเดอร์ใหม่ (แบบเต็มคำสั่ง)
            กรณีย์นี้สั่งสร้างโฟลเดอร์ ชื่อ newfolder ..สร้างแล้วเก็บไว้ในไดร์ C:\

            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            
            namespace WindowsFormsApplication1
            {
                public partial class Form1 : Form
                {
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                    private void button1_Click(object sender, EventArgs e)
                    {
                        System.IO.Directory.CreateDirectory("C:\\newfolder");   //คำสั่งแบบเต็ม
                    }
                }
            }
            ----------------------------------------------------------------------------
            ตัวอย่างที่2 ใช้คำสั่ง using System(อ้างอิง) namespace

            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using System.IO;    //<----------------- อ้างอิงตรงนี้
            namespace WindowsFormsApplication1
            {
                public partial class Form1 : Form
                {
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                    private void button1_Click(object sender, EventArgs e)
                    {
                        
                        Directory.CreateDirectory("C:\\newfolder");    //namespace ถูกย้ายขึ้นข้างบน
            
                    }
                }
            }
            Last edited by sak2005; 1 Dec 2009, 22:38:51.

            Comment


            • #7
              โค๊ดคำสั่ง: ก๊อบปี้โฟลเดอร์
              ใน C# ยังไม่มีสคริ๊ปคำสั่งสำหรับ Copy Directory (Folder) โดยตรง ต้องใช้การเขียนเป็นชุดโค๊ดขึ้นมาใช้งาน
              ยากหน่อยนะครับ.. แต่ก็ไม่เกินการทำความเข้าใจ
              ---------------------------------------
              กรณีย์นี้สั่งสร้างโฟลเดอร์ขึ้นมาใหม่ ชื่อ NewFolder ..สร้างแล้วเก็บไว้ในไดร์ฟ C:\
              จากนั้นก็สั่งก๊อบปี้โฟลเดอร์นี้ ..ก๊อบปี้แล้ว วางไว้บนเดสก์ทอป

              Code:
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Drawing;
              using System.Linq;
              using System.Text;
              using System.IO;
              using System.Windows.Forms;
              
              namespace btn
              {
                  public partial class Form1 : Form
                  {
                      public Form1()
                      {
                          InitializeComponent();
                      }
              
                      private void button1_Click(object sender, EventArgs e)
                      {
                          Directory.CreateDirectory("C:\\NewFolder");
                          DirectoryInfo sourceDir = new DirectoryInfo("C:\\NewFolder");
                          DirectoryInfo destinationDir = new DirectoryInfo("C:\\Documents and Settings\\Administrator\\Desktop\\NewFolder");
                          CopyDirectory(sourceDir, destinationDir);
                  }
              
                  static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
                  {
                      if (!destination.Exists)
                      {
                          destination.Create();
                      }
              
                      // Copy all files.
                      FileInfo[] files = source.GetFiles();
                      foreach (FileInfo file in files)
                      {
                          file.CopyTo(Path.Combine(destination.FullName, 
                              file.Name));
                      }
              
                      // Process subdirectories.
                      DirectoryInfo[] dirs = source.GetDirectories();
                      foreach (DirectoryInfo dir in dirs)
                      {
                          // Get destination directory.
                          string destinationDir = Path.Combine(destination.FullName, dir.Name);
              
                          // Call CopyDirectory() recursively.
                          CopyDirectory(dir, new DirectoryInfo(destinationDir));
                      }
                  }
                }
              }

              Comment


              • #8
                พอดีตอนนี้ผมกำลังสนใจเรื่อง Thread บน C++ และ C# ไม่ทราบมีหนังสือแนะนำไหมครับ

                ขอแบบเริ่มต้นเลยก็จะดีครับ เพราะเขียนไม่เป็นเลย

                เรียนมา 6 ปี ไม่เคยได้เขียน Thread ซักครั้ง เศร้าจิตจริงๆ

                ขอบคุณครับ

                Comment


                • #9
                  Originally posted by AmaKatsu View Post
                  พอดีตอนนี้ผมกำลังสนใจเรื่อง Thread บน C++ และ C# ไม่ทราบมีหนังสือแนะนำไหมครับ

                  ขอแบบเริ่มต้นเลยก็จะดีครับ เพราะเขียนไม่เป็นเลย

                  เรียนมา 6 ปี ไม่เคยได้เขียน Thread ซักครั้ง เศร้าจิตจริงๆ

                  ขอบคุณครับ
                  มีครับ.. ในร้านหนังสือตามห้างใหญ่ๆทั่วไป ลองไปเลือกอ่าน เลือกซื้อดูครับ
                  หนังสือราคาเล่มละประมาณ 200 กว่าบาท

                  Comment


                  • #10
                    คำสั่ง:ลบโฟลเดอร์
                    กรณีย์นี้สั่งลบโฟลเดอร์ ชื่อ NewFolder ที่อยู่บนเดสก์ทอป

                    Code:
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.Data;
                    using System.Drawing;
                    using System.Linq;
                    using System.Text;
                    using System.IO;
                    using System.Windows.Forms;
                    
                    namespace WindowsFormsApplication10
                    {
                        public partial class Form1 : Form
                        {
                            public Form1()
                            {
                                InitializeComponent();
                            }
                    
                            private void button1_Click(object sender, EventArgs e)
                    
                            {
                            Directory.Delete("C:\\Documents and Settings\\Administrator\\Desktop\\NewFolder", true);
                    
                            }
                        }
                    }

                    Comment


                    • #11
                      คำสั่ง: ย้ายโฟลเดอร์
                      กรณีย์นี้สั่งสร้างโฟลเดอร์ ชื่อ MyFolder ..สร้างแล้ววางไว้บนเดสก์ทอป
                      จากนั้นอีกประมาณ 10 วินาที โฟลเดอร์จะถูกย้ายไปเก็บไว้ที่ไดร์ฟ C:\

                      Code:
                      using System;
                      using System.Collections.Generic;
                      using System.ComponentModel;
                      using System.Data;
                      using System.Drawing;
                      using System.Linq;
                      using System.Text;
                      using System.IO;
                      using System.Threading;
                      using System.Windows.Forms;
                      
                      namespace WindowsFormsApplication10
                      {
                          public partial class Form1 : Form
                          {
                              public Form1()
                              {
                                  InitializeComponent();
                              }
                      
                              private void button1_Click(object sender, EventArgs e)
                      
                              {
                              Directory.CreateDirectory("C:\\Documents and Settings\\Administrator\\Desktop\\MyFolder");
                              Thread.Sleep(10000);   //ใช้ using ..namespace
                              Directory.Move("C:\\Documents and Settings\\Administrator\\Desktop\\MyFolder", "C:\\MyFolder");
                              }
                          }
                      }

                      Comment


                      • #12
                        เรียนรู้เกี่ยวกับการจัดการโฟลเดอร์กันไปพอสมควรแล้ว
                        ทีนี้เรามาเรียนรู้เกี่ยวกับการจัดการไฟล์กันบ้าง...
                        ----------------------------------------
                        คำสั่ง:สร้างไฟล์ใหม่
                        กรณีย์นี้สั่งสร้างไฟล์ 3 ไฟล์ ..ชื่อ new มีสกุลเป็น .rar , .bmp และ .doc
                        สร้างแล้ววางไว้บนเดสก์ทอป

                        Code:
                        using System;
                        using System.Collections.Generic;
                        using System.ComponentModel;
                        using System.Data;
                        using System.Drawing;
                        using System.Linq;
                        using System.Text;
                        using System.IO;
                        using System.Threading; 
                        using System.Windows.Forms;
                        
                        namespace test
                        {
                            public partial class Form1 : Form
                            {
                                public Form1()
                                {
                                    InitializeComponent();
                                }
                        
                                private void button1_Click(object sender, EventArgs e)
                                {
                                File.Create("C:\\Documents and Settings\\Administrator\\Desktop\\new.rar");
                                Thread.Sleep(1000);
                                File.Create("C:\\Documents and Settings\\Administrator\\Desktop\\new.bmp");
                                Thread.Sleep(1000);
                                File.Create("C:\\Documents and Settings\\Administrator\\Desktop\\new.doc");
                                }
                            }
                        }
                        Last edited by sak2005; 2 Dec 2009, 11:48:41.

                        Comment


                        • #13
                          คำสั่ง: ก๊อบปี้ไฟล์
                          กรณีย์นี้สั่งก๊อบปี้ไฟล์ ชื่อ new.bmp ที่วางอยู่บนเดสก์ทอป
                          ก๊อบปี้แล้ววางไว้ในไดร์ฟ C:\

                          Code:
                          using System;
                          using System.Collections.Generic;
                          using System.ComponentModel;
                          using System.Data;
                          using System.Drawing;
                          using System.Linq;
                          using System.Text;
                          using System.IO;    //namespace of File Copy
                          using System.Windows.Forms;
                          
                          namespace test
                          {
                              public partial class Form1 : Form
                              {
                                  public Form1()
                                  {
                                      InitializeComponent();
                                  }
                          
                                  private void button1_Click(object sender, EventArgs e)
                                  {
                                      File.Copy("C:\\Documents and Settings\\Administrator\\Desktop\\new.bmp", "C:\\new.bmp", true);
                                  }
                              }
                          }
                          Last edited by sak2005; 2 Dec 2009, 12:32:33.

                          Comment


                          • #14
                            การใช้งาน Common Controls Components (ToolBox) และการเขียนคำสั่งควบคุม
                            -------------------------------------------------------------------
                            คำสั่ง: เลือกเปิดโฟลเดอร์ที่ต้องการ (FolderBrowseDialog)

                            Code:
                            using System;
                            using System.Collections.Generic;
                            using System.ComponentModel;
                            using System.Data;
                            using System.Drawing;
                            using System.Linq;
                            using System.Text;
                            using System.Diagnostics;   //namespace of Process.Start
                            using System.Windows.Forms;
                            
                            namespace WindowsFormsApplication1
                            {
                                public partial class Form1 : Form
                                {
                                    public Form1()
                                    {
                                        InitializeComponent();
                                    }
                            
                                    private void button1_Click(object sender, EventArgs e)
                                    {
                                        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                                        {
                                            textBox1.Text = folderBrowserDialog1.SelectedPath;
                                        }
                                    }
                            
                                    private void button2_Click(object sender, EventArgs e)
                                    {
                                        if (textBox1.Text == folderBrowserDialog1.SelectedPath)
                                        {
                                           Process.Start(folderBrowserDialog1.SelectedPath);  //namespace is System.Diagnostics
                                        }
                                    }
                            
                                    private void Form1_Load(object sender, EventArgs e)
                                    {
                            
                                    }
                                }
                            }


                            Download: Sample Project
                            http://upload.one2car.com/download.a...AQ4DPBCIEX2LJS

                            Comment


                            • #15
                              Originally posted by sak2005 View Post
                              รูปแบบโครงสร้างสคริ๊ปคำสั่งภาษา C#
                              ถ้าใครเคยเขียนสคริ๊ปคำสั่งภาษา JAVA หรือ JScript จะเห็นได้ว่า รูปแบบสคริ๊ปคำสั่ง เกือบเหมือนหรือคล้ายคลึงกันมาก
                              ฉนั้นใครเรียนรู้เกี่ยวกับ JScript และเขียนเป็นแล้ว ย่อมทำความเข้าใจกับ C# ได้ไม่ยาก
                              การใช้งานโปรแกรม Visual C# ใช้หลักการเดียวกันกับ Visual VB.NET ทุกประการ
                              -------------------------------------------------------------------
                              ประเดิมด้วยคำสั่งง่ายๆ "Hello world!" MessageBox พร้อมคำอธิบายเกี่ยวกับ รูปแบบคำสั่ง

                              Code:
                              using System;
                              using System.Collections.Generic;
                              using System.ComponentModel;
                              using System.Data;
                              using System.Drawing;
                              using System.Linq;
                              using System.Text;
                              using System.Windows.Forms;
                              
                              namespace WindowsFormsApplication1
                              {
                                  public partial class Form1 : Form
                                  {
                                      public Form1()
                                      {
                                          InitializeComponent();
                                      }
                              
                                      private void button1_Click(object sender, EventArgs e)
                                      {
                                          MessageBox.Show("Hello World!", ""); 
                                      }
                                  }
                              }
                              -------------------------------------------------------------

                              ---------------------------------------------------------------
                              จะสังเกตุได้ว่า..ในคำสั่งจะประกอบไปด้วยเครื่องหมาย ปีกกา อยู่หลายปีก ..มีทั้ง ปีกกาปิด และ ปีกกาเปิด
                              เวลาเขียนต้องใส่ให้ครบ ขาดไม่ได้แม้แต่ปีกเดียว ..เพราะว่า ปีกทุกปีก มีความหมาย
                              และเครื่องหมาย ; (semi colon) ก็ต้องใส่ไว้ท้ายข้อความที่เป็นคำสั่ง Statement ทุกๆประโยค
                              ยกเว้นคำสั่งที่เป็น Event และ Public
                              ----------------------------------------------------------------
                              วิธีสังเกตุว่า..ใส่ปีกกาได้ครบและถูกต้องหรือไม่?
                              ปีกกา Class ประกอบด้วย ปีกกาเปิด (ปีกแรก) และ ปีกกาปิด (ปีกสุดท้าย)
                              ปีกกา Object ประกอบด้วย ปีกกาเปิด (ปีกที่2) และ ปีกกาปิด (ปีกที่7)
                              ปีกกา Statement ประกอบด้วย ปีกกา (ปีกที่3-4) และ ปีกกา (ปีกที่5-6)
                              อิอิ hello world เป็นโปรแกรมแรกที่ผมเขียน

                              Comment

                              Working...
                              X