วันพฤหัสบดีที่ 6 มิถุนายน พ.ศ. 2556

การรับ-ส่งค่าระหว่าง form c#

การรับ-ส่งค่าระหว่าง form 
   ในกรณีที่ต้องการรับ-ส่งค่าระหว่าง Window Form สามารถทำได้หลายวิธีในตัวอย่างนี้จะแสดงการส่งและ
รับค่าโดยใช้วิธี การสร้าง Delegate , constructor และ Method
การส่งและรับค่าโดยใช้ Delegate
1. สร้าง Window Form ขึ้นมาสอง Form ซึ่งมีชื่อเป็น Form1,Form2 โดยในตัวอย่างนี้จะทำการส่งค่า
    ที่รับมาจาก CheckBox ไปให้กับ Form2
   ใน Form1 ให้ลาก CheckedListBox และ Button มาวางดังรูป
alt.


จากนั้นคลิกที่รูปลูกศรด้านบนขวาของ
    CheckedListBox แล้วเลือก Edit Item ดังรูป


จากนั้นจะมีหน้าต่าง String Collection Editor ขึ้นมา
    ให้ใส่ข้อมูลลงไปสำหรับตัวเลือกแต่ละตัวของ checkbox พอใส่ข้อมูลเสร็จแล้วคลิกที่ปุ่ม

2.  จากนั้นดับเบิลคลิกที่ปุ่ม Button แล้วจะเข้าสู่หน้า Form1.cs เพื่อเขียนโค้ดให้เขียนโค้ดด้านบนการประกาศคลาสดังนี้
    public delegate void SendData(CheckedListBox chkList);
3. ใน event button1_Click เขียนโค้ดดังนี้
     private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendData send = new SendData(form2.GetData);
            send(checkedListBox1);
            form2.Show();
        }
4.   จากนั้นไปที่ Form2 ลาก Label มาวางใน Form คลิกที่ Label แล้วไปที่ properties กำหนดตรงช่อง Text ให้เป็นค่าว่าง
        จากนั้นไปที่ Form2.cs เขียนโค้ดดังนี้
         public void GetData(CheckedListBox chkList)
        {
            System.Collections.IEnumerator i = chkList.CheckedItems.GetEnumerator();
            while (i.MoveNext())
            {
                label1.Text += i.Current.ToString()+"\n";
            }
        }
ผลลัพธ์จะได้ดังรูป
alt.

alt.


ตัวอย่างโค้ดทั้งหมด
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public delegate void SendData(CheckedListBox chkList);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            SendData send = new SendData(form2.GetData);
            send(checkedListBox1);
            form2.Show();
        }
    }
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }
        public void GetData(CheckedListBox chkList)
        {
            System.Collections.IEnumerator i = chkList.CheckedItems.GetEnumerator();//chkList.CheckedIndices.GetEnumerator();
            while (i.MoveNext())
            {
                label1.Text += i.Current.ToString()+"\n";
            }
        }
    }
}
การส่งและรับค่าระหว่าง Form โดยใช้ constructor
1. ในตัวอย่างนี้ใน Form1 ให้ลาก TextBox,Button มาวางส่วน Form2 ก็ลาก Label มาวาง
    จากนั้นดับเบิลคลิกที่ button ใน Form1 แล้วเขียนโค้ดดังนี้
      private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.Show();
        }
2. จากนั้นไปที่ form2 เขียนโค้ดดังนี้
     public Form2(string data)
        {
            InitializeComponent();
            label1.Text = data;
        }
ผลลัพธ์จะได้ดังรูป



ตัวอย่างโค้ด
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.Show();
        }
    }
}
ตัวอย่างโค้ด
 Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2(string data)
        {
            InitializeComponent();
            label1.Text = data;
        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}
การรับส่งค่าระหว่าง Form โดยใช้ Method
ในตัวอย่างนี้ให้ลาก TextBox,Button มาวางใน form1 และลาก label มาวางใน form2
จากนั้นไปที่ form1
เขียนโค้ดดังนี้
 private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.textBox1.Text);
            form2.GetData(SendData());
            form2.Show();
        }
        public string SendData()
        {
            return textBox1.Text;
        }
จากนั้นไปที่ Form2 แล้วเขียนโค้ดดังนี้
  public void GetData(string str)
        {
            label1.Text = str;
        }
ผลลัพธ์จะได้ดังรูป


alt.

alt.

ตัวอย่างโค้ด
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.GetData(SendData());
            form2.Show();
        }
        public string SendData()
        {
            return textBox1.Text;
        }
    }
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {

        }
        public void GetData(string str)
        {
            label1.Text = str;
        }
    }
}
 
ที่มา
http://surin24.wordpress.com/2009/08/21/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%A3%E0%B8%B1%E0%B8%9A-%E0%B8%AA%E0%B9%88%E0%B8%87%E0%B8%84%E0%B9%88%E0%B8%B2%E0%B8%A3%E0%B8%B0%E0%B8%AB%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%87-form-c/ 

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