Tuesday, January 28

Passing Values Between Windows Forms

ตัวอย่างการส่งข้อมูลข้ามไปมา ระหว่างฟอร์ม ใน C# วันนี้จะนำเสนอ 3 รูปแบบ อย่างง่ายๆ จริงๆ อาจจะมีรูปแบบมากกว่านี้ แต่ ผมว่า 3 รูปแบบนี้ ก็ใช้งานดีครับ หรือถ้าใครมีรูปแบบอื่นๆ นำเสนอ ก็โพสมาได้เลยครับ

รูปแบบการส่งข้อมูลจากฟอร์มหลัก ไปฟอร์มอื่นๆ 3 รูปแบบดังนี้

- การส่งข้อมูลด้วยวิธี Delegate Method
- Constructor Method
- Function Method

โดยเราจะสร้างรูปแบบการส่งข้อมูลจากฟอร์มหลักที่อยู่ในฟอร์ม 1 แล้วส่งไปหาฟอร์มที่ 2 ,3 และ 4 ในแต่ละรูปแบบ เริ่มจากวางคอมโพเนนต์บนฟอร์มต่างๆ ตามรูป

โค๊ด Form1.cs

Passing Values Between Windows Forms

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 Passing_Values_Between_Windows_Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // ส่งค่าระหว่างฟอร์ม ด้วยวิธี delegate
        public delegate void SendData(CheckedListBox chkList);
        private void delegateButton_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            SendData send = new SendData(frm2.GetData);
            send(checkedListBox1);
            frm2.Show();
        }

        // ส่งค่าระหว่างฟอร์ม ด้วยวิธี constructor
        private void constructorButton_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3(textBox1.Text);
            frm3.Show();
        }

        // ส่งค่าระหว่างฟอร์ม ด้วยวิธี function
        private void button1_Click(object sender, EventArgs e)
        {
            Form4 frm4 = new Form4();
            frm4.GetData(SendValue());
            frm4.Show();
        }

        public string SendValue()
        {
            return textBox2.Text;
        }
       
        private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://visual-studio-express-project.blogspot.com/");
        }

    }
}

 

โค๊ด Form2.cs

Passing Values Between Windows Forms

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 Passing_Values_Between_Windows_Forms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            label1.Text = null;
        }

        public void GetData(CheckedListBox chkList)
        {
            System.Collections.IEnumerator i = chkList.CheckedItems.GetEnumerator();
            while (i.MoveNext())
            {
                label1.Text += i.Current.ToString() + "\n";
            }

        }
    }
}

 

โค๊ด Form3cs

Passing Values Between Windows Forms

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 Passing_Values_Between_Windows_Forms
{
    public partial class Form3 : Form
    {
        public Form3(string data)
        {
            InitializeComponent();
            label1.Text = null;
            label1.Text = data;
        }
    }
}

โค๊ด Form4.cs

Passing Values Between Windows Forms

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 Passing_Values_Between_Windows_Forms
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        public void GetData(string str)
        {
            label1.Text = str;
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}

เป็นโค๊ดอย่างง่ายๆ ดูแล้วไม่น่างง งั้นผมไม่ขออธิบายนะครับ หุๆๆๆ

0 ความคิดเห็น:

Post a Comment

 

แจกฟรี พื้นที่ฝากไฟล์ 2 GB

ads

ติดตามข่าวสารผ่าน Twitter

ติดตาม Blog นี้

Blog อื่นๆของฉัน

  • Test AMR Fleet Management program - ระบบบริหารจัดการจราจรและจัดคิวงานของรถ AMR (Autonomous Mobile Robot) ที่พัฒนาโดยบริษัท WP Solution Co., Ltd. จากภาษา C# ทั้งในส่วนของ Backend (Service app...
  • Gearset matching 2021 program - เป็นงานใหญ่ที่เพิ่งจบไป ที่ระยอง ปลวกแดง บริษัทผลิตชิ้นส่วนรถยนต์ คอนเซปต์ คือต้องการเก็บค่าหลังจากการทดสอบชิ้นส่วยรถยนต์ เพื่อเก็บไว้ในฐานข้อมูลให้แผนกถ...
  • RT ToolBox3 Standard version 1.70Y - กำลังศึกษาการใช้งานโปรแกรม RT ToolBox3 ในการเขียนโปรแกรมเพื่อควบคุมแขนกล Robots-MELFA ของค่าย Mitsubishi Facebook link
  • Free Screen Capture โปรแกรมบันทึกหน้าจอ ฟรีๆ - หากเพื่อนๆ จะมองหาโปรแกรม ที่ไว้สำหรับบันทึกหน้าจอคอมพิวเตอร์ ไม่ว่าจะบันทึกในรูปของภาพนีิ่ง (Screen shot) หรือแบบบันทึกเป็นภาพเคลื่อนไหว ( screencas...

จำนวนการเยี่ยมชมบล๊อก