Announcement

Collapse
No announcement yet.

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

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

  • #16
    คำสั่ง: เลือกไฟล์ที่ต้องการเปิด (openFileDialog)

    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 (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog1.FileName;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                    if (textBox1.Text == openFileDialog1.FileName)
                {
                    Process.Start(openFileDialog1.FileName);  //namespace is System.Diagnostics
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
            {
    
            }
        }
    }

    Comment


    • #17
      ของแถม! ..เป็นโปรแกรมที่ผมออกแบบไว้ให้ท่านศึกษา ใช้เวลาเขียนประมาณ 1 ช.ม
      Download: Auto Installer Project..Click here
      Size: 213 KB



      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;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  if (openFileDialog1.ShowDialog() == DialogResult.OK)
                  {
                      textBox1.Text = openFileDialog1.FileName;
                      comboBox1.Enabled = true;
                  }
              }
      
              private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
              {
                  textBox2.Text = comboBox1.Text;
                  button2.Enabled = true;
              }
      
              private void button2_Click(object sender, EventArgs e)
              
                  {
                      Process proc = new Process();  //สร้างเงื่อนไขให้คำสั่ง process
                      proc.StartInfo.FileName = textBox1.Text;
                      proc.StartInfo.Arguments = textBox2.Text;
                      proc.Start();
                      proc.WaitForExit();
                      MessageBox.Show ("Completed.", "", 0,MessageBoxIcon.Information);
                      textBox1.Text = "";
                      textBox2.Text = "";
                      comboBox1.Text = "";
                      comboBox1.Enabled = false;
                      button2.Enabled = false;
                  }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  comboBox1.Enabled = false;
                  button2.Enabled = false;
              }
      
              private void label3_Click(object sender, EventArgs e)
              {
                  Close();
              }
          }
      }
      Last edited by sak2005; 5 Dec 2009, 01:42:42.

      Comment


      • #18
        คำสั่ง: Control Loop ..ใช้ Timer (Common Control Component)
        ตัวอย่าง: ใช้ Timer ควบคุมการทำงานของ ProgressBar

        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 label2_Click(object sender, EventArgs e)
                {
        
                }
        
                private void button1_Click(object sender, EventArgs e)
                {
                    {
                        timer1.Enabled = true;
                    }
                }
                private void timer1_Tick(object sender, EventArgs e)
                {
                    progressBar1.Minimum = 0;
                    progressBar1.Maximum = 100;
                    progressBar1.Step = 1;
                    {
                        if (progressBar1.Value < 100)
                        {
                            progressBar1.Value += 1;
                            label2.Text = progressBar1.Value.ToString() + "%";
                        }
                        if (progressBar1.Value >= 100)
                        {
                            timer1.Enabled = false;
                            MessageBox.Show("Completed.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            progressBar1.Value = 0;
                           label2.Text = "0%";
                           
                        }
                    }
                }
            }
        }

        Comment


        • #19
          คำสั่ง: For Loop ..เขียนยากหน่อย แปลกดีไม่มีคำสั่ง Next

          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Threading;
          using System.Windows.Forms;
          
          namespace WindowsFormsApplication1
          {
              public partial class Form1 : Form
              {
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void label2_Click(object sender, EventArgs e)
                  {
          
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      progressBar1.Minimum = 0;
                      progressBar1.Maximum = 100;
                      progressBar1.Step = 1;
                      for (int x = 1; x <= 100; x++)
                     {
                      if (progressBar1.Value < 100)
                     {
                      progressBar1.PerformStep();
                      Thread.Sleep(100);
                      }
                       if (progressBar1.Value >= 100)
                      {
                       MessageBox.Show("Completed.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       progressBar1.Value = 0;
                      }
                    }
                  }
                  private void Form1_Load(object sender, EventArgs e)
               {
              }
            }
          }
          ----------------------------------------------------------
          ข้อควรสังเกตุ: เรื่องการควบคุมกำหนดระยะเวลาการทำงาน ..Control Loop ที่ใช้ Timer และคำสั่ง For
          จะเห็นได้ว่าใช้คำสั่งไม่เหมือนกัน
          ถ้า Timer ใช้คำสั่ง timer.Interval หรือปรับแต่งที่ในกรอบ Properties (ปกติโปรแกรมตั้งไว้ให้ที่ 100)
          ส่วน For ..จะใช้คำสั่ง Thread.Sleep เป็นตัวกำหนดระยะเวลา
          (อย่าลืม Import หรือนำเข้า Namespace คือ.. using System.Threading; ด้วยนะครับ)

          Comment


          • #20
            ดีมากเลยครับ...

            ขอตัวอย่างการใช้ Array ใน Console หน่อยสิครับ...

            เหอ ๆ

            Comment


            • #21
              มาแนะนำ tool สำหรับเขียน c# อีกตัวครับ ไฟล์ไม่ใหญ่ <20 Mb
              แต่คุณภาพเยี่ยมครับ ไม่ต่างจาก C# จากไมโครซอร์ฟเลยครับ
              SharpDev

              Comment


              • #22
                การใช้งาน MessageBox กล่อง หรือ กรอบข้อความ
                ทิ้งท้ายMsgBoxไว้ให้ตอนท้ายๆเลย เพราะเจตนาจะให้ความรู้เกี่ยวกับคำสั่งนี้โดยตรง
                รูปแบบคำสั่งก็ไม่ต่างจาก VB.NET เท่าไหร่
                ในที่นี้ผมจะขอแนะนำ การทำตัวติดตั้งโปรแกรมอัตโนมัติ (AutoSetup) ด้วย C# MessageBox
                แต่ก่อนที่จะทำ AutoSetup เรามาเรียนรู้เกี่ยวกับ MessageBox กันก่อนครับ
                ----------------------------------------------------------------------
                รูปแบบ: MessageBox ( Text, Title, Button, Icon)
                ประกอบด้วย..
                1.คำสั่งหลักที่ทำให้MessageBoxปรากฏขึ้น คือ MessageBox.Show
                2.Text คือ ข้อความที่เราเขียนขึ้น จะปรากฏอยู่ที่ในกล่องข้อความ
                3.Title คือ ข้อความที่เราเขียนขึ้น จะปรากฏอยู่ที่ในไตเติลบาร์ด้านบน
                4.Button คือ ปุ่มต่างๆที่ต้องการใช้งาน มีให้เลือกใช้ดังนี้
                ปุ่ม OK คำสั่งคือ MessageBoxButtons.OK
                ปุ่ม OK , Cancel คำสั่งคือ MessageBoxButtons.OKCancel
                ปุ่ม Yes , No คำสั่งคือ MessageBoxButtons.YesNo
                ปุ่ม Yes , No , Cancel คำสั่งคือ MessageBoxButtons.YesNoCancel
                5.Icon คือรูปเครื่องหมายต่างๆที่ต้องการให้ปรากฏบนกล่องข้อความ มีให้เลือกใช้ดังนี้
                รูปเครื่องหมาย คำถาม คำสั่งคือ MessageBoxIcon.Question
                รูปเครื่องหมาย รายละเอียด คำสั่งคือ MessageBoxIcon.Information
                รูปเครื่องหมาย เตือนให้ระวัง คำสั่งคือ MessageBoxIcon.Warning

                Comment


                • #23
                  เรามาลองเขียน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 Form1_Load(object sender, EventArgs e)
                          {
                  
                          }
                  
                          private void button1_Click(object sender, EventArgs e)
                          {
                           string message = "Hello World! ";
                           string caption = "Message Test";
                           MessageBoxButtons buttons = MessageBoxButtons.OK;
                              {
                               MessageBox.Show(message, caption, buttons);
                              }
                          }
                      }
                  }

                  Comment


                  • #24
                    MessageBox Project Test Sample
                    Download:http://upload.one2car.com/download.a...ECFOC96F79XAOL




                    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 Form1_Load(object sender, EventArgs e)
                            {
                    
                            }
                    
                            private void button1_Click(object sender, EventArgs e)
                            {
                                string message = "Hello World! ";
                                string caption = "Message Test";
                                MessageBoxButtons buttons = MessageBoxButtons.OK;
                                MessageBoxIcon Icon = MessageBoxIcon.Information;
                                {
                                    MessageBox.Show(message, caption, buttons, Icon);
                                }
                            }
                            private void button3_Click(object sender, EventArgs e)
                            {
                                string message = "Do you wanted test push button?";
                                string caption = "Message Test3";
                                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                                MessageBoxIcon Icon = MessageBoxIcon.Question;
                                DialogResult result;
                              result =  MessageBox.Show(message, caption, buttons, Icon);
                              if (result == System.Windows.Forms.DialogResult.Yes)
                              {
                                  MessageBox.Show("You click..Yes");
                              }
                              else
                              {
                                  MessageBox.Show("You click..No");
                              }
                            }
                            private void button2_Click(object sender, EventArgs e)
                            {
                                string message = "Error! ";
                                string caption = "Message Test2";
                                MessageBoxButtons buttons = MessageBoxButtons.OK;
                                MessageBoxIcon Icon = MessageBoxIcon.Warning;
                                {
                                    MessageBox.Show(message, caption, buttons, Icon);
                                }
                            }
                            private void button4_Click(object sender, EventArgs e)
                            {
                                string message = "Do you wanted test push button?";
                                string caption = "Message Test3";
                                MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
                                MessageBoxIcon Icon = MessageBoxIcon.Question;
                                DialogResult result;
                                result = MessageBox.Show(message, caption, buttons, Icon);
                                if (result == System.Windows.Forms.DialogResult.Yes)
                                {
                                    MessageBox.Show("You click..OK");
                                }
                                else
                                {
                                    MessageBox.Show("You click..Cancel");
                                }
                            }
                    
                            private void button5_Click(object sender, EventArgs e)
                            {
                                this.Close();
                            }
                        }
                    }

                    Comment


                    • #25
                      วิธีทำโปรแกรมติดตั้งอัตโนมัติด้วย C# MessageBox
                      ทำเป็นแบบ stand alone สดวกสบายเมื่อนำไปใช้งาน
                      โปรแกรมไหนที่คิดว่าใช้งานบ่อย ท่านก็ทำเก็บไว้
                      วิธีทำก็ง่ายมาก เขียนโค็ด MessageBox แบบเงื่อนไขขึ้นมา
                      ตามด้วยคำสั่งรันโปรแกรม
                      ใส่ชื่อ-สกุล และ parameter โปรแกรมที่จะติดตั้วลงไปในคำสั่งรัน
                      จากนั้นก็ลองทดสอบติดตั้งดู ..ถ้าOK ก็ build หรือ compile
                      ให้เป็นไฟล์สกุล .exe เก็บไว้ใช้งานต่อไป
                      -----------------------------------------------------
                      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;
                      using System.Windows.Forms;
                      
                      namespace WindowsFormsApplication1
                      {
                          public partial class Form1 : Form
                          {
                              public Form1()
                              {
                                  InitializeComponent();
                              }
                      
                              private void Form1_Load(object sender, EventArgs e)
                              {
                                  string message = "ท่านต้องการติดตั้งใช่หรือไม่?";
                                  string caption = "Winamp5 AutoSetup";
                                  MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                                  MessageBoxIcon Icon = MessageBoxIcon.Question;
                                  if (MessageBox.Show(message, caption, buttons, Icon)
                                      == DialogResult.Yes)
                                  {
                                      ProcessStartInfo startInfo = new ProcessStartInfo();
                      
                                      startInfo.FileName = "winamp557_full_7plus.exe";
                                      startInfo.Arguments = "/S";
                                      startInfo.WorkingDirectory = @"C:\\Documents and Settings\\Administrator\\Desktop";
                                      startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                                      startInfo.ErrorDialog = true;
                      
                                      Process process;
                                      {
                                          process = Process.Start(startInfo);
                                          process.WaitForExit();
                                          MessageBox.Show("ติดตั้งเรียบร้อย.");
                                          this.Close();
                                      }
                      
                                  }
                              }
                          }
                      }
                      --------------------------------------------------------
                      -:รายละเอียดการทำงานของโค๊ดคำสั่ง:-
                      namespace ที่ต้องใช้เพิ่มเติม คือ using System.Diagnostics;
                      โค๊ดทั้งหมดจะถูกใส่ไว้ที่ Form1_Load

                      if (MessageBox.Show(message, caption, buttons, Icon) == DialogResult.Yes) หมายถึง MessageBox เงื่อนไข Yes No

                      ProcessStartInfo startInfo = new ProcessStartInfo(); หมายถึง สร้าง option process ขึ้นมาใหม่ ..optionที่สร้างมีดังนี้

                      startInfo.FileName = "winamp557_full_7plus.exe"; หมายถึง โปรแกรมที่จะติดตั้ง

                      startInfo.Arguments = "/S"; หมายถึง parameter หรือคำสั่งติดตั้งอัตโนมัติ

                      startInfo.WorkingDirectory = @"C:\\Documents and Settings\\Administrator\\Desktop"; หมายถึง สถานที่ ที่เก็บหรือวาง ไฟล์โปรแกรมที่จะติดตั้งไว้ ..วางหรือเก็บไฟล์โปรแกรมไว้ที่ไหน? ต้องระบุ path ให้ถูกต้อง ..ในที่นี้ วางไฟล์โปรแกรมที่จะติดตั้งไว้บนหน้าจอ

                      startInfo.WindowStyle = ProcessWindowStyle.Hidden; หมายถึง สั่งไม่ให้เปิดหน้าต่างโปรแกรมขึ้นมา

                      process = Process.Start(startInfo); หมายถึง คำสั่งติดตั้งโปรแกรมอัตโนมัติ
                      -------------------------------------------------------------------
                      Tip: ถ้าการระบุ file path เป็นข้อความที่ยาวเกินไป ท่านสามารถใช้ VariableName แทนได้ดังนี้

                      string file path = "C:\\Documents and Settings\\Administrator\\Desktop";
                      startInfo.WorkingDirectory = (file path);
                      -------------------------------------------------------------------
                      Download:stand alone sample project
                      size:10.3 MB Click here

                      Last edited by sak2005; 21 Dec 2009, 12:39:59.

                      Comment


                      • #26
                        ขอถามหน่อยครับ
                        พอดีผมเพิ่งจะเริ่มเรียน C# โดยที่ผมไม่เคยมีพื้นฐานการเขียนโปรแกรมมาก่อนเลยครับ
                        ซื้อหนังสือมาแล้วก็ทำตามเลย
                        ทีนี้ติดปัญหาตรงที่ว่า ในการทำงานแบบ console กด F5 แล้วมันไม่แสดงผลเป็นภาษาไทยครับ
                        เหมือนกับว่ามันไม่รู้จักฟอนท์ มันจะขึ้นเครื่องหมาย ?????????
                        ลองกดคลิกขวา แล้วเข้าไปเซ็ทที่ Properties แล้วก็ไม่หายครับ
                        ไม่ทราบว่าจะต้องเซ็ทตรงไหนครับ
                        ผมใช้วินโดว์ 7 ครับ

                        Comment


                        • #27
                          หัวข้อต่อไปขอเป็นเรื่องการสั่ง print ออก printer นะครับ จะขอบคุณอย่างแรงเลย ^^"

                          Comment


                          • #28
                            ขอหัวข้อของ smart device มั่งครับ กะลังต้องการ select user pass จากเว็บ เพื่อตรวจสอบuser passผ่านจากโทรศัพท์เพื่อใช้ในการ login บนโทรศัพท์

                            Comment


                            • #29
                              Originally posted by SaMzAn View Post
                              ขอถามหน่อยครับ
                              พอดีผมเพิ่งจะเริ่มเรียน C# โดยที่ผมไม่เคยมีพื้นฐานการเขียนโปรแกรมมาก่อนเลยครับ
                              ซื้อหนังสือมาแล้วก็ทำตามเลย
                              ทีนี้ติดปัญหาตรงที่ว่า ในการทำงานแบบ console กด F5 แล้วมันไม่แสดงผลเป็นภาษาไทยครับ
                              เหมือนกับว่ามันไม่รู้จักฟอนท์ มันจะขึ้นเครื่องหมาย ?????????
                              ลองกดคลิกขวา แล้วเข้าไปเซ็ทที่ Properties แล้วก็ไม่หายครับ
                              ไม่ทราบว่าจะต้องเซ็ทตรงไหนครับ
                              ผมใช้วินโดว์ 7 ครับ
                              -------------------------------------------------
                              โพสท์ Code ที่เขียนให้ดูหน่อยครับ..เผื่อแก้ไขให้ได้

                              Comment


                              • #30
                                ตอนนี้ผมกำลัง ศึกษาเกี่ยวกับภาษาคอมครับ ไม่รู้จะเริ่มภาษาไหนก่อนดีครับ

                                พอดีเพิ่งสอบตรงติด คณะ เกี่ยวกับคอม อ่ะครับ คือ เราควรมีความรู้ ทางคณิต เปล่าครับ เรื่องอะไรบ้างที่เราควรสนใจ

                                Comment

                                Working...
                                X